We are writing online multiplayer game in Flash wish .Net server side, we need duplex channel so communication is done using sockets.
Can you tell me some best practices about protocol/format of exchanging data between server and client? IMO one of good choise may be using JSON serialization.
TCP sockets are used for communication between a server and a client process. The server's code runs first, which opens a port and listens for incoming connection requests from clients. Once a client connects to the same (server) port, the client or server may send a message.
I think, JSON can be the best choice and let me explain why.
Firstly, JSON-serialized game traffic is ligth-weighted, because JSON notation is like "name : val", not like "<name> val </name>"
.
Secondly, JSON serialization is the most intuitive one for games. XML and XML-like notations are good for multiline data, but in-game variables' values are usually rather short (HP, mana, AP, damage, etc.) and can be discribed with one line.
Thirdly, Actionscript 3 (I hope, you won't write MMO in AS2) has got perfect tools for JSON decoding/encoding. XML-parsing tools are good too, but IMO, JSON's are better. Also, in Flash 11+, JSON operations are extremely optimized.
Fourthly, many high-load projects use cache-servers with MongoDB, and it's main format is BSON (Binary JSON), so using JSON in this case will simplify data communication between DB and everything else.
It depends. Forget the involved technologies for a while and focus on your specific comm needs. Hope you don't mind if I explore a few related points as well.
If your code needs any kind of synchronous content, then you may need a reply flow control on you code, and a Messaging structure of some sort where Messages have an ID and may have a Original Message ID to which they are a reply. This will help you control content even on async protocols. In this case, an easily maintanable protocol like JSON may be better, as @JustLogin mentioned.
Full-Duplex mean you be staging more data on your network/interpreter layer, and it probably will have to handle asynchronous communications.
If performance is a concern, and depending on your specifics, then proposals like the one made by @Viacheslav can be attractive. A custom protocol can be heavily optimized, at the cost of maintanability and portability (several languages have their own JSON interpreter, while custom interpreters may need to be implemented or encapsuled, adding layers to the process and thus lowering performance, or requiring code conversion.)
If you couldn't care less if a packet or two are lost, then UDP may be a viable solution. Less control than TCP means a lighter protocol, but you're never sure that the data reached the other side unless you implement your own control.
TL;DR version: I would stick to standard low-level protocols like JSON, even if they add some overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With