[英]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.
但是,如果您害怕仅将其用于该功能,则可以自己实现该功能。
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消息中反序列化。 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
该消息,设置type
到message
和data
到给定的消息和reply
到true
如果ACK是必需的或false
如果不是。 And it serializes it into JSON and sends it as a WebSocket message. reply
is true
, it sends a special message whose type
is reply
setting data
to id
of that message. reply
是true
,它发出了一个特殊的消息,其type
是reply
设置data
来id
该消息的。 Then, the counterpart can confirm that its counterpart has received a message whose id is id
. id
的消息。 - 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.