赞
踩
目录
2、WebSocketServer这里就是重点了,核心都在这里。
推送延迟。服务端数据发生变更后,长轮询结束,立刻返回响应给客户端。
服务端压力。长轮询的间隔期一般很长,例如 30s、60s,并且服务端 hold 住连接不会消耗太多服务端资源。
推送延迟。
服务端压力。配置一般不会发生变化,频繁的轮询会给服务端造成很大的压力。
推送延迟和服务端压力无法中和。降低轮询的间隔,延迟降低,压力增加;增加轮询的间隔,压力降低,延迟增高
一旦WebSocket连接建立后,后续数据都以帧序列的形式传输。在客户端断开WebSocket连接或Server端中断连接前,不需要客户端和服务端重新发起连接请求。在海量并发及客户端与服务器交互负载流量大的情况下,极大的节省了网络带宽资源的消耗,有明显的性能优势,且客户端发送和接受消息是在同一个持久连接上发起,实现了“真·长链接”,实时性优势明显。
WebSocket有以下特点:
创建 SpringBoot项目,引入 WebSocket依赖,前端这里比较简陋。
<!-- websocket dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.7.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.7.12</version>
</dependency>
启用 WebSocket的支持也是很简单。
/**
* WebSocket配置类。开启WebSocket的支持
*/
@Configuration
public class WebSocketConfig {
/**
* bean注册:会自动扫描带有@ServerEndpoint注解声明的Websocket Endpoint(端点),注册成为Websocket bean。
* 要注意,如果项目使用外置的servlet容器,而不是直接使用springboot内置容器的话,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
因为WebSocket是类似客户端服务端的形式(采用ws协议),那么这里的WebSocketServer其实就相当于一个ws协议的Controller
直接@ServerEndpoint("/imserver/{userId}") 、@Component启用即可,然后在里面实现@OnOpen开启连接,@onClose关闭连接,@onMessage接收消息等方法。
新建一个ConcurrentHashMap用于接收当前userId的WebSocket或者Session信息,方便IM之间对userId进行推送消息。单机版实现到这里就可以。
集群版(多个ws节点)还需要借助 MySQL或者 Redis等进行订阅广播方式处理,改造对应的 sendMessage方法即可。
- /**
- * WebSocket的操作类
- */
- @Component
- @Slf4j
- /**
- * html页面与之关联的接口
- * var reqUrl = "http://localhost:8081/websocket/" + cid;
- * socket = new WebSocket(reqUrl.replace("http", "ws"));
- */
- @ServerEndpoint("/websocket/{sid}")
- public class WebSocketServer {
-
- /**
- * 静态变量,用来记录当前在线连接数,线程安全的类。
- */
- private static AtomicInteger onlineSessionClientCount = new AtomicInteger(0);
-
- /**
- * 存放所有在线的客户端
- */
- private static Map<String, Session> onlineSessionClientMap = new ConcurrentHashMap<>();
-
- /**
- * 连接sid和连接会话
- */
- private String sid;
- private Session session;
-
- /**
- * 连接建立成功调用的方法。由前端<code>new WebSocket</code>触发
- *
- * @param sid 每次页面建立连接时传入到服务端的id,比如用户id等。可以自定义。
- * @param session 与某个客户端的连接会话,需要通过它来给客户端发送消息
- */
- @OnOpen
- public void onOpen(@PathParam("sid") String sid, Session session) {
- /**
- * session.getId():当前session会话会自动生成一个id,从0开始累加的。
- */
- log.info("连接建立中 ==> session_id = {}, sid = {}", session.getId(), sid);
- //加入 Map中。将页面的sid和session绑定或者session.getId()与session
- //onlineSessionIdClientMap.put(session.getId(), session);
- onlineSessionClientMap.put(sid, session);
-
- //在线数加1
- onlineSessionClientCount.incrementAndGet();
- this.sid = sid;
- this.session = session;
- sendToOne(sid, "连接成功");
- log.info("连接建立成功,当前在线数为:{} ==> 开始监听新连接:session_id = {}, sid = {},。", onlineSessionClientCount, session.getId(), sid);
- }
-
- /**
- * 连接关闭调用的方法。由前端<code>socket.close()</code>触发
- *
- * @param sid
- * @param session
- */
- @OnClose
- public void onClose(@PathParam("sid") String sid, Session session) {
- //onlineSessionIdClientMap.remove(session.getId());
- // 从 Map中移除
- onlineSessionClientMap.remove(sid);
-
- //在线数减1
- onlineSessionClientCount.decrementAndGet();
- log.info("连接关闭成功,当前在线数为:{} ==> 关闭该连接信息:session_id = {}, sid = {},。", onlineSessionClientCount, session.getId(), sid);
- }
-
- /**
- * 收到客户端消息后调用的方法。由前端<code>socket.send</code>触发
- * * 当服务端执行toSession.getAsyncRemote().sendText(xxx)后,前端的socket.onmessage得到监听。
- *
- * @param message
- * @param session
- */
- @OnMessage
- public void onMessage(String message, Session session) {
- /**
- * html界面传递来得数据格式,可以自定义.
- * {"sid":"user-1","message":"hello websocket"}
- */
- JSONObject jsonObject = JSON.parseObject(message);
- String toSid = jsonObject.getString("sid");
- String msg = jsonObject.getString("message");
- log.info("服务端收到客户端消息 ==> fromSid = {}, toSid = {}, message = {}", sid, toSid, message);
-
- /**
- * 模拟约定:如果未指定sid信息,则群发,否则就单独发送
- */
- if (toSid == null || toSid == "" || "".equalsIgnoreCase(toSid)) {
- sendToAll(msg);
- } else {
- sendToOne(toSid, msg);
- }
- }
-
- /**
- * 发生错误调用的方法
- *
- * @param session
- * @param error
- */
- @OnError
- public void onError(Session session, Throwable error) {
- log.error("WebSocket发生错误,错误信息为:" + error.getMessage());
- error.printStackTrace();
- }
-
- /**
- * 群发消息
- *
- * @param message 消息
- */
- private void sendToAll(String message) {
- // 遍历在线map集合
- onlineSessionClientMap.forEach((onlineSid, toSession) -> {
- // 排除掉自己
- if (!sid.equalsIgnoreCase(onlineSid)) {
- log.info("服务端给客户端群发消息 ==> sid = {}, toSid = {}, message = {}", sid, onlineSid, message);
- toSession.getAsyncRemote().sendText(message);
- }
- });
- }
-
- /**
- * 指定发送消息
- *
- * @param toSid
- * @param message
- */
- private void sendToOne(String toSid, String message) {
- // 通过sid查询map中是否存在
- Session toSession = onlineSessionClientMap.get(toSid);
- if (toSession == null) {
- log.error("服务端给客户端发送消息 ==> toSid = {} 不存在, message = {}", toSid, message);
- return;
- }
- // 异步发送
- log.info("服务端给客户端发送消息 ==> toSid = {}, message = {}", toSid, message);
- toSession.getAsyncRemote().sendText(message);
- /*
- // 同步发送
- try {
- toSession.getBasicRemote().sendText(message);
- } catch (IOException e) {
- log.error("发送消息失败,WebSocket IO异常");
- e.printStackTrace();
- }*/
- }
-
- }

- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>666666</title>
- </head>
- <body>
- 传递来的数据值cid:
- <input type="text" th:value="${cid}" id="cid"/>
- <p>【toUserId】:
- <div><input id="toUserId" name="toUserId" type="text" value="user-1"></div>
- <p>【toUserId】:
- <div><input id="contentText" name="contentText" type="text" value="hello websocket"></div>
- <p>【操作】:
- <div>
- <button type="button" onclick="sendMessage()">发送消息</button>
- </div>
- </body>
-
- <script type="text/javascript">
- var socket;
- if (typeof (WebSocket) == "undefined") {
- console.log("您的浏览器不支持WebSocket");
- } else {
- console.log("您的浏览器支持WebSocket");
- //实现化WebSocket对象,指定要连接的服务器地址与端口 建立连接
-
- var cid = document.getElementById("cid").value;
- console.log("cid-->" + cid);
- var reqUrl = "http://localhost:8081/websocket/" + cid;
- socket = new WebSocket(reqUrl.replace("http", "ws"));
- //打开事件
- socket.onopen = function () {
- console.log("Socket 已打开");
- //socket.send("这是来自客户端的消息" + location.href + new Date());
- };
- //获得消息事件
- socket.onmessage = function (msg) {
- console.log("onmessage--" + msg.data);
- //发现消息进入 开始处理前端触发逻辑
- };
- //关闭事件
- socket.onclose = function () {
- console.log("Socket已关闭");
- };
- //发生了错误事件
- socket.onerror = function () {
- alert("Socket发生了错误");
- //此时可以尝试刷新页面
- }
- //离开页面时,关闭socket
- //jquery1.8中已经被废弃,3.0中已经移除
- // $(window).unload(function(){
- // socket.close();
- //});
- }
-
- function sendMessage() {
- if (typeof (WebSocket) == "undefined") {
- console.log("您的浏览器不支持WebSocket");
- } else {
- // console.log("您的浏览器支持WebSocket");
- var toUserId = document.getElementById('toUserId').value;
- var contentText = document.getElementById('contentText').value;
- var msg = '{"sid":"' + toUserId + '","message":"' + contentText + '"}';
- console.log(msg);
- socket.send(msg);
- }
- }
-
- </script>
- </html>
-

参考:
1.websocket和http区别_cwxcc的博客-CSDN博客
2. SpringBoot 集成WebSocket详解_springboot集成websocket_Charge8的博客-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。