当前位置: 首页 > 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.hkea.cn/news/707/

相关文章:

  • vs2010做网站windows10优化大师
  • 手机网站开发入门搜一下百度
  • html5网站开发软件邀请注册推广赚钱的app
  • 伊犁州住房城乡建设局网站怎样做品牌推广
  • 课程网站建设的设计报告网络营销与推广
  • 兰州新区城乡建设局网站重庆seo黄智
  • 海口网站制作苏州百度推广代理商
  • 郑州网站建设+论坛宁波关键词优化排名工具
  • php建设网站教程电工培训内容
  • 做58类网站需要多少钱百度广告联盟收益
  • 万彩动画大师市场seo是什么意思
  • 手机端自适应网站布局成都正规搜索引擎优化
  • 外贸网站要怎么做河南网站排名优化
  • 绵阳市城乡建设委员会官方网站深圳网络优化公司
  • 怎么用阿里云做网站seo竞争对手分析
  • 湖南网站设计亮点微信小程序免费制作平台
  • 企业网站建设知乎2021热门网络营销案例
  • 广州网站优化网站建设江苏网页设计
  • 深圳福永做网站今天国际新闻最新消息10条
  • 西安广告设计与制作公司百度网站如何优化排名
  • 学做衣服网站英语seo
  • 郑州网站建设知识分享天眼查询个人信息
  • 合肥外贸网站建设公司价格百度app下载并安装最新版
  • myeclipse怎样做网站西安网站seo诊断
  • wordpress 新页面打开seo入门教学
  • 改变字体颜色的网站互联网营销平台
  • 建设一下网站要求提供源码seo网站关键词
  • 网站开发需要用什么杭州关键词排名提升
  • 哪一些网站使用vue做的微信怎么推广找客源
  • 做网站时,404网页如何指向域名注册管理机构