Refine search
@Override protected void sendInternal(TextMessage textMessage) throws IOException { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); this.webSocketSession.sendMessage(textMessage); }
MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType())); if (useBinary) { session.sendMessage(new BinaryMessage(bytes)); session.sendMessage(new TextMessage(bytes)); logger.debug("Failed to send WebSocket message to client in session " + session.getId(), ex); if (StompCommand.ERROR.equals(command)) { try { session.close(CloseStatus.PROTOCOL_ERROR);
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // WebSocketHandlerDecorator could close the session if (!session.isOpen()) { return; } this.stats.incrementSessionCount(session); session = decorateSession(session); this.sessions.put(session.getId(), new WebSocketSessionHolder(session)); findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel); }
@Nullable private Principal getUser(WebSocketSession session) { Principal user = this.stompAuthentications.get(session.getId()); return (user != null ? user : session.getPrincipal()); }
public static void tryCloseWithError(WebSocketSession session, Throwable exception, Log logger) { if (logger.isErrorEnabled()) { logger.error("Closing session due to exception for " + session, exception); } if (session.isOpen()) { try { session.close(CloseStatus.SERVER_ERROR); } catch (Throwable ex) { // ignore } } }
@Override protected void writeFrameInternal(SockJsFrame frame) throws IOException { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); if (logger.isTraceEnabled()) { logger.trace("Writing " + frame); } TextMessage message = new TextMessage(frame.getContent()); this.webSocketSession.sendMessage(message); }
@Override public Principal getPrincipal() { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); return this.webSocketSession.getPrincipal(); }
ByteBuffer byteBuffer; if (webSocketMessage instanceof TextMessage) { byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes()); BufferingStompDecoder decoder = this.decoders.get(session.getId()); if (decoder == null) { throw new IllegalStateException("No decoder for session id '" + session.getId() + "'"); if (logger.isErrorEnabled()) { logger.error("Failed to parse " + webSocketMessage + " in session " + session.getId() + ". Sending STOMP ERROR to client.", ex); headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(getUser(session)); headerAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, headerAccessor.getHeartbeat()); if (isConnect) { Principal user = headerAccessor.getUser(); if (user != null && user != session.getPrincipal()) { this.stompAuthentications.put(session.getId(), user); if (logger.isErrorEnabled()) { logger.error("Failed to send client message to application via MessageChannel" + " in session " + session.getId() + ". Sending STOMP ERROR to client.", ex);
if (this.eventPublisher != null) { try { SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes()); SimpAttributesContextHolder.setAttributes(simpAttributes); Principal user = getUser(session); if (errorMessage != null) { accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class); Assert.state(accessor != null, "No StompHeaderAccessor"); payload = errorMessage.getPayload();
/** * Returns a session given a web socket. * * @param socket A web socket (may be null). * @return The session corresponding to the specified socket. If the socket was null, determines * the session from the execution context. * @throws IllegalStateException If no session corresponds to the specified socket. */ private static Session resolveSession(WebSocketSession socket) { Session session = socket == null ? ExecutionContext.getSession() : (Session) socket.getAttributes().get(Session.ATTR_SESSION); Assert.state(session != null, () -> "Request received on unknown socket"); return session; }
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { session.sendMessage(new TextMessage("go go")); } };
private void sendJsonMessage(String jsonMessage) throws IOException { synchronized (wsSession) { if (wsSession.isOpen()) { wsSession.sendMessage(new TextMessage(jsonMessage)); } else { log.error("Trying to send a message to a closed session"); } } } }, wsSession.getId());
private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception { List<TextMessage> messages = new ArrayList<>(); for (int i = 0; i < messageCount; i++) { messages.add(new TextMessage("m" + i)); } TestClientHandler handler = new TestClientHandler(); initSockJsClient(transport); URI url = new URI(this.baseUrl + "/echo"); WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get(); for (TextMessage message : messages) { session.sendMessage(message); } handler.awaitMessageCount(messageCount, 5000); for (TextMessage message : messages) { assertTrue("Message not received: " + message, handler.receivedMessages.remove(message)); } assertEquals("Remaining messages: " + handler.receivedMessages, 0, handler.receivedMessages.size()); session.close(); }
@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); }
public class GreetingHandler extends TextWebSocketHandler { @Override public void handleTextMessage(WebSocketSession session, TextMessage message) { Thread.sleep(3000); // simulated delay TextMessage msg = new TextMessage("Hello, " + message.getPayload() + "!"); session.sendMessage(msg); } }
@Override public void sendMessage(WebSocketMessage<?> message) throws IOException { this.delegate.sendMessage(message); }
@Test // SPR-11648 public void sendSubscribeToControllerAndReceiveReply() throws Exception { TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build(); String destHeader = "destination:/app/number"; TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", destHeader).build(); TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1); WebSocketSession session = doHandshake(clientHandler, "/ws").get(); try { assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)); String payload = clientHandler.actual.get(1).getPayload(); assertTrue("Expected STOMP destination=/app/number, got " + payload, payload.contains(destHeader)); assertTrue("Expected STOMP Payload=42, got " + payload, payload.contains("42")); } finally { session.close(); } }
@Override public String getId() { return this.delegate.getId(); }
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()); }