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

上海网站建设公司服务怎么做农化网站建设

上海网站建设公司服务怎么做,农化网站建设,无锡seo网站推广费用,网络维护服务合同模板WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单#xff0c;允许服务端主动向客户端推送数据。在WebSocket API中#xff0c;浏览器和服务器只需要完成一次握手#xff0c;两者之间就直接可以创建持久性的连接…WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单允许服务端主动向客户端推送数据。在WebSocket API中浏览器和服务器只需要完成一次握手两者之间就直接可以创建持久性的连接并进行双向数据传输。 1.添加依赖包 !--webSocket-- dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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 CopyOnWriteArraySetWebSocketServer webSockets new CopyOnWriteArraySet();/*** 用来存在线连接用户信息*/private static ConcurrentHashMapString, Session sessionPool new ConcurrentHashMapString, 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 headmeta charsetutf-8titleWebsocket客户端/title /head script srchttps://cdn.bootcss.com/jquery/3.3.1/jquery.js/script scriptvar 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 bodydiv客户端身份标识IDinput idclientId typetext value10button stylecolor: cornflowerbluea onclickopenSocket()开启WebSocket连接/a/button /div brbr div客户端向服务器发送的内容input idtargetId typetext value20input idcontentText typetext valuehello WebSocketbutton stylecolor: cornflowerbluea onclicksendMessage()发送消息/a/button /div/body /html5.模拟服务端发送消息 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 CopyOnWriteArraySetWebSocketServer webSockets new CopyOnWriteArraySet();/*** 用来存在线连接用户信息*/private static ConcurrentHashMapString, Session sessionPool new ConcurrentHashMapString, 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?messagehello 发送点对点消息http://localhost:8081/api//websocket/sendSinglePointMessage?messagehellotargetId10
http://www.dnsts.com.cn/news/120977.html

相关文章:

  • 南宁网站建设q479185700棒网站会员充值接口怎么做的
  • 可以做区位分析的网站iis创建网站
  • 品牌网站建设公司哪好哪里可以建设网站
  • 检查色盲效果网站网站icp 备案查询
  • 西安市做网站的网站建设企业响应式网站模板
  • 梅州专业网站建设教程北京 网站设计公司
  • 响应式 网站 开发制作网站域名需要多少钱
  • 国际物流网站数字中国建设峰会 官方网站
  • 贵州网站建设hsyunsowordpress来看看爆款
  • 岳阳网站设计为什么网站要改版
  • 帝国网站模板建设视频免费开源企业网站程序
  • 怎么制作网站导航页玉林seo
  • 电商网站开发设计方法旅游网站开发难吗
  • 自己如何做公司网站深圳建设网站开发
  • 东莞网站维护企业高端网站建设美工
  • 充值网站架设微信自动加好友软件
  • 网站的整体规划怎么写网页设计的实验报告
  • 模板云网站建设网站的结构是什么样的
  • 公司建立网站用于业务以企业介绍为主做外贸网站好吗
  • 做的好的奥运会网站外国设计师素材网站
  • 做语文高考题网站wordpress两个域名访问
  • 汕头做网站优化哪家好网站建设需求 百度文库
  • 武威百度做网站多少钱优购网
  • 视频网站直播怎么做的北京网站建设工作
  • 怎么加php网站登陆源码遵义网址
  • 民兵信息化网站建设wordpress主题手机端
  • 深圳网站建设哪个好保健品网站建设案例
  • 百度指数 网站天猫交易网站
  • 服务器做视频网站吗建筑论坛网站
  • 编程和做网站有关系吗上海中学图片