List of usage examples for org.springframework.web.socket TextMessage TextMessage
public TextMessage(byte[] payload)
From source file:org.springframework.messaging.simp.stomp.StompWebSocketHandler.java
/** * Handle STOMP messages going back out to WebSocket clients. *//*from w w w. j a va 2s. c o m*/ @Override public void handleMessage(Message<?> message) { StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); headers.setCommandIfNotSet(StompCommand.MESSAGE); if (StompCommand.CONNECTED.equals(headers.getCommand())) { // Ignore for now since we already sent it return; } String sessionId = headers.getSessionId(); if (sessionId == null) { // TODO: failed message delivery mechanism logger.error("Ignoring message, no sessionId header: " + message); return; } WebSocketSession session = this.sessions.get(sessionId); if (session == null) { // TODO: failed message delivery mechanism logger.error("Ignoring message, sessionId not found: " + message); return; } if (StompCommand.MESSAGE.equals(headers.getCommand()) && (headers.getSubscriptionId() == null)) { // TODO: failed message delivery mechanism logger.error("Ignoring message, no subscriptionId header: " + message); return; } if (!(message.getPayload() instanceof byte[])) { // TODO: failed message delivery mechanism logger.error("Ignoring message, expected byte[] content: " + message); return; } try { message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build(); byte[] bytes = this.stompMessageConverter.fromMessage(message); session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8")))); } catch (Throwable t) { sendErrorMessage(session, t); } finally { if (StompCommand.ERROR.equals(headers.getCommand())) { try { session.close(CloseStatus.PROTOCOL_ERROR); } catch (IOException e) { } } } }
From source file:org.springframework.samples.portfolio.web.tomcat.TestStompClient.java
public void subscribe(String destination, MessageHandler messageHandler) { String id = String.valueOf(this.subscriptionIndex.getAndIncrement()); this.subscriptionHandlers.put(id, messageHandler); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE); headers.setSubscriptionId(id);/* w w w . j av a2 s. c om*/ headers.setDestination(destination); Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); byte[] bytes = encoder.encode(message); try { this.session.sendMessage(new TextMessage(new String(bytes, DEFAULT_CHARSET))); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.springframework.samples.portfolio.web.tomcat.TestStompClient.java
public void send(String destination, Object payload) { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND); headers.setDestination(destination); Message<byte[]> message = (Message<byte[]>) this.messageConverter.toMessage(payload, new MessageHeaders(headers.toMap())); byte[] bytes = this.encoder.encode(message); try {/* www . ja va2 s . c om*/ this.session.sendMessage(new TextMessage(new String(bytes, DEFAULT_CHARSET))); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter.java
@OnWebSocketMessage public void onWebSocketText(String payload) { TextMessage message = new TextMessage(payload); try {/*from w w w . jav a 2s. c o m*/ this.webSocketHandler.handleMessage(this.wsSession, message); } catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } }
From source file:org.springframework.web.socket.adapter.JettyWebSocketHandlerAdapter.java
@Override public void onWebSocketText(String payload) { TextMessage message = new TextMessage(payload); try {// w ww.jav a2s .c o m this.webSocketHandler.handleMessage(this.wsSession, message); } catch (Throwable t) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, t, logger); } }
From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java
/** * Invoked when no/*from w ww . j a v a 2 s.co m*/ * {@link #setErrorHandler(StompSubProtocolErrorHandler) errorHandler} * is configured to send an ERROR frame to the client. */ private void sendErrorMessage(WebSocketSession session, Throwable error) { StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR); headerAccessor.setMessage(error.getMessage()); byte[] bytes = this.stompEncoder.encode(headerAccessor.getMessageHeaders(), EMPTY_PAYLOAD); try { session.sendMessage(new TextMessage(bytes)); } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) logger.debug("Failed to send STOMP ERROR to client", ex); } }
From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java
private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload) { StompCommand command = stompAccessor.getCommand(); try {/*from www .j a va 2 s. co m*/ byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(session instanceof SockJsSession) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType())); if (useBinary) { session.sendMessage(new BinaryMessage(bytes)); } else { session.sendMessage(new TextMessage(bytes)); } } catch (SessionLimitExceededException ex) { // Bad session, just get out throw ex; } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) if (logger.isDebugEnabled()) { logger.debug("Failed to send WebSocket message to client in session " + session.getId(), ex); } command = StompCommand.ERROR; } finally { if (StompCommand.ERROR.equals(command)) { try { session.close(CloseStatus.PROTOCOL_ERROR); } catch (IOException ex) { // Ignore } } } }
From source file:org.springframework.web.socket.sockjs.AbstractSockJsSession.java
public void delegateMessages(String[] messages) throws Exception { for (String message : messages) { this.handler.handleMessage(this, new TextMessage(message)); }/*from www . j a va 2s . c om*/ }
From source file:org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.java
@Override public final void sendMessage(WebSocketMessage<?> message) throws IOException { if (!(message instanceof TextMessage)) { throw new IllegalArgumentException(this + " supports text messages only."); }/*from ww w .j a va2s. c om*/ if (this.state != State.OPEN) { throw new IllegalStateException(this + " is not open: current state " + this.state); } String payload = ((TextMessage) message).getPayload(); payload = getMessageCodec().encode(payload); payload = payload.substring(1); // the client-side doesn't need message framing (letter "a") TextMessage messageToSend = new TextMessage(payload); if (logger.isTraceEnabled()) { logger.trace("Sending message " + messageToSend + " in " + this); } sendInternal(messageToSend); }
From source file:org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.java
private void handleMessageFrame(SockJsFrame frame) { if (!isOpen()) { if (logger.isErrorEnabled()) { logger.error("Ignoring received message due to state " + this.state + " in " + this); }//from ww w .j av a2s . c om return; } String[] messages = null; String frameData = frame.getFrameData(); if (frameData != null) { try { messages = getMessageCodec().decode(frameData); } catch (IOException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to decode data for SockJS \"message\" frame: " + frame + " in " + this, ex); } silentClose(CloseStatus.BAD_DATA); return; } } if (messages == null) { return; } if (logger.isTraceEnabled()) { logger.trace("Processing SockJS message frame " + frame.getContent() + " in " + this); } for (String message : messages) { if (isOpen()) { try { this.webSocketHandler.handleMessage(this, new TextMessage(message)); } catch (Throwable ex) { logger.error("WebSocketHandler.handleMessage threw an exception on " + frame + " in " + this, ex); } } } }