4
\$\begingroup\$

I'm working on a multiplayer checkers game. I want to let players choose another connected player and invite him to play. I have a working game engine and rendering, but I'm stuck on the multiplayer part.

This is specifically a web-based game.

  • How can I know whether a player is connected?
  • How can I detect when a player closes the browser, and set them to disconnected?
  • When a player sends an invitation to another, how do I let the first player know whether the other accepted?
  • How do I connect 2 players on a same game and send their moves to each other?
\$\endgroup\$
  • 2
    \$\begingroup\$ Consider looking at NodeJS or WebSockets. They are more suitable for this (realtime). \$\endgroup\$ – Lolums Sep 13 '15 at 7:53
1
\$\begingroup\$

How can I know whether a player is connected?

There are several ways, but you could use an AJAX request that pings every 10 seconds. If the user last timestamp was in the last 10+1 seconds, he should still be 'connected'.

How can I detect when a player closes the browser, and set them to disconnected?

Use the above solution, the user simply times out if there is more than 10 to 30 seconds since last AJAX request.

When a player sends an invitation to another, how do I let the first player know whether the other accepted?

If you have this properly set in the database, I think this is obvious, you should think a little harder if you know basic php & mysql.

How do I connect 2 players on a same game and send their moves to each other?

You send AJAX requests to the server, and the server responds to each player, the other player moves. You better use Web Sockets if you want a near real-time interaction

\$\endgroup\$
0
\$\begingroup\$

When you want to have a pure ajax solution, the clients need to send updates about their own state to the server in regular intervals in form of XmlHttpRequests. The server then responds to these request with the current state of the game and the other clients.

A disconnect can be detected by noticing that a client doesn't send updates anymore for an extended amount of time.

But notice that WebSockets are usually a far superior solution for games because:

  1. both client and server can send messages whenever they want. The server can push events immediately and doesn't need to wait until a client sends a request.
  2. the server gets notified when a client disconnects by closing their web browser.
\$\endgroup\$
0
\$\begingroup\$

Most modern web based games (and platform too) are based on UDP socket connection, it's like a tunnel with the server, each of them has a socket id, so you can either interact or identify every connection. Using sockets resolves all your troubles, it sends an action when users connects, disconects or emit's any call.

The easiest way to build a sockets app is with nodejs, you can build the sockets infrastructure in node and use your current php services from it.

I developed a morts that uses nodejs to connect clients and manage game rooms, python scripts to manage map objects and c++ "services" to do the hard calculations.

If you wanna stay with pure php you can build your own socket infrastructure, there are good documentation, just google "php socket".

Game rooms relative...In my online game i wrote an object called game that had all the code of a game and an array called Games that stores all the games, so, when two players enteres the lobby it runs something like this:

Games.push(new game(GAME_UID,PLAYERS,...));

and with an eassy search function you can associate games to its unique GAME_UID, PLAYER_ID or whatever.

Good luck ;)

\$\endgroup\$
  • \$\begingroup\$ "Most modern web based games (and platform too) are based on UDP socket connection" -- Source? That sounds suspect given that it's relatively difficult to form any kind of UDP connection from the browser without relying on Flash or nascent mostly-unsupported Web technologies like WebRTC datachannels. Are you perhaps confusing UDP connections with the TCP-based streams of WebSocket? \$\endgroup\$ – Sean Middleditch Dec 12 '15 at 20:48
  • \$\begingroup\$ Sorry, webgames works over tcp websockets \$\endgroup\$ – PRDeving Dec 13 '15 at 21:14
  • \$\begingroup\$ Also, extending @Sean's comment, if they would be available that wouldn't mean you should use them automatically. TCPs have the pro of being very reliable, if you send a socket form the server, then if both the client and the server is online, then its 100% it reaches the client. UDP in other hand has a package loss ratio, wich depends mostly on the connection of the 2 sides. Both has their use case. \$\endgroup\$ – Bálint Oct 6 '16 at 11:45
0
\$\begingroup\$

1. You could add a table to your database named rooms with a one-to-many relationship and do ajax calls to the database whenever a user enters or leaves a room.

2. javascript has these useful event listeners.

window.onbeforeunload = function() {
return "Hey, you're leaving the site. Bye!";
//or an ajax call to the database which deletes the user connectivity.
};

3. To make it more real-time you could add a javascript timer to tell which checks the database for the invited user's response to the invitation.

How do I connect 2 players on a same game and send their moves to each other?

4. You could do this with database relations. record players in a table and use the foreign key to join their moves.

This is if you don't have time to be able to learn websockets and other asynchronous web technologies. which allows for better structural design of your game.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.