当前位置: 首页 > news >正文

关于咖啡厅网站建设的论文网站美工做图

关于咖啡厅网站建设的论文,网站美工做图,vscode的网站开发配置,网站开发及app开发报价WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接…

WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。

1.添加依赖包

<!--webSocket-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

2.添加WebSocket配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configuration
public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}}

3.编写web服务端

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;@Component
@ServerEndpoint("/websocket/{clientId}")
@Slf4j
public class WebSocketServer {/*** 客户端的连接会话,需要通过它来给客户端发送数据*/private Session session;/*** 客户端id*/private String clientId;/*** 用来存放每个客户端对应的MyWebSocket对象*/private static CopyOnWriteArraySet<WebSocketServer> webSockets = new CopyOnWriteArraySet<>();/*** 用来存在线连接用户信息*/private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();/*** 链接成功调用的方法*/@OnOpenpublic void onOpen(Session session, @PathParam(value = "clientId") String clientId) {try {this.session = session;this.clientId = clientId;webSockets.add(this);sessionPool.put(clientId, session);log.info("【websocket消息】有新的连接 clientId:{},总数为:{}", clientId, webSockets.size());} catch (Exception e) {log.info("【websocket消息】有新的连接构建失败 clientId:{},总数为:{},error:", clientId, webSockets.size(), e);}}/*** 链接关闭调用的方法*/@OnClosepublic void onClose() {try {webSockets.remove(this);sessionPool.remove(this.clientId);log.info("【websocket消息】连接断开 clientId:{},总数为:{}", this.clientId, webSockets.size());} catch (Exception e) {log.info("【websocket消息】连接断开失败 clientId:{},总数为:{},error:", clientId, webSockets.size(), e);}}/*** 收到客户端消息后调用的方法*/@OnMessagepublic void onMessage(String message) {log.info("【websocket消息】收到客户端消息:{}", message);}/*** 发送错误时的处理** @param session* @param error*/@OnErrorpublic void onError(Session session, Throwable error) {log.error("【websocket消息】发生错误,原因:", error);}/*** 此为广播消息*/public void sendBroadcastMessage(String message) {log.info("【websocket消息】广播消息:{}", message);for (WebSocketServer webSocket : webSockets) {try {if (webSocket.session.isOpen()) {webSocket.session.getAsyncRemote().sendText(message);}} catch (Exception e) {log.error("【websocket消息】广播消息异常,消息:{},error:", message, e);}}}/*** 此为单点消息*/public void sendSinglePointMessage(String targetId, String message) {Session session = sessionPool.get(targetId);if (session != null && session.isOpen()) {try {session.getAsyncRemote().sendText(message);log.info("【websocket消息】单点消息,targetId:{},消息:{}", targetId, message);} catch (Exception e) {log.error("【websocket消息】单点消息异常,targetId:{},消息:{},error:", targetId, message, e);}}}}

4.html测试页面

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Websocket客户端</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>var socket;function openSocket() {const socketUrl = "ws://localhost:8081/websocket/" + $("#clientId").val();console.log(socketUrl);if (socket != null) {socket.close();socket = null;}socket = new WebSocket(socketUrl);// 开启WebSocket连接socket.onopen = function () {console.log("websocket已开启");};// 获得消息事件socket.onmessage = function (msg) {console.log(msg.data);};// 关闭事件socket.onclose = function () {console.log("websocket已关闭");};// 发生了错误事件socket.onerror = function () {console.log("websocket发生了错误");}}function sendMessage() {socket.send('{"targetId":"' + $("#targetId").val() + '","contentText":"' + $("#contentText").val() + '"}');console.log('{"targetId":"' + $("#targetId").val() + '","contentText":"' + $("#contentText").val() + '"}');}
</script>
<body><div>客户端身份标识ID<input id="clientId" type="text" value="10"><button style="color: cornflowerblue"><a onclick="openSocket()">开启WebSocket连接</a></button>
</div>
<br><br>
<div>客户端向服务器发送的内容<input id="targetId" type="text" value="20"><input id="contentText" type="text" value="hello WebSocket"><button style="color: cornflowerblue"><a onclick="sendMessage()">发送消息</a></button>
</div></body>
</html>

5.模拟服务端发送消息

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;@Component
@ServerEndpoint("/websocket/{clientId}")
@Slf4j
public class WebSocketServer {/*** 客户端的连接会话,需要通过它来给客户端发送数据*/private Session session;/*** 客户端id*/private String clientId;/*** 用来存放每个客户端对应的MyWebSocket对象*/private static CopyOnWriteArraySet<WebSocketServer> webSockets = new CopyOnWriteArraySet<>();/*** 用来存在线连接用户信息*/private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<String, Session>();/*** 链接成功调用的方法*/@OnOpenpublic void onOpen(Session session, @PathParam(value = "clientId") String clientId) {try {this.session = session;this.clientId = clientId;webSockets.add(this);sessionPool.put(clientId, session);log.info("【websocket消息】有新的连接 clientId:{},总数为:{}", clientId, webSockets.size());} catch (Exception e) {log.info("【websocket消息】有新的连接构建失败 clientId:{},总数为:{},error:", clientId, webSockets.size(), e);}}/*** 链接关闭调用的方法*/@OnClosepublic void onClose() {try {webSockets.remove(this);sessionPool.remove(this.clientId);log.info("【websocket消息】连接断开 clientId:{},总数为:{}", this.clientId, webSockets.size());} catch (Exception e) {log.info("【websocket消息】连接断开失败 clientId:{},总数为:{},error:", clientId, webSockets.size(), e);}}/*** 收到客户端消息后调用的方法*/@OnMessagepublic void onMessage(String message) {log.info("【websocket消息】收到客户端消息:{}", message);}/*** 发送错误时的处理** @param session* @param error*/@OnErrorpublic void onError(Session session, Throwable error) {log.error("【websocket消息】发生错误,原因:", error);}/*** 此为广播消息*/public void sendBroadcastMessage(String message) {log.info("【websocket消息】广播消息:{}", message);for (WebSocketServer webSocket : webSockets) {try {if (webSocket.session.isOpen()) {webSocket.session.getAsyncRemote().sendText(message);}} catch (Exception e) {log.error("【websocket消息】广播消息异常,消息:{},error:", message, e);}}}/*** 此为单点消息*/public void sendSinglePointMessage(String targetId, String message) {Session session = sessionPool.get(targetId);if (session != null && session.isOpen()) {try {session.getAsyncRemote().sendText(message);log.info("【websocket消息】单点消息,targetId:{},消息:{}", targetId, message);} catch (Exception e) {log.error("【websocket消息】单点消息异常,targetId:{},消息:{},error:", targetId, message, e);}}}}

发送广播消息:http://localhost:8081/api//websocket/sendBroadcastMessage?message=hello

发送点对点消息:http://localhost:8081/api//websocket/sendSinglePointMessage?message=hello&targetId=10

http://www.yayakq.cn/news/133540/

相关文章:

  • wordpress外贸建站怎么加左侧边栏成都网站建设博客
  • 大型银行网站建设id注册网站
  • 企业网站推广宣传方案江西省城乡建设厅建设网站
  • 济南专业网站制作公司北京贸易公司网站制作
  • 坪山手机网站建设微盟小程序是什么
  • 天津网站建设制作品牌公司福田公司董事长
  • 河南网站开发培训服装网站建设都有哪些
  • 廊坊网站建设电话做网站单位
  • 重庆市建设厅网站首页友情链接例子
  • 建立了网站后如何发贴网站是否含有seo收录功能
  • 石岩网站建设梅江区建设局网站
  • 怎么制作网站程序dw做网站后台
  • 艺术品网站模板网站建设 套格式
  • 上海网站快速排名提升房屋装修简约风格
  • 谭谭心怎么建设网站游戏开发制作培训
  • 网站备案状态查询网站优化的关键词
  • 做网站需要服务器吗做网站单线程CPU和多线程cpu
  • 怎么在云服务器上建设网站网页工具栏怎么显示出来
  • 深圳做网站哪家公司最好企业信息系统规划的含义
  • 南阳医疗网站制作价格如何做品牌网站
  • 网站的关键词多少合适六安市城市建设档案馆网站
  • 广西宏泰成建设集团网站58同城机械加工订单
  • 物联网就业方向江苏网站seo营销模板
  • 网站建设微信公众号小程序app淄博网站建设0533cl
  • html个人网站完整代码成都建设官方网站
  • 开发一个网站需要多少时间东莞市建设局官网
  • 贵阳网站建设哪家便宜搜索引擎营销方式
  • 企业网站建设 调研北京网络网站建设公司
  • 做网站聊城云网站建设 优帮云
  • 怎样查询网站空间织梦网站字体