@Override public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) { return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs); }
@Override public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException { WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex); } }
@Override public void sendMessageInternal(String message) throws SockJsTransportFailureException { // Open frame not sent yet? // If in the session initialization thread, then cache, otherwise wait. if (!this.openFrameSent) { synchronized (this.initSessionLock) { if (!this.openFrameSent) { this.initSessionCache.add(message); return; } } } cancelHeartbeat(); writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message)); scheduleHeartbeat(); }
public void initializeDelegateSession(WebSocketSession session) { synchronized (this.initSessionLock) { this.webSocketSession = session; try { // Let "our" handler know before sending the open frame to the remote handler delegateConnectionEstablished(); this.webSocketSession.sendMessage(new TextMessage(SockJsFrame.openFrame().getContent())); // Flush any messages cached in the mean time while (!this.initSessionCache.isEmpty()) { writeFrame(SockJsFrame.messageFrame(getMessageCodec(), this.initSessionCache.poll())); } scheduleHeartbeat(); this.openFrameSent = true; } catch (Throwable ex) { tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); } } }
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception { String payload = message.getPayload(); if (StringUtils.isEmpty(payload)) { return; } String[] messages; try { messages = getSockJsServiceConfig().getMessageCodec().decode(payload); } catch (Throwable ex) { logger.error("Broken data received. Terminating WebSocket connection abruptly", ex); tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA); return; } if (messages != null) { delegateMessages(messages); } }
public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException { this.attributes = attributes; if (wsHandler instanceof SockJsWebSocketHandler) { // work around SPR-12716 SockJsWebSocketHandler sockJs = (SockJsWebSocketHandler) wsHandler; WebSocketServerSockJsSession session = (WebSocketServerSockJsSession) ReflectionTestUtils .getField(sockJs, "sockJsSession"); this.attributes = session.getAttributes(); } return true; } }
@Override public void afterConnectionClosed(WebSocketSession wsSession, CloseStatus status) throws Exception { this.sockJsSession.delegateConnectionClosed(status); }
@Override public void handleTextMessage(WebSocketSession wsSession, TextMessage message) throws Exception { this.sockJsSession.handleMessage(message, wsSession); }
@Override public void handleTransportError(WebSocketSession webSocketSession, Throwable exception) throws Exception { this.sockJsSession.delegateError(exception); }
public void initializeDelegateSession(WebSocketSession session) { synchronized (this.initSessionLock) { this.webSocketSession = session; try { // Let "our" handler know before sending the open frame to the remote handler delegateConnectionEstablished(); this.webSocketSession.sendMessage(new TextMessage(SockJsFrame.openFrame().getContent())); // Flush any messages cached in the mean time while (!this.initSessionCache.isEmpty()) { writeFrame(SockJsFrame.messageFrame(getMessageCodec(), this.initSessionCache.poll())); } scheduleHeartbeat(); this.openFrameSent = true; } catch (Throwable ex) { tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); } } }
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception { String payload = message.getPayload(); if (StringUtils.isEmpty(payload)) { return; } String[] messages; try { messages = getSockJsServiceConfig().getMessageCodec().decode(payload); } catch (Throwable ex) { logger.error("Broken data received. Terminating WebSocket connection abruptly", ex); tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA); return; } if (messages != null) { delegateMessages(messages); } }
@Override public void afterConnectionClosed(WebSocketSession wsSession, CloseStatus status) throws Exception { this.sockJsSession.delegateConnectionClosed(status); }
@Override public void handleTextMessage(WebSocketSession wsSession, TextMessage message) throws Exception { this.sockJsSession.handleMessage(message, wsSession); }
@Override public void handleTransportError(WebSocketSession webSocketSession, Throwable exception) throws Exception { this.sockJsSession.delegateError(exception); }
public void initializeDelegateSession(WebSocketSession session) { synchronized (this.initSessionLock) { this.webSocketSession = session; try { // Let "our" handler know before sending the open frame to the remote handler delegateConnectionEstablished(); this.webSocketSession.sendMessage(new TextMessage(SockJsFrame.openFrame().getContent())); // Flush any messages cached in the mean time while (!this.initSessionCache.isEmpty()) { writeFrame(SockJsFrame.messageFrame(getMessageCodec(), this.initSessionCache.poll())); } scheduleHeartbeat(); this.openFrameSent = true; } catch (Throwable ex) { tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); } } }
@Override public void sendMessageInternal(String message) throws SockJsTransportFailureException { // Open frame not sent yet? // If in the session initialization thread, then cache, otherwise wait. if (!this.openFrameSent) { synchronized (this.initSessionLock) { if (!this.openFrameSent) { this.initSessionCache.add(message); return; } } } cancelHeartbeat(); writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message)); scheduleHeartbeat(); }
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception { String payload = message.getPayload(); if (StringUtils.isEmpty(payload)) { return; } String[] messages; try { messages = getSockJsServiceConfig().getMessageCodec().decode(payload); } catch (Throwable ex) { logger.error("Broken data received. Terminating WebSocket connection abruptly", ex); tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA); return; } if (messages != null) { delegateMessages(messages); } }
@Override public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException { WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex); } }
@Test public void getSubProtocolsNone() throws Exception { WebSocketHandler handler = new TextWebSocketHandler(); TaskScheduler scheduler = mock(TaskScheduler.class); DefaultSockJsService service = new DefaultSockJsService(scheduler); WebSocketServerSockJsSession session = new WebSocketServerSockJsSession("1", service, handler, null); SockJsWebSocketHandler sockJsHandler = new SockJsWebSocketHandler(service, handler, session); assertEquals(Collections.emptyList(), sockJsHandler.getSubProtocols()); }
@Override public void afterConnectionClosed(WebSocketSession wsSession, CloseStatus status) throws Exception { this.sockJsSession.delegateConnectionClosed(status); }