-2
\$\begingroup\$

I'm new to PHP and making a simple dice game. It works well in single-player, but in multi-player, I don't know how to show the results of one player's rand(1,6) dice roll to the others using a web page.

How can I do this?

\$\endgroup\$
2
  • \$\begingroup\$ -1. This question shows no effort. \$\endgroup\$ Commented May 11, 2014 at 3:34
  • \$\begingroup\$ Im doing it singleplayer in isalcedo.com/G \$\endgroup\$ Commented May 12, 2014 at 0:28

1 Answer 1

2
\$\begingroup\$

You can use websockets to resolve this problem. To be short, a websocket provides a way to exchange messages between several clients and your server.

Principles

Your websocket application could behave like this :

  1. Whenever someone opens your game web page, javascript will open a websocket connection with the server (You don't need any library on the client side, only a recent web-browser)
  2. A client rolls the dice by clicking on a button
  3. Some javascript sends the value on the websocket
  4. The server receives the value and broadcasts it to all clients
  5. Some javascript handles the value received on clients page and displays it.

How to do that ?

Node.js is particularly convenient to handle websocket-based applications on the server side, and that's why so many people would recommend it. However, it is also possible to handle websocket in PHP.

I'm not goind to write a full tutorial, so the best advice i can give you is to find one on how to implement a chat application with websocket in PHP. Then if you are able to develop a chat, you should be able to develop almost any real-time multiplayer web game.

I have found this tutorial for PHP : http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket

Have a look here too : Javascript and PHP for real-time multiplayer?

\$\endgroup\$
1
  • \$\begingroup\$ That is enough to put me on the good way. Thanks a lot. \$\endgroup\$ Commented May 12, 2014 at 0:33

Not the answer you're looking for? Browse other questions tagged .