use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class AbstractClientSockJsSession method sendMessage.
@Override
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
if (!(message instanceof TextMessage)) {
throw new IllegalArgumentException(this + " supports text messages only.");
}
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);
// the client-side doesn't need message framing (letter "a")
payload = payload.substring(1);
TextMessage messageToSend = new TextMessage(payload);
if (logger.isTraceEnabled()) {
logger.trace("Sending message " + messageToSend + " in " + this);
}
sendInternal(messageToSend);
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class WebSocketMessageBrokerConfigurationSupportTests method clientInboundChannelSendMessage.
@Test
public void clientInboundChannelSendMessage() throws Exception {
ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class);
SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class);
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size() - 1).getClass());
TestWebSocketSession session = new TestWebSocketSession("s1");
session.setOpen(true);
webSocketHandler.afterConnectionEstablished(session);
TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build();
webSocketHandler.handleMessage(session, textMessage);
Message<?> message = channel.messages.get(0);
StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
assertNotNull(accessor);
assertFalse(accessor.isMutable());
assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType());
assertEquals("/foo", accessor.getDestination());
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class StompSubProtocolHandlerTests method handleMessageToClientWithBinaryWebSocketMessage.
// SPR-12475
@Test
public void handleMessageToClientWithBinaryWebSocketMessage() {
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
headers.setMessageId("mess0");
headers.setSubscriptionId("sub0");
headers.setContentType(MimeTypeUtils.APPLICATION_OCTET_STREAM);
headers.setDestination("/queue/foo");
// Non-empty payload
byte[] payload = new byte[1];
Message<byte[]> message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
this.protocolHandler.handleMessageToClient(this.session, message);
assertEquals(1, this.session.getSentMessages().size());
WebSocketMessage<?> webSocketMessage = this.session.getSentMessages().get(0);
assertTrue(webSocketMessage instanceof BinaryMessage);
// Empty payload
payload = EMPTY_PAYLOAD;
message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
this.protocolHandler.handleMessageToClient(this.session, message);
assertEquals(2, this.session.getSentMessages().size());
webSocketMessage = this.session.getSentMessages().get(1);
assertTrue(webSocketMessage instanceof TextMessage);
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class StompSubProtocolHandlerTests method handleMessageFromClientWithInvalidStompCommand.
@Test
public void handleMessageFromClientWithInvalidStompCommand() {
TextMessage textMessage = new TextMessage("FOO\n\n\0");
this.protocolHandler.afterSessionStarted(this.session, this.channel);
this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
verifyZeroInteractions(this.channel);
assertEquals(1, this.session.getSentMessages().size());
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertTrue(actual.getPayload().startsWith("ERROR"));
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAck.
@Test
public void handleMessageToClientWithSimpConnectAck() {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setHeartbeat(10000, 10000);
accessor.setAcceptVersion("1.0,1.1");
Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] { 15000, 15000 });
Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
this.protocolHandler.handleMessageToClient(this.session, ackMessage);
assertEquals(1, this.session.getSentMessages().size());
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertEquals("CONNECTED\n" + "version:1.1\n" + "heart-beat:15000,15000\n" + "user-name:joe\n" + "\n" + "