# webSocket_demo3 **Repository Path**: sunjinchao/webSocket_demo3 ## Basic Information - **Project Name**: webSocket_demo3 - **Description**: 基于springMVC的webSocket 主要原因是为了运行在老版本的tomcat上,(备忘,以后肯定没用) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-11-18 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 基于springMVC的一个项目,由于使用了非常老版本的tomcat7,对websocket的支持无法使用@ServerEndpoint注解,故采用继承 WebSocketServlet类实现 websocket 项目结构: ![](https://gitee.com/sunjinchao/cloudfile_C01/raw/master/img/20191118214010.png) MyMsgInbound类 ``` package com.ziytek.eps; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.WsOutbound; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; /** * */ public class MyMsgInbound extends MessageInbound { protected String clientId; public MyMsgInbound(String clientId) { this.clientId = clientId; } @Override protected void onClose(int status){ System.out.println("连接断开了"); super.onClose(status); } @Override protected void onOpen(WsOutbound outbound){ System.out.println("有人连接过来了"); super.onOpen(outbound); } @Override protected void onBinaryMessage(ByteBuffer arg0) throws IOException { // 二进制消息 } @Override protected void onTextMessage(CharBuffer message) throws IOException { // 文本消息 System.out.println("onTextMessage:"+ message.toString()); //将websocket传过来的值返回回去 WsOutbound outbound=this.getWsOutbound(); outbound.writeTextMessage(message); outbound.flush(); } public String getClientId() { return clientId; } public void setClientId(String clientId) { this.clientId = clientId; } } ``` MyWebsocket类 ``` package com.ziytek.eps; import java.io.IOException; import java.nio.CharBuffer; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.catalina.websocket.StreamInbound; import org.apache.catalina.websocket.WebSocketServlet; import org.apache.catalina.websocket.WsOutbound; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; /** * */ public class MyWebsocket extends WebSocketServlet { private static Map clients = new ConcurrentHashMap(); public MyWebsocket(){ System.out.println("sddddddddddddddd"); } @Override public void init() throws ServletException { //todo 在这里初始化你要注入进来spring的bean System.out.println("始化"); super.init(); } @Override protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { System.out.println("初始化。。。。。"); String clientId = request.getParameter("clientId"); MyMsgInbound myMsgInbound = new MyMsgInbound(clientId); clients.put(clientId, myMsgInbound); return myMsgInbound; } /** * 发送消息给某个客户端 * * @param message * @param To * @throws IOException */ public static void sendMessageTo(String message, String To) throws IOException { for (MyMsgInbound item : clients.values()) { if (item.clientId.equals(To)) { WsOutbound outbound = item.getWsOutbound(); outbound.writeTextMessage(CharBuffer.wrap(message.toCharArray())); outbound.flush(); } } } /** * 发送消息给某些客户端 * * @param message * @param To * @throws IOException */ public static void sendMessageToSomeone(String message, String To) throws IOException { for (MyMsgInbound item : clients.values()) { if (item.clientId.equals(To)) { WsOutbound outbound = item.getWsOutbound(); outbound.writeTextMessage(CharBuffer.wrap(message.toCharArray())); outbound.flush(); } } } /** * 发送消息给所有客户端 * * @param message * @throws IOException */ public static void sendMessageAll(String message) throws IOException { for (MyMsgInbound item : clients.values()) { WsOutbound outbound = item.getWsOutbound(); outbound.writeTextMessage(CharBuffer.wrap(message.toCharArray())); outbound.flush(); } } } ``` web.xml ``` myWebsocket com.ziytek.eps.MyWebsocket myWebsocket /myWebsocket.do ``` index.html ``` Insert title here 服务器返回的信息: 浏览器发送的信息: ``` ![](https://gitee.com/sunjinchao/cloudfile_C01/raw/master/img/20191118215403.png) 效果 ![](https://gitee.com/sunjinchao/cloudfile_C01/raw/master/img/20191118214352.png) 仓库地址:https://gitee.com/sunjinchao/webSocket_demo3.git ### 参考