Question

I need to write a messaging protocol between client and server for a game application. I need to send messages back and forth such as, Player joined a game, player left game, player performed this action, etc.

I have a ClientMessage class that will construct a udp or tcp message to the client and send as plain text for now. But of course for the client to understand, there should be a simple protocol for each type of message (the aforementioned examples).

I am just not sure where to start as I've never had to. My only idea is

// Player actions
private int playerJoinGame = 100;
private int playerLeaveGame = 101;

// Game events
private int gameStart = 200;
private int gameEnd = 201;

Then a message would look like

public PlayerJoinGame(string playerName){
   string msg = playerJoinGame + ',' + playerName;
   SendClientMessage(msg);
}

Does this design pattern make sense? Is the only way to manage this, that is writing the responding end (client), to just be diligent and make sure when you update the server code, you update the cilent? (if event IDs change).

Thanks.

Was it helpful?

Solution

If you want a robust protocol that can handle things like an old server talking to a new client for a while (you can't always deploy everything to everywhere in an instant), you need your messages to conform to a specific, schema'd data format that allows for backwards compatible updates to said schema.

I believe XML and JSON are the most popular data formats upon which such protocols are usually built (where I work, we use some of both). XML has the advantage of a single, standardized schema format. JSON has the advantage of being generally less verbose. If you really want to, you can spec out your own extremely simple context-free grammer and write your own parser/builder routines for it, but it's probably worth the initial learning curve to integrate some existing XML or JSON processing libraries. I'm sure C# has plenty of them.

OTHER TIPS

I'd recommend ZeroMQ. While not as informative as writing your own messaging service, it works nicely out of the box and is available for virtually any language you could name, C# included. It's also widely used so you can find support when things go wrong or you want to bring someone else into the project.

It supports a huge number of formats and messaging protocols so it can be made to work however you want for the most part. It's also asynchronous which can be a nice advantage when you've got a bunch of clients.

Licensed under: CC-BY-SA with attribution
scroll top