I'm planning to write a real time co-op multiplayer game. Currently I'm in the research phase. I've already written a turn-based game which used websockets and it was working fine.
I haven't tried writing a real time game using this technology however. My questions is about websockets. Is there an alternative way to handle munications between (browser) clients? My idea is to have the game state in each client and only send the deltas to the clients using the server as a mediator/synchronization tool.
My main concern is network speed. I want clients to be able to receive each other's actions as fast as possible so my game can stay real time. I have about 20-30 frames per second with less than a KByte of data per frame (which means a maximum of 20-30 KBytes of data per second per client).
I know that things like "network speed" depend on the connection but I'm interested in the "if all else equals" case.
I'm planning to write a real time co-op multiplayer game. Currently I'm in the research phase. I've already written a turn-based game which used websockets and it was working fine.
I haven't tried writing a real time game using this technology however. My questions is about websockets. Is there an alternative way to handle munications between (browser) clients? My idea is to have the game state in each client and only send the deltas to the clients using the server as a mediator/synchronization tool.
My main concern is network speed. I want clients to be able to receive each other's actions as fast as possible so my game can stay real time. I have about 20-30 frames per second with less than a KByte of data per frame (which means a maximum of 20-30 KBytes of data per second per client).
I know that things like "network speed" depend on the connection but I'm interested in the "if all else equals" case.
Share Improve this question edited Feb 24, 2015 at 11:27 Adam Arold asked Feb 23, 2015 at 22:06 Adam AroldAdam Arold 30.5k25 gold badges118 silver badges218 bronze badges 9- how could something be faster than websockets, which lets you ship raw binary with just a few bytes overhead? 30kb/s is also too much for the general public, if it's tied to frames at all. – dandavis Commented Feb 23, 2015 at 23:04
- 4 I don't know that's why I asked. – Adam Arold Commented Feb 24, 2015 at 11:27
- 1 fair enough, my point is that with websockets you can ship data as fast as the network goes and with about as minimal latency and overhead as it gets. 30FPS should be no problem, socket trips are usually under 10ms (not counting TTFB), unless your server is far away, in which case it can take up to 15ms to go mostly around the world. 15ms*2=30ms round trip, 1000ms/30ms=33.3FPS. shipping pure deltas likely means shipping a key:value pair, but an ordered array of values-only would probably be "cheaper", even with the extra mas creating a sparse array whilst omitting redundant elements. – dandavis Commented Feb 24, 2015 at 21:02
- Sounds good. You may want to put that as an answer so I can upvote it. – Adam Arold Commented Feb 24, 2015 at 22:46
- @dandavis, latency times are much greater than you think. transatlantic (eg: NY-London) is typically > 100ms. if you are unlucky enough (from a network engineering perspective) to live in New Zealand you can expect at least 150ms to /any/ inexpensive datacentre based hosting. many of the big telcos publish historical round trip times. eg: verizonenterprise./about/network/latency – mrdunk Commented Jun 10, 2016 at 22:29
1 Answer
Reset to default 12From a standard browser, a webSocket is going to be your best bet. The only two alternatives are webSocket and Ajax. Both are TCP under the covers so once the connection is established they pretty much offer the same transport. But, a webSocket is a persistent connection so you save connection overhead everytime you want to send something. Plus the persistent connection of the webSocket allows you to send directly from server to client which you will want.
In a typical gaming design, the underlying gaming engine needs to adapt to the speed of the transport between the server and any given client. If the client has a slower connection, then you have to drop the number of packets you send to something that can keep up (perhaps fewer frame updates in your case). The connection speed is what it is so you have to make your app deliver the best experience you can at the speed that there is.
Some other things you can do to optimize the use of the transport:
Collect all data you need to send at one time and send it in one larger send operation rather than lots of small sends. In a webSocket, don't send three separate messages each with their own data. Instead, create one message that contains the info from all three messages.
Whenever possible, don't rely on the latency of the connection by sending, waiting for a response, sending again, waiting for response, etc... Instead, try to parallelize operations so you send, send, send and then process responses as they e in.
Tweak settings for outgoing packets from your server so their is no Nagle delay waiting to see if there is other data to go in the same packet. See Nagle's Algorithm. I don't think you have the ability in a browser to tweak this from the client.
Make sure your data is encoded as efficiently as possible for smallest packet size.