gpt4 book ai didi

spring框架下websocket的搭建

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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包 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< 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的版本 。

?
1
2
3
4
5
6
< 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文件) 。

?
1
2
3
4
5
6
7
8
9
<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 。

?
1
2
3
4
5
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
       registry.addHandler( new HelloHandler(), "/hello" ).addInterceptors( new HandshakeInterceptor()).withSockJS().setHttpMessageCacheSize( 20000 );
 
    }

3 创建握手(handshake)接口 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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处理类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 ;
   }
}

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

原文链接:http://www.cnblogs.com/difme/p/5584204.html 。

最后此篇关于spring框架下websocket的搭建的文章就讲到这里了,如果你想了解更多关于spring框架下websocket的搭建的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com