@Before public void setup() { MockitoAnnotations.initMocks(this); this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel); when(stompHandler.getSupportedProtocols()) .thenReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp")); when(mqttHandler.getSupportedProtocols()).thenReturn(Arrays.asList("MQTT")); this.session = new TestWebSocketSession(); this.session.setId("1"); }
/** * Handle an inbound message from a WebSocket client. */ @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { WebSocketSessionHolder holder = this.sessions.get(session.getId()); if (holder != null) { session = holder.getSession(); } SubProtocolHandler protocolHandler = findProtocolHandler(session); protocolHandler.handleMessageFromClient(session, message, this.clientInboundChannel); if (holder != null) { holder.setHasHandledMessages(); } checkSessions(); }
private String resolveSessionId(Message<?> message) { for (SubProtocolHandler handler : this.protocolHandlerLookup.values()) { String sessionId = handler.resolveSessionId(message); if (sessionId != null) { return sessionId; } } if (this.defaultProtocolHandler != null) { String sessionId = this.defaultProtocolHandler.resolveSessionId(message); if (sessionId != null) { return sessionId; } } return null; }
/** * Register a sub-protocol handler. */ public void addProtocolHandler(SubProtocolHandler handler) { List<String> protocols = handler.getSupportedProtocols(); if (CollectionUtils.isEmpty(protocols)) { logger.error("No sub-protocols for " + handler + "."); return; } for (String protocol: protocols) { SubProtocolHandler replaced = this.protocolHandlerLookup.put(protocol, handler); if ((replaced != null) && (replaced != handler) ) { throw new IllegalStateException("Can't map " + handler + " to protocol '" + protocol + "'. Already mapped to " + replaced + "."); } } this.protocolHandlers.add(handler); }