两个springboot+websocket之间的通信
2020回血第8天
链接: https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket.

@ServerEndpoint(value="/web/commodity/{fromUserId}/{toUserId}")//这个{}可有可无业务需求吧
public class WebSocketServer {
// 已经建立链接的对象缓存起来,也不一定非得用这个容器做容器-。-
private static ConcurrentMap<Integer, WebSocketServer> serverMap = new ConcurrentHashMap<Integer, WebSocketServer>();
// 当前session,嗯,做个样子,方便其他方法欣赏,借鉴
private Session currentSession;
@OnOpen
public void onOpen(Session session, @PathParam("fromUserId") int fromUserId, @PathParam("toUserId") int toUserId) throws IOException {
this.currentSession = session;
serverMap.put(fromUserId, this);
}
@OnClose
public void onClose(Session session, CloseReason reason) {
System.out.println(reason.toString());
//关闭链接时,删除缓存对象,要是做多个,就一个一个遍历吧,这个遍历容器删除嘛,-。-有小问题,
//自己注意吧。
serverMap