@Override public Map<String, Object> getAttributes() { return this.delegate.getAttributes(); }
private String getHttpSessionId(WebSocketSession wsSession) { Map<String, Object> attributes = wsSession.getAttributes(); return SessionRepositoryMessageInterceptor.getSessionId(attributes); }
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { session.getAttributes().put("decorated", true); super.afterConnectionEstablished(session); } }
@Test public void webSocketHandlerDecorator() throws Exception { ApplicationContext config = createConfig(WebSocketHandlerDecoratorConfig.class); WebSocketHandler handler = config.getBean(SubProtocolWebSocketHandler.class); assertNotNull(handler); SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) config.getBean("stompWebSocketHandlerMapping"); WebSocketHttpRequestHandler httpHandler = (WebSocketHttpRequestHandler) mapping.getHandlerMap().get("/test"); handler = httpHandler.getWebSocketHandler(); WebSocketSession session = new TestWebSocketSession("id"); handler.afterConnectionEstablished(session); assertEquals(true, session.getAttributes().get("decorated")); }
if (this.eventPublisher != null) { try { SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes()); SimpAttributesContextHolder.setAttributes(simpAttributes); Principal user = getUser(session);
headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(getUser(session)); headerAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, headerAccessor.getHeartbeat());
private Message<byte[]> createDisconnectMessage(WebSocketSession session) { StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.DISCONNECT); if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); Principal user = getUser(session); if (user != null) { headerAccessor.setUser(user); } return MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders()); }
assertEquals(true, session.getAttributes().get("decorated"));
@Before public void setup() { String sessionId = "session-id"; MapSession session = new MapSession(sessionId); this.attributes = new HashMap<>(); SessionRepositoryMessageInterceptor.setSessionId(this.attributes, sessionId); given(this.wsSession.getAttributes()).willReturn(this.attributes); given(this.wsSession.getPrincipal()).willReturn(this.principal); given(this.wsSession.getId()).willReturn("wsSession-id"); given(this.wsSession2.getAttributes()).willReturn(this.attributes); given(this.wsSession2.getPrincipal()).willReturn(this.principal); given(this.wsSession2.getId()).willReturn("wsSession-id2"); Map<String, Object> headers = new HashMap<>(); headers.put(SimpMessageHeaderAccessor.SESSION_ATTRIBUTES, this.attributes); given(this.message.getHeaders()).willReturn(new MessageHeaders(headers)); this.listener = new WebSocketRegistryListener(); this.connect = new SessionConnectEvent(this.listener, this.wsSession); this.connect2 = new SessionConnectEvent(this.listener, this.wsSession2); this.disconnect = new SessionDisconnectEvent(this.listener, this.message, this.wsSession.getId(), CloseStatus.NORMAL); this.deleted = new SessionDeletedEvent(this.listener, session); this.expired = new SessionExpiredEvent(this.listener, session); }
@Override public Map<String, Object> getAttributes() { return this.delegate.getAttributes(); }
@Override public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) throws Exception { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(session.getPrincipal()); headerAccessor.setHeader("content-length", webSocketMessage.getPayloadLength()); headerAccessor.setLeaveMutable(true); Message<?> message = MessageBuilder.createMessage(webSocketMessage.getPayload(), headerAccessor.getMessageHeaders()); try { SimpAttributesContextHolder.setAttributesFromMessage(message); outputChannel.send(message); } finally { SimpAttributesContextHolder.resetAttributes(); } }
private String getNameFromSession(WebSocketSession session) { Map<String, Object> attributes = session.getAttributes(); return (String) attributes.get(Constant.USER_NAME); } }
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception { int uid = (Integer) webSocketSession.getAttributes().get("uid"); if (userSocketSessionMap.get(uid) == null) { userSocketSessionMap.put(uid, webSocketSession); } }
@Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { RoomParticipant user = (RoomParticipant) session.getAttributes().get(USER); if (user != null && !user.isClosed()) { log.warn("Transport error", exception); } }
@Override public void handleTransportError(WebSocketSession session, Throwable exception) throws IOException { String username = (String) session.getAttributes().get("username"); session.close(CloseStatus.SERVER_ERROR); log.error("transport error, username:[{}]", username, exception); }
@Override public void afterConnectionEstablished(WebSocketSession session) throws IOException { String username = (String) session.getAttributes().get("username"); ConcurrentWebSocketSessionDecorator sessionDecorator = new ConcurrentWebSocketSessionDecorator(session, 1000, 5120); sessionMap.put(username, sessionDecorator); log.info("connect succ, username:[{}]", username); JSONObject jsonObject = new JSONObject(); jsonObject.put("action", "connect"); jsonObject.put("username", username); sessionDecorator.sendMessage(new TextMessage(jsonObject.toJSONString())); }
@Override public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) { Long wampSessionId = (Long) session.getAttributes() .get(WampMessageHeader.WAMP_SESSION_ID.name()); if (wampSessionId != null) { this.applicationEventPublisher.publishEvent(new WampDisconnectEvent( wampSessionId, session.getId(), session.getPrincipal())); this.wampSessionIds.remove(wampSessionId); session.getAttributes().remove(WampMessageHeader.WAMP_SESSION_ID.name()); } }
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { String principalId = (String) session.getAttributes().get("principalId"); String sessionId = session.getId(); // 客户端与服务器端建立连接后,此处记录谁下线了 logger.info("WebSocket Offline, Principal Id[{}], Session Id[{}]", principalId, sessionId); SocketSessionRegistry.unRegisterSessionId(principalId, sessionId); super.afterConnectionClosed(session, closeStatus); } }
@Override public void afterConnectionEstablished(final WebSocketSession session) throws Exception { String principalId = (String) session.getAttributes().get("principalId"); String sessionId = session.getId(); // 客户端与服务器端建立连接后,此处记录谁上线了 logger.info("WebSocket Online, Principal Id[{}], Session Id[{}]", principalId, sessionId); SocketSessionRegistry.registerSessionId(principalId, sessionId); super.afterConnectionEstablished(session); }
@Override public void onConnected(Session session) { super.onConnected(session); logger.debug("agent session created :" +session); //新建一个agent session 额外做一些操作 EnvSession envSession = new EnvSession(); envSession.setClusterId(Long.parseLong((String) session.getWebSocketSession().getAttributes().get("clusterId"))); envSession.setVersion((String) session.getWebSocketSession().getAttributes().get("version")); envSession.setRegisterKey(session.getRegisterKey()); optionalListener.onConn(envSession); agentSessionManager.onAgentCreated(session); }