欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring框架下websocket的搭建

程序员文章站 2024-03-02 17:48:28
本文基于apach tomcat 8.0.3+myeclipse+maven+jdk1.7 spring4.0以后加入了对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包

<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须和spring mvc的version保持一致,否则会出现问题
  <version>4.0.5.release</version>
</dependency>
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-messaging</artifactid>
  <version>4.0.5.release</version>
</dependency>

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

<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配置(applicationcontext.xml文件)

<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);

    }

3 创建握手(handshake)接口

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  创建websocket处理类

public class hellohandler extends textwebsockethandler {
 
 public static list<websocketsession> users;
 static{
 users = new arraylist<websocketsession>();
 }
 
 @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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。