一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

利用spring

 別無(wú)分店 2017-06-20

折騰了將近一天,終于搭建好websocket服務(wù),中間遇到不少的坑,現(xiàn)在記錄一下,也供他們參考少走彎路。

開發(fā)環(huán)境:<spring.version>4.2.5.RELEASE</spring.version>     Tomcat8   jdk8  maven

spring搭建websocket有兩種方式:

1、利用javaee-api以注解的形式搭建,見(jiàn)上一篇博文《spring+tomcat8實(shí)現(xiàn)websocket 》


2、利用spring-websocket包搭建,本篇介紹的內(nèi)容。

注意:這種方式必須注釋掉這個(gè),不然會(huì)沖突

<!-- <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency> -->

需要用到的spring包:

<dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-websocket</artifactId>  
           <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-messaging</artifactId>  
           <version>${spring.version}</version>  
        </dependency>
其他輔助包:

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson-version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson-version}</version>
        </dependency>


Java代碼:兩個(gè)類  WebSocketConfig 和 SystemWebSocketHandler


  1. @Configuration  
  2. @EnableWebMvc  
  3. @EnableWebSocket  
  4. public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer{  
  5.   
  6.     @Override  
  7.     public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {  
  8.         registry.addHandler(systemWebSocketHandler(),"/webSocketServer");  
  9.         registry.addHandler(systemWebSocketHandler(),"/webSocketServer/sockjs").setAllowedOrigins("*").withSockJS();  
  10.          //registry.addHandler(systemWebSocketHandler(),"/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor());  
  11.          //registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor()).withSockJS();  
  12.          //registry.addHandler(systemWebSocketHandler(), "/webSocketServer/sockjs").withSockJS();  
  13.          /*registry.addHandler(systemWebSocketHandler(),"/ws").addInterceptors(new WebSocketHandshakeInterceptor()); 
  14.             registry.addHandler(systemWebSocketHandler(), "/ws/sockjs").addInterceptors(new WebSocketHandshakeInterceptor()) 
  15.                     .withSockJS();*/  
  16.     }  
  17.       
  18.     @Bean  
  19.     public WebSocketHandler systemWebSocketHandler(){  
  20.         return new SystemWebSocketHandler();  
  21.     }  
  22.       

  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.Date;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7. import org.springframework.messaging.Message;  
  8. import org.springframework.web.socket.CloseStatus;  
  9. import org.springframework.web.socket.TextMessage;  
  10. import org.springframework.web.socket.WebSocketHandler;  
  11. import org.springframework.web.socket.WebSocketMessage;  
  12. import org.springframework.web.socket.WebSocketSession;  
  13.   
  14. import com.mspj.frame.core.web.controller.BaseWebControllerImpl;  
  15.   
  16. public class SystemWebSocketHandler implements WebSocketHandler {  
  17.        
  18.     private Logger log = LoggerFactory.getLogger(SystemWebSocketHandler.class);  
  19.       
  20.     private static final ArrayList<WebSocketSession> users = new ArrayList<WebSocketSession>();;  
  21.    
  22.    
  23.     @Override  
  24.     public void afterConnectionEstablished(WebSocketSession session) throws Exception {  
  25.         System.out.println("ConnectionEstablished");  
  26.         log.debug("ConnectionEstablished");  
  27.         users.add(session);  
  28.           
  29.         session.sendMessage(new TextMessage("connect"));  
  30.         session.sendMessage(new TextMessage("new_msg"));  
  31.           
  32.     }  
  33.    
  34.     @Override  
  35.     public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {  
  36.         System.out.println("handleMessage" + message.toString());  
  37.         log.debug("handleMessage" + message.toString());  
  38.         //sendMessageToUsers();  
  39.         session.sendMessage(new TextMessage(new Date() + ""));  
  40.     }  
  41.    
  42.     @Override  
  43.     public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {  
  44.         if(session.isOpen()){  
  45.             session.close();  
  46.         }  
  47.         users.remove(session);  
  48.           
  49.         log.debug("handleTransportError" + exception.getMessage());  
  50.     }  
  51.    
  52.     @Override  
  53.     public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {  
  54.         users.remove(session);  
  55.         log.debug("afterConnectionClosed" + closeStatus.getReason());  
  56.           
  57.     }  
  58.    
  59.     @Override  
  60.     public boolean supportsPartialMessages() {  
  61.         return false;  
  62.     }  
  63.    
  64.     /** 
  65.      * 給所有在線用戶發(fā)送消息 
  66.      * 
  67.      * @param message 
  68.      */  
  69.     public void sendMessageToUsers(TextMessage message) {  
  70.         for (WebSocketSession user : users) {  
  71.             try {  
  72.                 if (user.isOpen()) {  
  73.                     user.sendMessage(message);  
  74.                 }  
  75.             } catch (IOException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.         }  
  79.     }  
  80.    
  81. }  

前端調(diào)式頁(yè)面代碼

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>WebSocket/SockJS Echo Sample (Adapted from Tomcat's echo sample)</title>  
  6.     <style type="text/css">  
  7.         #connect-container {  
  8.             float: left;  
  9.             width: 400px  
  10.         }  
  11.   
  12.         #connect-container div {  
  13.             padding: 5px;  
  14.         }  
  15.   
  16.         #console-container {  
  17.             float: left;  
  18.             margin-left: 15px;  
  19.             width: 400px;  
  20.         }  
  21.   
  22.         #console {  
  23.             border: 1px solid #CCCCCC;  
  24.             border-right-color: #999999;  
  25.             border-bottom-color: #999999;  
  26.             height: 170px;  
  27.             overflow-y: scroll;  
  28.             padding: 5px;  
  29.             width: 100%;  
  30.         }  
  31.   
  32.         #console p {  
  33.             padding: 0;  
  34.             margin: 0;  
  35.         }  
  36.     </style>  
  37.   
  38.     <script src="http://cdn./sockjs-0.3.min.js"></script>  
  39.   
  40.     <script type="text/javascript">  
  41.         var ws = null;  
  42.         var url = null;  
  43.         var transports = [];  
  44.   
  45.         function setConnected(connected) {  
  46.             document.getElementById('connect').disabled = connected;  
  47.             document.getElementById('disconnect').disabled = !connected;  
  48.             document.getElementById('echo').disabled = !connected;  
  49.         }  
  50.   
  51.         function connect() {  
  52.             if (!url) {  
  53.                 alert('Select whether to use W3C WebSocket or SockJS');  
  54.                 return;  
  55.             }  
  56.               
  57.               
  58.             //ws = new WebSocket('ws://192.168.10.107:8080/mspjapi/webSocketServer');/* (url.indexOf('sockjs') != -1) ?   
  59.                 //new SockJS(url, undefined, {protocols_whitelist: transports}) :  */  
  60.                 ws = new SockJS("http://192.168.10.107:8080/mspjapi/webSocketServer/sockjs");  
  61.                 //console.log("http://192.168.10.107:8080/mspjapi/webSocketServer/sockjs");  
  62.                   
  63.             ws.onopen = function () {  
  64.                 setConnected(true);  
  65.                 log('Info: connection opened.');  
  66.             };  
  67.               
  68.             ws.onmessage = function (event) {  
  69.                 log('Received: ' + event.data);  
  70.             };  
  71.               
  72.             ws.onclose = function (event) {  
  73.                 setConnected(false);  
  74.                 log('Info: connection closed.');  
  75.                 log(event);  
  76.             };  
  77.         }  
  78.   
  79.         function disconnect() {  
  80.             if (ws != null) {  
  81.                 ws.close();  
  82.                 ws = null;  
  83.             }  
  84.             setConnected(false);  
  85.         }  
  86.   
  87.         function echo() {  
  88.             if (ws != null) {  
  89.                 var message = document.getElementById('message').value;  
  90.                 log('Sent: ' + message);  
  91.                 ws.send(message);  
  92.             } else {  
  93.                 alert('connection not established, please connect.');  
  94.             }  
  95.         }  
  96.   
  97.         function updateUrl(urlPath) {  
  98.             if (urlPath.indexOf('sockjs') != -1) {  
  99.                 url = urlPath;  
  100.                 document.getElementById('sockJsTransportSelect').style.visibility = 'visible';  
  101.             }  
  102.             else {  
  103.               if (window.location.protocol == 'http:') {  
  104.                   url = 'ws://' + window.location.host + urlPath;  
  105.               } else {  
  106.                   url = 'wss://' + window.location.host + urlPath;  
  107.               }  
  108.               document.getElementById('sockJsTransportSelect').style.visibility = 'hidden';  
  109.             }  
  110.         }  
  111.   
  112.         function updateTransport(transport) {  
  113.           transports = (transport == 'all') ?  [] : [transport];  
  114.         }  
  115.           
  116.         function log(message) {  
  117.             var console = document.getElementById('console');  
  118.             var p = document.createElement('p');  
  119.             p.style.wordWrap = 'break-word';  
  120.             p.appendChild(document.createTextNode(message));  
  121.             console.appendChild(p);  
  122.             while (console.childNodes.length > 25) {  
  123.                 console.removeChild(console.firstChild);  
  124.             }  
  125.             console.scrollTop = console.scrollHeight;  
  126.         }  
  127.     </script>  
  128. </head>  
  129. <body>  
  130. <noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets   
  131.     rely on Javascript being enabled. Please enable  
  132.     Javascript and reload this page!</h2></noscript>  
  133. <div>  
  134.     <div id="connect-container">  
  135.         <input id="radio1" type="radio" name="group1" onclick="updateUrl('/mspjapi');">  
  136.             <label for="radio1">W3C WebSocket</label>  
  137.         <br>  
  138.         <input id="radio2" type="radio" name="group1" onclick="updateUrl('/spring-websocket-test/sockjs/echo');">  
  139.             <label for="radio2">SockJS</label>  
  140.         <div id="sockJsTransportSelect" style="visibility:hidden;">  
  141.             <span>SockJS transport:</span>  
  142.             <select onchange="updateTransport(this.value)">  
  143.               <option value="all">all</option>  
  144.               <option value="websocket">websocket</option>  
  145.               <option value="xhr-polling">xhr-polling</option>  
  146.               <option value="jsonp-polling">jsonp-polling</option>  
  147.               <option value="xhr-streaming">xhr-streaming</option>  
  148.               <option value="iframe-eventsource">iframe-eventsource</option>  
  149.               <option value="iframe-htmlfile">iframe-htmlfile</option>  
  150.             </select>  
  151.         </div>  
  152.         <div>  
  153.             <button id="connect" onclick="connect();">Connect</button>  
  154.             <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>  
  155.         </div>  
  156.         <div>  
  157.             <textarea id="message" style="width: 350px">Here is a message!</textarea>  
  158.         </div>  
  159.         <div>  
  160.             <button id="echo" onclick="echo();" disabled="disabled">Echo message</button>  
  161.         </div>  
  162.     </div>  
  163.     <div id="console-container">  
  164.         <div id="console"></div>  
  165.     </div>  
  166. </div>  
  167.   
  168. <a href="echoendpoint.jsp">echoendpoint test</a>  
  169. <a href="websocket2.jsp">echoendpoint test</a>  
  170.   
  171. </body>  
  172. </html>  


最后說(shuō)下幾個(gè)坑點(diǎn):

1、html頁(yè)面訪問(wèn)限制的問(wèn)題的:403 Forbidden

  1. registry.addHandler(systemWebSocketHandler(),"/webSocketServer/sockjs").setAllowedOrigins("*").withSockJS();  
2、web.xml配置需要升級(jí)成3.1

xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

3、web.xml里面所有過(guò)濾器需要加上

<async-supported>true</async-supported>

如:

<servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    搡老熟女老女人一区二区| 久久经典一区二区三区| 高清一区二区三区大伊香蕉| 亚洲精品偷拍视频免费观看| 91人妻人人精品人人爽| 69久久精品亚洲一区二区| 99久久精品午夜一区| 国产欧美日韩在线精品一二区 | 国产成人高清精品尤物| 成人午夜免费观看视频| 亚洲中文字幕剧情在线播放| 午夜福利92在线观看| 女同伦理国产精品久久久| 亚洲中文字幕人妻av| 久久精品久久久精品久久| 精品人妻av区波多野结依| 亚洲中文在线中文字幕91| 国产一级不卡视频在线观看| 激情亚洲内射一区二区三区| 九九热精彩视频在线免费| 一区二区不卡免费观看免费| 肥白女人日韩中文视频| 九九九热视频最新在线| 精品少妇一区二区三区四区| 污污黄黄的成年亚洲毛片| 91在线播放在线播放观看| 日本熟妇熟女久久综合| 91偷拍视频久久精品| 欧美日韩亚洲国产av| 成人区人妻精品一区二区三区| 亚洲欧洲日韩综合二区| 一二区不卡不卡在线观看| 日韩精品免费一区二区三区| 黄色国产自拍在线观看| 亚洲乱妇熟女爽的高潮片| 激情五月综五月综合网| 欧美日韩中国性生活视频| 日韩不卡一区二区视频| 中文字幕五月婷婷免费 | 亚洲中文在线中文字幕91| 人妻一区二区三区在线|