Construction of websocket under spring framework

  • 2020-06-15 09:08:27
  • OfStack

This paper is based on Apach Tomcat 8.0.3+MyEclipse+maven+JDK1.7

spring4.0 after adding the support of websocket technology, the main project is using SSM (springMVC+spring+MyBatis) framework, so must first spring comes with websocket

Add jar packages that websocket depends on to ES19en. xml of maven


<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-core</artifactId>
 <version>2.4.0</version>
</dependency>
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>2.4.0</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-websocket</artifactId>//version To be and spring mvc the version keep 1 Cause, otherwise there will be problems 
  <version>4.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-messaging</artifactId>
  <version>4.0.5.RELEASE</version>
</dependency>

Update namespace. xsd in spring-ES28en. xml


<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:websocket="http://www.springframework.org/schema/websocket"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">

spring configuration for handling classes and handshake protocols (applicationContext.xml file)


<bean id="websocket" class="com.xl.websocket.handler"/>

<websocket:handlers>
  <websocket:mapping path="/websocket" handler="websocket"/>
  <websocket:handshake-interceptors>
  <bean class="com.xl.websocket.HandshakeInterceptor"/>
  </websocket:handshake-interceptors>
   <websocket:sockjs/>
</websocket:handlers>

webconfig


@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
      registry.addHandler(new HelloHandler(), "/hello").addInterceptors(new HandshakeInterceptor()).withSockJS().setHttpMessageCacheSize(20000);

    }

Create the Handshake (handshake) interface


public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
 @Override
 public boolean beforeHandshake(ServerHttpRequest arg0,
  ServerHttpResponse arg1, WebSocketHandler arg2,
  Map<String, Object> arg3) throws Exception {
 
 System.out.println("---- Before Handshake ----");
 return super.beforeHandshake(arg0, arg1, arg2, arg3);
 }
 
 @Override
 public void afterHandshake(ServerHttpRequest request,
  ServerHttpResponse response, WebSocketHandler wsHandler,
  Exception ex) {
 
 System.out.println("---- After Handshake ----");
 super.afterHandshake(request, response, wsHandler, ex);
 }
}

4 Create the websocket processing class


public class HelloHandler extends TextWebSocketHandler {
 
 public static List<WebSocketSession> users;
 static{
 users = new ArrayList<WebSocketSession>();
 }
 
 @Override
  public void handleTextMessage(WebSocketSession session, TextMessage message) {
    // Called when a client message is received 
    System.out.println("text message: " + session.getId() + "-" + message.getPayload());
  }

  @Override
  public void afterConnectionEstablished(WebSocketSession session)
      throws Exception {
    //  Call after completing the connection with the client 
    System.out.println("afterConnectionEstablished");
    System.out.println("getId:" + session.getId());
    System.out.println("getLocalAddress:" + session.getLocalAddress().toString());
    System.out.println("getTextMessageSizeLimit:" + session.getTextMessageSizeLimit());
    System.out.println("getUri:" + session.getUri().toString());
    System.out.println("getPrincipal:" + session.getPrincipal());
    System.out.println(soslistService.getsss());
    session.sendMessage(new TextMessage(" hello "));
    
    users.add(session);
  }

  @Override
  public void handleTransportError(WebSocketSession session,
      Throwable exception) throws Exception {
    //  Called when a message transfer error occurs 
    System.out.println("handleTransportError");
  }

  @Override
  public void afterConnectionClosed(WebSocketSession session,
      CloseStatus closeStatus) throws Exception {
    // 1 Closed when the client connection is disconnected 
    System.out.println("afterConnectionClosed");
  }

  @Override
  public boolean supportsPartialMessages() {
    // TODO Auto-generated method stub
    return false;
  }
}


Related articles: