As Erwin pointed out, you can adopt a higher protocol providing a feature like STOMP. However, if you are afraid to accept it only for this function, you can implement this function yourself.
- First of all, in order for each message
id identify each message type to recognize the purpose of each message, data for transferring the contents of the message and reply , which is the flag to see, does not require ACK and use the JSON format to serialize / deserialize the object containing this data in / from a WebSocket message. - When sending a message, it creates an object, creating a new
id for this message, setting type - message and data for the message and reply - true if ACK is required or false if not. And he serializes it to JSON and sends it as a WebSocket message. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110 - Upon receiving the message, it deserializes the JSON for the specified object. If
reply is true , it sends a special message whose type value is reply from data to id this message. Then a colleague can confirm that his colleague received a message with id . - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76
The above links point to a similar implementation in Cettia , which is the real-time web application framework I wrote. Although this implementation is a bit complicated because it is designed to allow the user to handle callbacks with the result, you are likely to get the main idea.
The API implemented by this link is as follows.
A server or client that requires an event processing result.
// Using Java server with lambda socket.send("foo", "bar", result -> , reason -> );
The appropriate client or server that is responsible for sending the result.
// Using JavaScript client with arrow functions socket.on("foo", (data, reply) => { // data is 'bar' // 'reply.resolve(result)' if it successes // 'reply.reject(reason)' if it fails });
Donghwan kim
source share