如何在spring环境中搭建websocket

今天就跟大家聊聊有关如何在spring环境中搭建websocket,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

黄山区网站建设公司创新互联公司,黄山区网站设计制作,有大型网站制作公司丰富经验。已为黄山区上千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的黄山区做网站的公司定做!

本文基于Apach Tomcat 8.0.3+MyEclipse+maven+JDK1.7

spring4.0以后加入了对websocket技术的支持,撸主目前的项目用的是SSM(springMVC+spring+MyBatis)框架,所以肯定要首选spring自带的websocket

1 在maven的pom.xml中加入websocket所依赖的jar包


 com.fasterxml.jackson.core
 jackson-core
 2.4.0


 com.fasterxml.jackson.core
 jackson-databind
 2.4.0


  org.springframework
  spring-websocket//version须和spring mvc的version保持一致,否则会出现问题
  4.0.5.RELEASE


  org.springframework
  spring-messaging
  4.0.5.RELEASE

2 更新spring-mvc.xml中namespace.xsd的版本

处理类和握手协议的spring配置(applicationContext.xml文件)




  
  
  
  
   

webconfig

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

    }

3 创建握手(handshake)接口

public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
 @Override
 public boolean beforeHandshake(ServerHttpRequest arg0,
  ServerHttpResponse arg1, WebSocketHandler arg2,
  Map 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  创建websocket处理类

public class HelloHandler extends TextWebSocketHandler {
 
 public static List users;
 static{
 users = new ArrayList();
 }
 
 @Override
  public void handleTextMessage(WebSocketSession session, TextMessage message) {
    //接收到客户端消息时调用
    System.out.println("text message: " + session.getId() + "-" + message.getPayload());
  }

  @Override
  public void afterConnectionEstablished(WebSocketSession session)
      throws Exception {
    // 与客户端完成连接后调用
    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("你好"));
    
    users.add(session);
  }

  @Override
  public void handleTransportError(WebSocketSession session,
      Throwable exception) throws Exception {
    // 消息传输出错时调用
    System.out.println("handleTransportError");
  }

  @Override
  public void afterConnectionClosed(WebSocketSession session,
      CloseStatus closeStatus) throws Exception {
    // 一个客户端连接断开时关闭
    System.out.println("afterConnectionClosed");
  }

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

看完上述内容,你们对如何在spring环境中搭建websocket有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


网站题目:如何在spring环境中搭建websocket
URL标题:http://scyanting.com/article/jjcoho.html