Toggle navigation
技术博客
首页
Java
Spring
DB
服务器
JS
软件
微语
留言
登录
当前位置:
首页
Springboot配置Websocket
Springboot配置Websocket
2023-08-31 10:35:44
字数:4616
157
次
# pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Websocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> # 添加java类 ```java import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; /** * @author huangyuchen * @date 2023/4/2 16:57 * @decrption */ @Component public class MyWebSocketHandler extends TextWebSocketHandler { private static final Set<WebSocketSession> sessions = new CopyOnWriteArraySet<>(); @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { sessions.add(session); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { sessions.remove(session); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { String msg = message.getPayload(); for (WebSocketSession s : sessions) { s.sendMessage(new TextMessage(msg)); } } } ``` ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; /** * @author huangyuchen */ @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new MyWebSocketHandler(), "/socket/fxft").setAllowedOrigins("*"); } } ``` # JSP测试 ```html <!DOCTYPE HTML> <html> <head> <title>WebSocket</title> <script type="text/javascript" src="/js/jquery-3.6.0.min.js"></script> <style> body { width: 100%; height: 100%; } button { padding: 24px; font-size: 36px; margin: 12px; cursor: pointer; font-family: sans-serif; } .text { height: 60px; font-size: 20px; width: 100%; margin: 8px auto; text-align: center; display: none; } #message { color: green; font-size: 24px; padding: 36px; height: 300px; overflow: auto; border: 1px solid #eee; } </style> </head> <body> <div id="message"> </div> </body> <script type="text/javascript"> var websocket = null; var wx_url = "ws://localhost:8080/socket/test"; $('.wxUrl').html("socket监听地址: " + wx_url); //判断当前浏览器是否支持WebSocket if ('WebSocket' in window) { $('.wxUrl').html("socket监听地址: " + wx_url); websocket = new WebSocket(wx_url); } else { alert('Not support websocket') } //连接发生错误的回调方法 websocket.onerror = function () { setMessageInnerHTML("error"); }; //连接成功建立的回调方法 websocket.onopen = function (event) { setMessageInnerHTML("websocket已连接."); } //接收到消息的回调方法 websocket.onmessage = function (event) { setMessageInnerHTML(event.data); } //连接关闭的回调方法 websocket.onclose = function () { setMessageInnerHTML("close"); } //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function () { websocket.close(); } //将消息显示在网页上 function setMessageInnerHTML(innerHTML) { document.getElementById('message').innerHTML += innerHTML + '<br/>'; } //关闭连接 function closeWebSocket() { websocket.close(); } //发送消息 function send(message) { websocket.send(message); } </script> </html> ```
给我留言吧
昵称*
提交评论
留言板
目录
微信
分类
Nginx
系统
文学
生活
历史
地理
学习网站
nginx资源站
runoob学习网
redis学习网
vue中文网
dubbo
返回顶部
×
登录/注册
用户名
昵称
密码
确认密码
提交
没有账号?
马上注册
提交
已有账号?
去登录