一、所需jar
二、创建websocket.jsp(注意此页面链接,需要链接到自己的服务)
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <html><head><title>Web Socket Client</title></head> 8 <body> 9 <script type="text/javascript"> 10 var socket; 11 if (!window.WebSocket) { 12 window.WebSocket = window.MozWebSocket; 13 } 14 // Javascript Websocket Client 15 if (window.WebSocket) { 16 socket = new WebSocket("ws://192.168.1.168:8080/websocketDemo/ws?123123"); 17 socket.onmessage = function(event) { 18 var ta = document.getElementById('responseText'); 19 ta.value = ta.value + ' ' + event.data 20 }; 21 socket.onopen = function(event) { 22 var ta = document.getElementById('responseText'); 23 ta.value = "Web Socket opened!"; 24 }; 25 socket.onclose = function(event) { 26 var ta = document.getElementById('responseText'); 27 ta.value = ta.value + "Web Socket closed"; 28 }; 29 socket.onerror = function(event) { 30 onError(event); 31 }; 32 } else { 33 alert("Your browser does not support Web Socket."); 34 } 35 // Send Websocket data 36 function send(message) { 37 if (!window.WebSocket) { return; } 38 if (socket.readyState == WebSocket.OPEN) { 39 socket.send(message); 40 } else { 41 alert("The socket is not open."); 42 } 43 } 44 </script> 45 <h3>Send :</h3> 46 <form onsubmit="return false;"> 47 <input type="text" name="message" value="Hello World!"/><input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)" /> 48 <h3>Receive :</h3> 49 <textarea id="responseText" style="500px;height:300px;"></textarea> 50 </form> 51 </body> 52 </html>
三、创建服务类WebSocketServer.java
1 package com.sh.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.jsp.tagext.TryCatchFinally; 7 import javax.websocket.OnClose; 8 import javax.websocket.OnError; 9 import javax.websocket.OnMessage; 10 import javax.websocket.OnOpen; 11 import javax.websocket.Session; 12 import javax.websocket.server.ServerEndpoint; 13 14 import org.springframework.asm.commons.TryCatchBlockSorter; 15 16 /** 17 * WebSocket服务 18 * 19 */ 20 @ServerEndpoint("/ws") 21 public class WebSocketServer{ 22 23 private static List<WebSocketServer> wslist = new ArrayList<WebSocketServer>(); 24 25 private Session session; 26 27 public WebSocketServer() { 28 } 29 /** 30 * 连接后执行 31 * @param session 32 */ 33 @OnOpen 34 public void onOpen(Session session) { 35 System.out.println("连接开启"); 36 String str = session.getQueryString();//获取开启后的参数 链接?后面的字符串 37 wslist.add(this); 38 39 this.session = session; 40 } 41 42 /** 43 * 接到消息执行 44 * @param session 45 * @param msg 46 */ 47 @OnMessage 48 public void message(Session session, String msg) { 49 for(WebSocketServer wss : this.wslist) {//可循环对所有人广播,也可单独对一人广播 50 if(wss.session != null && wss.session.isOpen()) { 51 wss.session.getAsyncRemote().sendText(wss.session.getId()+" send msg "+msg); 52 } 53 } 54 } 55 56 57 58 /** 59 * 关闭后执行执行 60 * @param session 61 */ 62 @OnClose 63 public void onClose(Session session) { 64 System.out.println("连接关闭"); 65 if(wslist.contains(this)) { 66 wslist.remove(this); 67 } 68 } 69 /** 70 * 关闭浏览器后执行 71 * @param t 72 */ 73 @OnError 74 public void onError(Throwable t) { 75 if(wslist.contains(this)) { 76 wslist.remove(this); 77 } 78 } 79 80 81 }
四、创建测试方法TestController.java
1 package com.sh.test; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 10 @Controller 11 public class TestController { 12 private void TestController() {} 13 @RequestMapping(value = "/ws", method = RequestMethod.GET) 14 public String ws(HttpServletRequest request, HttpServletResponse response) { 15 return "/websocket"; 16 } 17 18 }
五、使用TomCat7运行项目,即可通讯
http://192.168.1.168:8080/wstest/ws