使用springboot快速搭建测试案例
<!-- 导入websocket --><dependency> <groupID>org.springframework.boot</groupID> <artifactID>spring-boot-starter-websocket</artifactID> <version>1.3.5.RELEASE</version></dependency>
将bean注入
@Configurationpublic class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }}
创建一个处理请求和发送数据的类
@ServerEndpoint(value = "/websocket")@Componentpublic class MyWebSocket { //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 private static int onlineCount = 0; //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 private static copyOnWriteArraySet<MyWebSocket> webSocketSet = new copyOnWriteArraySet<MyWebSocket>(); //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; /** * 连接建立成功调用的方法 */ @Onopen public voID onopen(Session session) { this.session = session; webSocketSet.add(this); //加入set中 addOnlineCount(); //在线数加1 System.out.println("有新连接加入!当前在线人数为" + getonlineCount()); try { sendMessage("连接异常,请检查网络"); } catch (IOException e) { System.out.println("IO异常"); } } /** * 连接关闭调用的方法 */ @OnClose public voID onClose() { webSocketSet.remove(this); //从set中删除 subOnlineCount(); //在线数减1 System.out.println("有一连接关闭!当前在线人数为" + getonlineCount()); } /** * 收到客户端消息后调用的方法 * * @param message 客户端发送过来的消息 */ @OnMessage public voID onMessage(String message,Session session) { System.out.println("来自客户端的消息:" + message); //群发消息 for (MyWebSocket item : webSocketSet) { try { item.sendMessage(message); } catch (IOException e) { e.printstacktrace(); } } } /** * 发生错误时调用 * * @OnError */ public voID onError(Session session,Throwable error) { System.out.println("发生错误"); error.printstacktrace(); } //发送消息 public voID sendMessage(String message) throws IOException { this.session.getBasicRemote().sendText(message); } /** * 群发自定义消息 */ public static voID sendInfo(String message) throws IOException { for (MyWebSocket item : webSocketSet) { try { item.sendMessage(message); } catch (IOException e) { continue; } } } //获取在线人数 public static synchronized int getonlineCount() { return onlineCount; } //加入新人 public static synchronized voID addOnlineCount() { MyWebSocket.onlineCount++; } //退出 public static synchronized voID subOnlineCount() { MyWebSocket.onlineCount--; }}
客户端代码
<!DOCTYPE HTML><HTML><head> <Title>WebSocket实现</Title></head><body> Welcome<br/><input ID="text" type="text"/> <button onclick="send()">发送消息</button> <hr/> <button onclick="closeWebSocket()">关闭WebSocket连接</button> <hr/> <div ID="message"></div></body><script type="text/JavaScript"> var websocket = null; //判断当前浏览器是否支持WebSocket if (‘WebSocket‘ in window) { websocket = new WebSocket("ws://localhost:8080/demo/websocket"); } else { alert(‘当前浏览器 Not support websocket‘) } //连接发生错误的回调方法 websocket.onerror = function () { setMessageINNERHTML("WebSocket连接发生错误"); }; //连接成功建立的回调方法 websocket.onopen = function () { setMessageINNERHTML("WebSocket连接成功"); } //接收到消息的回调方法 websocket.onmessage = function (event) { setMessageINNERHTML(event.data); } //连接关闭的回调方法 websocket.onclose = function () { setMessageINNERHTML("WebSocket连接关闭"); } //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function () { closeWebSocket(); } //将消息显示在网页上 function setMessageINNERHTML(INNERHTML) { document.getElementByID(‘message‘).INNERHTML += INNERHTML + ‘<br/>‘; } //关闭WebSocket连接 function closeWebSocket() { websocket.close(); } //发送消息 function send() { var message = document.getElementByID(‘text‘).value; websocket.send(message); }</script></HTML>总结
以上是内存溢出为你收集整理的WebSocket技术demo全部内容,希望文章能够帮你解决WebSocket技术demo所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)