spring+websocket整合(springMVC+spring+MyBatis即SSM框架和websocket技术的整合)

时间:2025-03-20 11:43:06
  • <!DOCTYPE html>  
  • <html>  
  • <head>  
  •     <title>WebSocket/SockJS Echo Sample (Adapted from Tomcat's echo sample)</title>  
  •     <style type="text/css">  
  •         #connect-container {  
  •             float: left;  
  •             width: 400px  
  •         }  
  •   
  •         #connect-container div {  
  •             padding: 5px;  
  •         }  
  •   
  •         #console-container {  
  •             float: left;  
  •             margin-left: 15px;  
  •             width: 400px;  
  •         }  
  •   
  •         #console {  
  •             border: 1px solid #CCCCCC;  
  •             border-right-color: #999999;  
  •             border-bottom-color: #999999;  
  •             height: 170px;  
  •             overflow-y: scroll;  
  •             padding: 5px;  
  •             width: 100%;  
  •         }  
  •   
  •         #console p {  
  •             padding: 0;  
  •             margin: 0;  
  •         }  
  •     </style>  
  •   
  •     <script src="/sockjs-0."></script>  
  •   
  •     <script type="text/javascript">  
  •         var ws = null;  
  •         var url = null;  
  •         var transports = [];  
  •   
  •         function setConnected(connected) {  
  •             ('connect').disabled = connected;  
  •             ('disconnect').disabled = !connected;  
  •             ('echo').disabled = !connected;  
  •         }  
  •   
  •         function connect() {  
  •             alert("url:"+url);  
  •             if (!url) {  
  •                 alert('Select whether to use W3C WebSocket or SockJS');  
  •                 return;  
  •             }  
  •   
  •             ws = (('sockjs') != -1) ?   
  •                 new SockJS(url, undefined, {protocols_whitelist: transports}) : new WebSocket(url);  
  •   
  •              = function () {  
  •                 setConnected(true);  
  •                 log('Info: connection opened.');  
  •             };  
  •              = function (event) {  
  •                 log('Received: ' + );  
  •             };  
  •              = function (event) {  
  •                 setConnected(false);  
  •                 log('Info: connection closed.');  
  •                 log(event);  
  •             };  
  •         }  
  •   
  •         function disconnect() {  
  •             if (ws != null) {  
  •                 ();  
  •                 ws = null;  
  •             }  
  •             setConnected(false);  
  •         }  
  •   
  •         function echo() {  
  •             if (ws != null) {  
  •                 var message = document.getElementById('message').value;  
  •                 log('Sent: ' + message);  
  •                 (message);  
  •             } else {  
  •                 alert('connection not established, please connect.');  
  •             }  
  •         }  
  •   
  •         function updateUrl(urlPath) {  
  •             if (('sockjs') != -1) {  
  •                 url = urlPath;  
  •                 ('sockJsTransportSelect'). = 'visible';  
  •             }  
  •             else {  
  •               if ( == 'http:') {  
  •                   url = 'ws://' +  + urlPath;  
  •               } else {  
  •                   url = 'wss://' +  + urlPath;  
  •               }  
  •               ('sockJsTransportSelect'). = 'hidden';  
  •             }  
  •         }  
  •   
  •         function updateTransport(transport) {  
  •             alert(transport);  
  •           transports = (transport == 'all') ?  [] : [transport];  
  •         }  
  •           
  •         function log(message) {  
  •             var console = document.getElementById('console');  
  •             var p = document.createElement('p');  
  •              = 'break-word';  
  •             ((message));  
  •             (p);  
  •             while ( > 25) {  
  •                 ();  
  •             }  
  •              = console.scrollHeight;  
  •         }  
  •     </script>  
  • </head>  
  • <body>  
  • <noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets   
  •     rely on Javascript being enabled. Please enable  
  •     Javascript and reload this page!</h2></noscript>  
  • <div>  
  •     <div id="connect-container">  
  •         <input id="radio1" type="radio" name="group1" onclick="updateUrl(''/spring-websocket-uptest/websocket');">  
  •             <label for="radio1">W3C WebSocket</label>  
  •         <br>  
  •         <input id="radio2" type="radio" name="group1" onclick="updateUrl('/spring-websocket-uptest/websocket');">  
  •             <label for="radio2">SockJS</label>  
  •         <div id="sockJsTransportSelect" style="visibility:hidden;">  
  •             <span>SockJS transport:</span>  
  •             <select onchange="updateTransport()">  
  •               <option value="all">all</option>  
  •               <option value="websocket">websocket</option>  
  •               <option value="xhr-polling">xhr-polling</option>  
  •               <option value="jsonp-polling">jsonp-polling</option>  
  •               <option value="xhr-streaming">xhr-streaming</option>  
  •               <option value="iframe-eventsource">iframe-eventsource</option>  
  •               <option value="iframe-htmlfile">iframe-htmlfile</option>  
  •             </select>  
  •         </div>  
  •         <div>  
  •             <button id="connect" onclick="connect();">Connect</button>  
  •             <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>  
  •         </div>  
  •         <div>  
  •             <textarea id="message" style="width: 350px">Here is a message!</textarea>  
  •         </div>  
  •         <div>  
  •             <button id="echo" onclick="echo();" disabled="disabled">Echo message</button>  
  •         </div>  
  •     </div>  
  •     <div id="console-container">  
  •         <div id="console"></div>  
  •     </div>  
  • </div>  
  • </body>  
  • </html>