简体   繁体   English

Web Socket-Spring:确认收到的消息

[英]Web Socket - Spring : Confirm of message received

I am sending a message through WebSocket with Spring from Tomcat Server to a SockJSClient with the method: 我正在通过Spring通过WebSocket从Tomcat服务器向SockJSClient发送消息,方法是:

WebSocketSession.sendMessage(WebSocketMessage<?> message)

and I would like to know when the message has been received (eventually with complementary information, for example whether the logic on client successfully processed), then go for next message. 我想知道何时接收到消息(最终带有补充信息,例如,客户端逻辑是否成功处理),然后再发送下一条消息。

This is an Activity diagram that explains the use case. 这是一个活动图,解释了用例。 在此处输入图片说明

How can I receive confirmation of reception or result from client? 如何接收客户的确认或结果确认?

As Erwin pointed, you can adopt some higher protocol providing such feature like STOMP. 正如Erwin指出的那样,您可以采用一些更高的协议来提供诸如STOMP之类的功能。 However, if you are afraid to adopt it only for that feature, you can implement that feature by yourself. 但是,如果您害怕仅将其用于该功能,则可以自己实现该功能。

  • The first thing is to give each message id to identify each message, type to recognize the purpose of each message, data to transport a message's content and reply which is a flag to see whether or not ACK is required and to use a format like JSON to serialize/deserialize an object containing these data into/from WebSocket message. 第一件事是给每个消息id以标识每个消息, type以识别每个消息的目的, data以传输消息的内容并进行reply ,这是一个标志,用于查看是否需要ACK并使用JSON之类的格式将包含这些数据的对象序列化/反序列化到WebSocket消息中/从WebSocket消息中反序列化。
  • When sending a message, it creates an object by issuing a new id for that message, setting type to message and data to given message and reply to true if ACK is required or false if not. 当发送消息时,它通过发出一个新创建的对象id该消息,设置typemessagedata到给定的消息和replytrue如果ACK是必需的或false如果不是。 And it serializes it into JSON and sends it as a WebSocket message. 并将其序列化为JSON并将其作为WebSocket消息发送。 - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110 -https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110
  • When receiving a message, it deserializes JSON to the above object. 收到消息时,它将JSON反序列化为上述对象。 If reply is true , it sends a special message whose type is reply setting data to id of that message. 如果replytrue ,它发出了一个特殊的消息,其typereply设置dataid该消息的。 Then, the counterpart can confirm that its counterpart has received a message whose id is id . 然后,对方可以确认对方收到了id为id的消息。 - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76 -https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76

The above links point similar implementation in Cettia which is a real-time web application framework I wrote. 上面的链接指向了Cettia中的类似实现, Cettia是我编写的实时Web应用程序框架。 Though that implementation is a little bit complex as it is designed to allow for user to handle callbacks with result, you are likely to get the basic idea. 尽管该实现有些复杂,因为它被设计为允许用户处理带有结果的回调,但是您可能会得到基本的想法。

API implemented by that link looks like the following. 该链接实现的API如下所示。

A server or client which requires a result of event processing. 需要事件处理结果的服务器或客户端。

// Using Java server with lambda
socket.send("foo", "bar", result -> /* resolved */, reason -> /* rejected */);

The corresponding client or server which has a responsibility to submit 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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM