javascript 如何使用java服务器向特定的websocket连接发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17080216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send message to particular websocket connection using java server
提问by manankhh
I am new to WebSockets.
我是 WebSockets 的新手。
I have already made a simple server-client chat in WebSockets.
我已经在 WebSockets 中进行了简单的服务器-客户端聊天。
And now I am trying to make client-server-client chat application.
现在我正在尝试制作客户端-服务器-客户端聊天应用程序。
I have a question that in java server how can we send a message to particular WebSocket connection.
我有一个问题,在 Java 服务器中,我们如何向特定的 WebSocket 连接发送消息。
If user-A want to send a message to User-B.
如果用户 A 想向用户 B 发送消息。
Then how can I manage that User-B is using this or that connection or send a message to that particular connection?
那么我如何管理用户 B 正在使用这个或那个连接或者向那个特定连接发送消息?
I am searching too much for this on google but could not find anything good.
我在谷歌上搜索了太多,但找不到任何好的东西。
回答by eepp
You have to design an architecture for that.
你必须为此设计一个架构。
When a client establishes a connection with the server (opens the WebSocket), the server has to keep the connection somewhere (howsoever you're identifying a specific connection with the Java backend you're using), in a data structure that will depend on what you're trying to do. A good identifier would be an ID the user provides (like a nickname that's not already picked by another peer connected to the same server). Otherwise, simply use the socket object as a unique identifier and, when listing other users on the frontend, associate them with their unique identifier so that a client can send a message to a specific peer.
当客户端建立与服务器的连接(打开 WebSocket)时,服务器必须将连接保持在某处(无论您标识与您正在使用的 Java 后端的特定连接),数据结构将取决于你想做什么。一个好的标识符应该是用户提供的 ID(例如连接到同一服务器的另一个对等点尚未选择的昵称)。否则,只需使用套接字对象作为唯一标识符,并在前端列出其他用户时,将他们与他们的唯一标识符相关联,以便客户端可以向特定对等方发送消息。
A HashMap
would be a good choice for a data structure if a client is going to chat with another specific client, as you can map the unique ID of a client to the socket and find an entry with in O(1) in a hash table.
HashMap
如果客户端要与另一个特定客户端聊天,A将是数据结构的不错选择,因为您可以将客户端的唯一 ID 映射到套接字并在哈希表中找到 O(1) 中的条目。
If you want to broadcast a message from a client to all other clients, although a HashMap
would also work pretty well (with something like HashMap.values()
), you may use a simple List
, sending the incoming message to all connected clients except the original sender.
如果您想从一个客户端向所有其他客户端广播消息,尽管 aHashMap
也可以很好地工作(使用类似HashMap.values()
),您可以使用一个简单的List
,将传入消息发送到除原始发件人之外的所有连接的客户端。
Of course, you also want to remove a client from the data structure when you lose connection with it, which is easy using a WebSocket (the Java framework you are using should call you back when a socket closes).
当然,您还希望在失去与数据结构的连接时从数据结构中删除客户端,这很容易使用 WebSocket(您使用的 Java 框架应该在套接字关闭时回调您)。
Here's an (almost complete) example using a Jetty9 WebSocket (and JDK 7):
这是一个使用Jetty9 WebSocket(和 JDK 7)的(几乎完整的)示例:
package so.example;
import java.io.IOException;
import java.util.HashMap;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
public class MyWebSocket {
private final static HashMap<String, MyWebSocket> sockets = new HashMap<>();
private Session session;
private String myUniqueId;
private String getMyUniqueId() {
// unique ID from this class' hash code
return Integer.toHexString(this.hashCode());
}
@OnWebSocketConnect
public void onConnect(Session session) {
// save session so we can send
this.session = session;
// this unique ID
this.myUniqueId = this.getMyUniqueId();
// map this unique ID to this connection
MyWebSocket.sockets.put(this.myUniqueId, this);
// send its unique ID to the client (JSON)
this.sendClient(String.format("{\"msg\": \"uniqueId\", \"uniqueId\": \"%s\"}",
this.myUniqueId));
// broadcast this new connection (with its unique ID) to all other connected clients
for (MyWebSocket dstSocket : MyWebSocket.sockets.values()) {
if (dstSocket == this) {
// skip me
continue;
}
dstSocket.sendClient(String.format("{\"msg\": \"newClient\", \"newClientId\": \"%s\"}",
this.myUniqueId));
}
}
@OnWebSocketMessage
public void onMsg(String msg) {
/*
* process message here with whatever JSON library or protocol you like
* to get the destination unique ID from the client and the actual message
* to be sent (not shown). also, make sure to escape the message string
* for further JSON inclusion.
*/
String destUniqueId = ...;
String escapedMessage = ...;
// is the destination client connected?
if (!MyWebSocket.sockets.containsKey(destUniqueId)) {
this.sendError(String.format("destination client %s does not exist", destUniqueId));
return;
}
// send message to destination client
this.sendClient(String.format("{\"msg\": \"message\", \"destId\": \"%s\", \"message\": \"%s\"}",
destUniqueId, escapedMessage));
}
@OnWebSocketClose
public void onClose(Session session, int statusCode, String reason) {
if (MyWebSocket.sockets.containsKey(this.myUniqueId)) {
// remove connection
MyWebSocket.sockets.remove(this.myUniqueId);
// broadcast this lost connection to all other connected clients
for (MyWebSocket dstSocket : MyWebSocket.sockets.values()) {
if (dstSocket == this) {
// skip me
continue;
}
dstSocket.sendClient(String.format("{\"msg\": \"lostClient\", \"lostClientId\": \"%s\"}",
this.myUniqueId));
}
}
}
private void sendClient(String str) {
try {
this.session.getRemote().sendString(str);
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendError(String err) {
this.sendClient(String.format("{\"msg\": \"error\", \"error\": \"%s\"}", err));
}
}
The code is self explanatory. About JSON formatting and parsing, Jetty has some interesting utilities within package org.eclipse.jetty.util.ajax
.
该代码是不言自明的。关于 JSON 格式化和解析,Jetty 在 package 中有一些有趣的实用程序org.eclipse.jetty.util.ajax
。
Also note that if your WebSocket server framework is not thread-safe, you will need to synchronize the data structure to make sure there's no data corruption (here MyWebSocket.sockets
).
另请注意,如果您的 WebSocket 服务器框架不是线程安全的,您将需要同步数据结构以确保没有数据损坏(此处MyWebSocket.sockets
)。