如何从 REST bean 中找到 WebSocket

How To find a WebSocket from a REST bean

我有一个 javascript 客户端,它通过 websockets 连接到服务器并通过 ajax 将数据发送到服务器。 服务器处理完数据后,我想通过 websocket 向客户端发送一条消息。 问题是:如何从 REST bean 中找到 WebSocket(希望在同一会话中...)

Websocket 连接以 HTTP 握手开始。在握手时,您还会收到 cookie(例如会话 ID cookie),因此您可以访问 HTTP 会话。

From the JSR 356 Java API for WebSocket spec:

Because websocket connections are initiated with an http request, there is an association between the HttpSession under which a client is operating and any websockets that are established within that HttpSession. The API allows access in the opening handshake to the unique HttpSession corresponding to that same client.

这就是以您认为合适的任何方式将 HTTP 会话与 websocket 连接相关联的关键。