Search in sources :

Example 1 with SockJsFrame

use of org.springframework.web.socket.sockjs.frame.SockJsFrame in project spring-framework by spring-projects.

the class PollingSockJsSession method flushCache.

@Override
protected void flushCache() throws SockJsTransportFailureException {
    String[] messages = new String[getMessageCache().size()];
    for (int i = 0; i < messages.length; i++) {
        messages[i] = getMessageCache().poll();
    }
    SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
    SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, messages);
    writeFrame(frame);
}
Also used : SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.SockJsMessageCodec) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame)

Example 2 with SockJsFrame

use of org.springframework.web.socket.sockjs.frame.SockJsFrame in project spring-framework by spring-projects.

the class HttpSendingTransportHandlerTests method frameFormats.

@Test
public void frameFormats() throws Exception {
    this.servletRequest.setQueryString("c=callback");
    this.servletRequest.addParameter("c", "callback");
    SockJsFrame frame = SockJsFrame.openFrame();
    SockJsFrameFormat format = new XhrPollingTransportHandler().getFrameFormat(this.request);
    String formatted = format.format(frame);
    assertEquals(frame.getContent() + "\n", formatted);
    format = new XhrStreamingTransportHandler().getFrameFormat(this.request);
    formatted = format.format(frame);
    assertEquals(frame.getContent() + "\n", formatted);
    format = new HtmlFileTransportHandler().getFrameFormat(this.request);
    formatted = format.format(frame);
    assertEquals("<script>\np(\"" + frame.getContent() + "\");\n</script>\r\n", formatted);
    format = new EventSourceTransportHandler().getFrameFormat(this.request);
    formatted = format.format(frame);
    assertEquals("data: " + frame.getContent() + "\r\n\r\n", formatted);
    format = new JsonpPollingTransportHandler().getFrameFormat(this.request);
    formatted = format.format(frame);
    assertEquals("/**/callback(\"" + frame.getContent() + "\");\r\n", formatted);
}
Also used : SockJsFrameFormat(org.springframework.web.socket.sockjs.frame.SockJsFrameFormat) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) Test(org.junit.Test)

Example 3 with SockJsFrame

use of org.springframework.web.socket.sockjs.frame.SockJsFrame in project spring-framework by spring-projects.

the class StreamingSockJsSession method flushCache.

@Override
protected void flushCache() throws SockJsTransportFailureException {
    while (!getMessageCache().isEmpty()) {
        String message = getMessageCache().poll();
        SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
        SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message);
        writeFrame(frame);
        this.byteCount += (frame.getContentBytes().length + 1);
        if (logger.isTraceEnabled()) {
            logger.trace(this.byteCount + " bytes written so far, " + getMessageCache().size() + " more messages not flushed");
        }
        if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) {
            logger.trace("Streamed bytes limit reached, recycling current request");
            resetRequest();
            this.byteCount = 0;
            break;
        }
    }
    scheduleHeartbeat();
}
Also used : SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.SockJsMessageCodec) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame)

Example 4 with SockJsFrame

use of org.springframework.web.socket.sockjs.frame.SockJsFrame in project spring-framework by spring-projects.

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithStompFrame.

@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/destination");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message);
    TextMessage textMessage = new TextMessage(bytes);
    SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
    String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : Jackson2SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) CloseStatus(org.springframework.web.socket.CloseStatus) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Aggregations

SockJsFrame (org.springframework.web.socket.sockjs.frame.SockJsFrame)4 Test (org.junit.Test)2 SockJsMessageCodec (org.springframework.web.socket.sockjs.frame.SockJsMessageCodec)2 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 CloseStatus (org.springframework.web.socket.CloseStatus)1 TextMessage (org.springframework.web.socket.TextMessage)1 Jackson2SockJsMessageCodec (org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec)1 SockJsFrameFormat (org.springframework.web.socket.sockjs.frame.SockJsFrameFormat)1