Deploying queues for Websocket

Asked

Viewed 637 times

8

I’m developing a server WebSocket which will control multi client connections, will basically function as a chat, will be a kind of game multiplayer, after many researches and some tests I realized that when using Socket for messages in asynchronous it is not possible to know if the message actually arrived to the client server and there is still the risk of the messages arriving in the wrong order and this serial crucial in a multiplayer game, after some research saw that the solution to this will be to implement a message queue for the socket, implementing the queue apparently is smooth theoretically would add all the messages to the end of the queue and send the messages from the first position and so respectively, but my doubt is how I’ll be sure that the first message was sent to send the second in line and so on, if I have to implement a listener for this to be a server response and I have to inform which message is the response that would influence the latency of my connection which by the way needs to be low, I’m having a hard time figuring out how to fix this.

The server application is done in C# I am using the framework Alchemy Client applications are javascript using WebSockets(HTML5)

3 answers

4

You can study the basic functionality of TCP, which provides for proper sorting of packages.

as your connection is not persistent you will basically need a control within the package, an ID starting at 0 that is incremented with each transport, so on the server it is possible to recreate the order of the messages from the ID’s.

3

regarding "doubt is how I will be sure that the first message was sent to send the second of the queue and so on..."

Here’s the thing, in socket, the server side can "hear" a client, when the client sends a message to the server it will do the scheduled routine for that message and can make a return to the client (or everyone who is listening to multiplayer type), if you want a confirmation of who received the message from the server, you can program another message to the server in the client saying that this message was received by such client.

0

To create a multi-client socket server, it is necessary to store the client connections Ids in a queue, it can be a simple array where in each received connection is stored the connection array that will be used to send specific messages.

When creating this list it is important to think about how you can recover the client connection ID, for example using the user name as the array key and the value the connection ID.

When you need to send a specific message to this client, you can recover the connection, write to its buffer and continue processing. If an error occurs while trying to write to the client buffer, you will know that the message was not delivered.

About the queue of messages the server receives, it is nothing more than a new line that was written to the server buffer that is recovered. If you need to create a queue of commands received by the server, you need to set a message pattern so that you can interpret and separate when the server receives the new commands.

Here is an example of implementation: http://ashishware.com/websocksample.shtml

I made an explanation about how to work with PHP implementation in Send a message to the specific user in Websocket PHP

Browser other questions tagged

You are not signed in. Login or sign up in order to post.