From 7199b040ca0d8d8c72c3a354e8068dd8cc4d8ff6 Mon Sep 17 00:00:00 2001 From: dds Date: Fri, 26 Jul 2019 11:48:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/demo/WebSocketServer.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/demo/WebSocketServer.java b/src/main/java/com/example/demo/WebSocketServer.java index 4b6d032..a3cd90b 100644 --- a/src/main/java/com/example/demo/WebSocketServer.java +++ b/src/main/java/com/example/demo/WebSocketServer.java @@ -9,13 +9,14 @@ import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; +import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -@ServerEndpoint("/websocket") // 客户端URI访问的路径 +@ServerEndpoint("/ws/{userId}") // 客户端URI访问的路径 @Component public class WebSocketServer { @@ -28,10 +29,10 @@ public class WebSocketServer { private Session session; @OnOpen - public void onOpen(Session session) { + public void onOpen(Session session, @PathParam("userId") String userId) { this.session = session; - System.out.println(session.getId() + ":建立连接"); + System.out.println(session.getId() + ":建立连接:" + userId); // 添加socket addSocket(session); } @@ -40,7 +41,7 @@ public class WebSocketServer { public void onClose() { Session session = this.session; String room = roomList.get(session.getId()); - CopyOnWriteArrayList curRoom;//= this.roomList.get(room);//获取对应房间的列表 + CopyOnWriteArrayList curRoom; if (room != null) { curRoom = rooms.get(room);//获取对应房间的列表 for (Session aCurRoom : curRoom) {