Java Code Examples for org.springframework.web.socket.sockjs.frame.SockJsFrame#messageFrame()

The following examples show how to use org.springframework.web.socket.sockjs.frame.SockJsFrame#messageFrame() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamingSockJsSession.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example 2
Source File: RestTemplateXhrTransportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: StreamingSockJsSession.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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();
}
 
Example 4
Source File: RestTemplateXhrTransportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 5
Source File: StreamingSockJsSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 6
Source File: RestTemplateXhrTransportTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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(Charset.forName("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);
}
 
Example 7
Source File: PollingSockJsSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: PollingSockJsSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 9
Source File: PollingSockJsSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	resetRequest();
}