【问题标题】:how to send message to particular websocket connection using java server如何使用java服务器将消息发送到特定的websocket连接
【发布时间】:2013-06-13 06:03:01
【问题描述】:

我是 WebSockets 的新手。

我已经在 WebSockets 中做了一个简单的服务器-客户端聊天。

现在我正在尝试制作客户端-服务器-客户端聊天应用程序。

我有一个问题,在 java 服务器中我们如何向特定的 WebSocket 连接发送消息。

如果用户 A 想向用户 B 发送消息。

那么我该如何管理 User-B 正在使用这个或那个连接或向那个特定连接发送消息?

我在谷歌上搜索太多,但找不到任何好东西。

【问题讨论】:

    标签: javascript websocket


    【解决方案1】:

    你必须为此设计一个架构。

    当客户端与服务器建立连接(打开 WebSocket)时,服务器必须将连接保持在某个地方(无论您要识别与正在使用的 Java 后端的特定连接),其数据结构为将取决于你想要做什么。一个好的标识符应该是用户提供的一个 ID(比如连接到同一服务器的另一个对等方尚未选择的昵称)。否则,只需使用套接字对象作为唯一标识符,并在前端列出其他用户时,将它们与他们的唯一标识符相关联,以便客户端可以向特定对等方发送消息。

    如果客户端要与另一个特定客户端聊天,HashMap 将是一个不错的数据结构选择,因为您可以将客户端的唯一 ID 映射到套接字并在 O(1 ) 在哈希表中。

    如果您想从一个客户端向所有其他客户端广播消息,尽管HashMap 也可以很好地工作(使用类似HashMap.values()),您可以使用简单的List,发送传入的消息除了原始发件人之外的所有连接的客户端。

    当然,您还想在失去与数据结构的连接时从数据结构中删除客户端,这很容易使用 WebSocket(您使用的 Java 框架应该在套接字关闭时回调您)。

    这是一个使用Jetty 9 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));
        }
    }
    

    代码是不言自明的。关于 JSON 格式化和解析,Jetty 在 org.eclipse.jetty.util.ajax 包中有一些有趣的实用程序。

    另请注意,如果您的 WebSocket 服务器框架不是线程安全的,则需要同步数据结构以确保没有数据损坏(此处为 MyWebSocket.sockets)。

    【讨论】:

    • 你能给我建议任何链接或站点.....或任何参考代码......如何发送 uniqe id 任何我们如何在地图中设置它????
    • 如何获取目的地唯一 ID 和转义消息,请告诉我,因为我对 websocket 码头了解较少??
    • 也许你应该使用 ConcurrentHashMap :)
    • 或者您可以将 Hazelcast-IMap 用于多服务器/集群架构
    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多