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

家电网站建设费用西安市专业网站建设

家电网站建设费用,西安市专业网站建设,搜狗网站收录,大连做网站哪里好1、webSocket实现代码 Component public class WebSocketStompConfig {//这个bean的注册,用于扫描带有ServerEndpoint的注解成为websocket// ,如果你使用外置的tomcat就不需要该配置文件Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpoi…

1、webSocket实现代码

@Component
public class WebSocketStompConfig {//这个bean的注册,用于扫描带有@ServerEndpoint的注解成为websocket// ,如果你使用外置的tomcat就不需要该配置文件@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}
}

@Component
@ServerEndpoint(value = "/webSocket")
@Slf4j
public class WebSocket {private static int onlineCount = 0;private static ConcurrentHashMap<String, Set<Session>> webSocketMap  = new ConcurrentHashMap<>();private Session session;@OnOpenpublic void onOpen(Session session) {this.session = session;// 获取URL中的参数Map<String, List<String>> params = session.getRequestParameterMap();List<String> funcTypes = params.get("funcType");if (!funcTypes.isEmpty()) {// 取出funcType参数的值String funcType = funcTypes.get(0);if(webSocketMap.containsKey(funcType)){webSocketMap.get(funcType).add(session);}else{Set<Session> sessionSet = new HashSet<>();sessionSet.add(session);webSocketMap.put(funcType,sessionSet);}}System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());}@OnClosepublic void onClose(){webSocketMap=new ConcurrentHashMap<>();log.info("有一连接关闭!当前在线人数为" + getOnlineCount());}@OnMessagepublic void onMessage(String message, Session session) {System.out.println("来自客户端的消息:" + message);}@OnErrorpublic void onError(Session session, Throwable error) {System.out.println("发生错误");error.printStackTrace();}public static void sendMessage(Session session, String message) throws IOException {session.getBasicRemote().sendText(message);}public static synchronized int getOnlineCount() {return onlineCount;}public static synchronized void addOnlineCount() {WebSocket.onlineCount++;}public static synchronized void subOnlineCount() {WebSocket.onlineCount--;}public static void setOnlineCount(int onlineCount) {WebSocket.onlineCount = onlineCount;}public static ConcurrentHashMap<String, Set<Session>> getWebSocketMap() {return webSocketMap;}public static void setWebSocketMap(ConcurrentHashMap<String, Set<Session>> webSocketMap) {WebSocket.webSocketMap = webSocketMap;}public Session getSession() {return session;}public void setSession(Session session) {this.session = session;}/*** 发送自定义消息* */public static void sendInfo(String message,String funcType) throws Exception {if(webSocketMap.containsKey(funcType)){Set<Session> sessionSet = webSocketMap.get(funcType);if(sessionSet!=null){for (Session session : sessionSet) {if(session.getBasicRemote()!=null){session.getBasicRemote().sendText(message);}}}}else{log.error("订阅类型:"+funcType+",不存在!");throw new Exception("连接已关闭,请刷新页面后重试");}}}

二、java代码调用,往websocker赋数据

WebSocket.sendInfo(JSON.toJSONString("sdfasd232"),"3");

三、测试是否连接成功以及推送信息

四、前端代码处理

/*** 初始化websocket连接*/
function initWebSocket() {var websocket = null;if('WebSocket' in window) {websocket = new WebSocket("ws://127.0.0.1:8080/webSocket?funcType=3" );} else {alert("该浏览器不支持websocket!");}websocket.onopen = function(event) {console.log("建立连接");websocket.send('Hello WebSockets!');}websocket.onclose = function(event) {console.log('连接关闭')reconnect(); //尝试重连websocket}//建立通信后,监听到后端的数据传递websocket.onmessage = function(event) {let data = JSON.parse(event.data);//业务处理....if(data.step == 1){alert(data.msg);}}websocket.onerror = function() {// notify.warn("websocket通信发生错误!");// initWebSocket()}window.onbeforeunload = function() {websocket.close();}
// 重连
function reconnect() {console.log("正在重连");// 进行重连setTimeout(function () {initWebSocket();}, 1000);
}

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

相关文章:

  • 电子商务网站建设目标及利益分析做旅游网站需要什么
  • 怎么盗用网站做外语网站
  • 微信小程序与微网站医院网站制作设计
  • 单页网站网址策划公司
  • 石家庄做网站seo做网站优化推广
  • wordpress网站换空间电力建设科学技术进步申报网站
  • html网页模板网站做旅行同业的网站
  • 自己做游戏app的网站吗电视剧百度风云榜
  • 网站内容营销开发一套电商网站多少钱
  • 天津整站域名解析查询
  • 专门做衣服特卖的网站有哪些山东省建设职业教育集团网站
  • 打折网站建设教程下载万网icp网站备案专题
  • 网站建设的技巧有哪些方面wordpress生成app
  • 哪些网站可以做代理商东莞搭建网站要多少钱
  • 在线制作钓鱼网站源码wordpress 漏洞列表
  • win7可以做网站吗wordpress数据库配置文件路径
  • 成都网站建设公司有哪几家手机怎么样做网站
  • 目前做公司网站有没有用做网站有弹窗叫什么
  • 自助seo网站建设网站开发 报刊
  • 成都高端网站制作公司湖南省建设监理协会官网
  • 网站建设要学多少课程wordpress隐私页
  • 房产网站怎么建设东营网站开发招聘
  • 优惠券网站cms建设ppt模板做的好的网站有
  • 青岛建立网站电话建立自己的网站需要多少钱
  • 强的网站建设公域名被墙查询检测
  • 建工厂网站的公司成都网站建设好的公司
  • 关于建设工程招标类的公共网站北京广告公司聚集地
  • 网站建设防火墙级别要求网络广告策划流程有哪些
  • 音乐网站开发的项目背景天津app开发公司
  • 网站怎么做显得简洁美观做网站哪家强