Deploy queues for WebSocket

8

I am developing a WebSocket server that will control multi client connections, it will basically work as a chat, it will be a kind of multiplayer game , after many searches and some tests I realized that when using Socket for messages in asynchronous it is not possible to know if the message actually reached the client \ server and there is still the risk of the messages reaching the wrong order and this is crucial serial in a multiplayer game, after some research I saw that the solution to this was to implement a message queue for the socket, implementing the queue apparently is quiet theoretically would add all messages to the end of the queue and send the message from the first position and so respectively, but my doubt is how will I make sure the first message was sent to send the second in the queue and so on, if I have to implement a listener pair to this is a response from the server, besides having to inform which message is the response, this would influence the latency of my connection, which by the way has to be low, I am having difficulty solving this.

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

    
asked by anonymous 03.04.2014 / 15:31

3 answers

4

You can study the basic functionality of TCP, which provides for proper ordering of packets.

Since your connection is not persistent, you will basically need a control inside the package, an ID starting at 0 that is incremented every transport, so the server can recreate the order of messages from the ID's.

    
16.05.2014 / 21:21
3

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

In the socket, the server side can "listen" to a client, when the client sends a message to the server it will do the routine scheduled for that message and can make a return to the client (or all if you want to receive a confirmation message from the server, you can schedule another message to be sent to the server saying that such a message was received by that client.

    
03.07.2014 / 13:03
0

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

When mounting this list it is important to think about how you will be able to retrieve the client connection ID, for example, use the username as the array key and the value of the connection ID.

When you need to send a specific message to this client, you can retrieve the connection, write to the 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 message queue the server receives, is nothing more than a new line that was written to the server buffer that is retrieved. If you need to create a command queue received by the server, you will have to define a message pattern so that you can interpret and separate when the server receives the new commands.

Here is an example implementation: link

I made an explanation of the operation however with implementation in PHP at Send message to specific user in Websocket PHP

    
16.11.2015 / 05:52