WebSocket
Create a simple Spring WebSocket application
Add POM dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span></dependency><span data-mce-type="bookmark" id="mce_SELREST_end" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
Add WebSocket handler
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {
@Bean
SimpleTextWebSocketHandler webSocketHandler() {
return new SimpleTextWebSocketHandler();
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry
.addHandler(webSocketHandler(), "/echo")
.setAllowedOrigins("*");
}
}
<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
In the above configuration, there is important line that is setAllowedOrigins(“*”), if we don’t have this, the WebSocket handshake request could respond with 403 request forbidden.
public class SimpleTextWebSocketHandler extends TextWebSocketHandler {
private WebSocketSession session;
@Override
protected void handleTextMessage(
WebSocketSession session,
TextMessage message) throws Exception {
super.handleTextMessage(session, message);
System.out.println(message.getPayload());
session.sendMessage(new TextMessage("Hi"));
}
}<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
Now I have just set up the WebSocket server. Continue, I will test it
Send message from client to server
I use the extension from Chrome for testing

Open the WebSocket connection
Send a “hii” message to the endpoint and I receive the response immediately
Send message from server to client
WebSocket is a full-duplex, bidirectional connection, so we will try to send a message from server side to client side
Customize the message handler so that I can keep the WebSocket session
public class SimpleTextWebSocketHandler extends TextWebSocketHandler {
private WebSocketSession session;
@Override
protected void handleTextMessage(
WebSocketSession session,
TextMessage message) throws Exception {
this.session = session;
super.handleTextMessage(session, message);
System.out.println(message.getPayload());
session.sendMessage(new TextMessage("Hi"));
}
public void sendMessage(String message) {
if(session!=null) {
try {
session.sendMessage(new TextMessage(message));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
By this way, I can send the message to client.
SockJS
Problem with WebSocket
Over the public Internet, restrictive proxies outside your control may preclude WebSocket interactions because:
- They are not configured to pass on the Upgrade header
- They close long lived connections that appear idle
Solution from SockJS
It allows applications use a WebSocket API but fall back to non-WebSocket alternatives when necessary at runtime, i.e. without the need to change the application code.
WebSocket
Create a simple Spring WebSocket application
Add POM dependencies
Add WebSocket handler
In the above configuration, there is important line that is setAllowedOrigins(“*”), if we don’t have this, the WebSocket handshake request could respond with 403 request forbidden.
Now I have just set up the WebSocket server. Continue, I will test it
Send message from client to server
I use the extension from Chrome for testing
Open the WebSocket connection
Send a “hii” message to the endpoint and I receive the response immediately
Send message from server to client
WebSocket is a full-duplex, bidirectional connection, so we will try to send a message from server side to client side
Customize the message handler so that I can keep the WebSocket session
By this way, I can send the message to client.
SockJS
Problem with WebSocket
Over the public Internet, restrictive proxies outside your control may preclude WebSocket interactions because:
- They are not configured to pass on the Upgrade header
- They close long lived connections that appear idle
Solution from SockJS
It allows applications use a WebSocket API but fall back to non-WebSocket alternatives when necessary at runtime, i.e. without the need to change the application code.
WebSocket problem
http://host:port/myApp/myEndpoint/server-id/session-id/transport
Test
@Bean
public ServletServerContainerFactoryBean servletServerContainerFactoryBean() {
final ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container
.setMaxTextMessageBufferSize(
this.webSocketProperties.getMaxTextBufferSize());
container
.setMaxBinaryMessageBufferSize(
this.webSocketProperties.getMaxBinaryBufferSize());
return container;
}<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
Test other paragraph
public Integer getMaxBinaryBufferSize() {
return maxBinaryBufferSize;
}
It concludes that…
Like this:
Like Loading...