小试spring websocket 简介配置

        需要实现继承重写两个类,一个是类是TextWebSocketHandler,一个是HttpSessionHandshakeInterceptor,后面的那个是对前面的那个hander的连接方法的处理。

        xmk中配置如下:

       <bean id="websocket1" class="com.ha.websocket.WebsocketEndPoint"/>  
  
   <websocket:handlers>  

        <!-- path表示对应的连接 -->
      <websocket:mapping path="/websocket.json" handler="websocket1"/>  
      <websocket:handshake-interceptors>  

       <!-- 这个class是连接的流程控制方法,这个重写HttpSessionHandshakeInterceptor的方法-->
      <bean class="com.ha.websocket.HandshakeInterceptor"/>  
      </websocket:handshake-interceptors>  

<!-- 配 置这个为sockjs连接-->
      <websocket:sockjs /> 
    </websocket:handlers>  
     <!-- 这个是websocket的配置,如果用websocket是ws的连接js连接-->
    <websocket:handlers>  
      <websocket:mapping path="/ws/websocket.json" handler="websocket1"/>  
      <websocket:handshake-interceptors>  
      <bean class="com.ha.websocket.HandshakeInterceptor"/>  
      </websocket:handshake-interceptors>  
    </websocket:handlers> 

js如何连接如下:

<script src="js/sockjs-0.3.min.js"></script>
<script>



         
         var websocket;
         if ('WebSocket' in window) {
             websocket = new WebSocket("ws://localhost:8080/sForm/ws/websocket.json");
         } else if ('MozWebSocket' in window) {
             websocket = new MozWebSocket("ws://localhost:8080/sForm/ws/websocket.json");
         } else {
             websocket = new SockJS("http://localhost:8080/sForm/websocket.json");
         }
         websocket.onopen = function (evnt) {
        console.log("it is start open");
        console.log('send start ');
             websocket.send('send a  message ');
         };
         websocket.onmessage = function (event){
        alert("received data is  "+event.data);
         };
        
         websocket.onclose = function (event) {
             console.log('Info: connection closed.');
             console.log(event);
             alert("it is time close");
         };
         
         
</script>

这样就好了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值