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

推荐西安知名的集团门户网站建设公司济南网站建设行知keji

推荐西安知名的集团门户网站建设公司,济南网站建设行知keji,免费的虚拟主机空间,福州品牌网站设计一、概述 本文基于spring-boot-starter-websocket简单的完成收发信息功能#xff0c;使用注解形式进行实现。 二、相关配置 spring:2.0.2#xff0c;jdk:1.8.202#xff0c;maven:3.3.9 因为spring和maven有版本匹配的要求#xff0c;请大家注意自己的版本是否匹配 三…一、概述 本文基于spring-boot-starter-websocket简单的完成收发信息功能使用注解形式进行实现。 二、相关配置 spring:2.0.2jdk:1.8.202maven:3.3.9 因为spring和maven有版本匹配的要求请大家注意自己的版本是否匹配 三、项目结构 四、代码 1.启动类 SocketDemoApplication package com.lp.socketdemo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;EnableScheduling SpringBootApplication public class SocketDemoApplication {public static void main(String[] args) {SpringApplication.run(SocketDemoApplication.class, args);}}2. 监听类 WsServerEndpoint package com.lp.socketdemo.java;import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;/*** 监听websocket地址myWs*/ ServerEndpoint(/myWs) Component public class WsServerEndpoint {static MapString,Session map new ConcurrentHashMap();OnOpenpublic void onOpen(Session session) {map.put(session.getId(),session);System.out.println(webSockte is open: session.getId());sendAll(session,上线);}OnMessagepublic String onMessage(String message) {System.out.println(收到一条信息message);return 已收到你的消息message;}OnClosepublic void onClose(Session session) {System.out.println(webSockte is closesession.getId());sendAllEceptMe(session,下线);map.remove(session.getId());}Scheduled(fixedRate 60000)public void sendMsg() {for (String key: map.keySet()) {try {map.get(key).getBasicRemote().sendText(心跳);} catch (IOException e) {e.printStackTrace();}}}public void sendAll(Session session,String msg) {for (String key: map.keySet()) {try {map.get(key).getBasicRemote().sendText(session.getId():msg);} catch (IOException e) {e.printStackTrace();}}}public void sendAllEceptMe(Session session,String msg) {for (String key: map.keySet()) {try {if(!session.getId().equals(key)) {map.get(key).getBasicRemote().sendText(session.getId():msg);}} catch (IOException e) {e.printStackTrace();}}} }3.配置类 WebSocketConfig package com.lp.socketdemo.java;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter;/*** 配置类注入bean*/ Configuration public class WebSocketConfig {Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();} }4.前段 websocketTest.html !DOCTYPE html html headmeta http-equivContent-Type contenttext/html; charsetUTF-8titlewebsocket调试页面/title /head script typetext/javascript srcjquery.min.js/script body div stylefloat: left; padding: 20pxstronglocation:/strong br /input typetext idserverUrl size35 value / br /button onclickconnect()connect/buttonbutton onclickwsclose()disConnect/buttonbr / strongmessage:/strong br / input idtxtMsg typetext size50 /br /button onclicksendEvent()发送/button /divdiv stylefloat: left; margin-left: 20px; padding-left: 20px; width: 350px; border-left: solid 1px #cccccc; strong消息记录/strongdiv styleborder: solid 1px #999999;border-top-color: #CCCCCC;border-left-color: #CCCCCC; padding: 5px;width: 100%;height: 172px;overflow-y: scroll; idecho-log/divbutton onclickclearLog() styleposition: relative; top: 3px;清除消息/button /div/div /body !-- 下面是h5原生websocket js写法 -- script typetext/javascriptlet output ;let websocket;function connect(){ //初始化连接output document.getElementById(echo-log)let inputNode document.getElementById(serverUrl);let wsUri inputNode.value;try{websocket new WebSocket(wsUri);}catch(ex){console.log(ex)alert(对不起websocket连接异常)}connecting();window.addEventListener(load, connecting, false);}function connecting(){websocket.onopen function(evt) { onOpen(evt) };websocket.onclose function(evt) { onClose(evt) };websocket.onmessage function(evt) { onMessage(evt) };websocket.onerror function(evt) { onError(evt) };}function sendEvent(){let msg document.getElementById(txtMsg).valuedoSend(msg);}//连接上事件function onOpen(evt){writeToScreen(CONNECTED);doSend(WebSocket 已经连接成功);}//关闭事件function onClose(evt){writeToScreen(连接已经断开);}//后端推送事件function onMessage(evt){writeToScreen(span stylecolor: blue;服务器: evt.data/span);}function onError(evt){writeToScreen(span stylecolor: red;异常信息:/span evt.data);}function doSend(message){writeToScreen(客户端A: message);websocket.send(message);}//清除div的内容function clearLog(){output.innerHTML ;}//浏览器主动断开连接function wsclose(){websocket.close();}function writeToScreen(message){let pre document.createElement(p);pre.innerHTML message;output.appendChild(pre);} /script /html五、效果展示 六、引用 课程介绍_WebSocket入门与案例实战-慕课网 最全面的SpringMVC教程六——WebSocket_springmvc websocket_小新要变强的博客-CSDN博客
http://www.pierceye.com/news/990849/

相关文章:

  • 网站名称没有排名上海工商网查询企业章程
  • 网站建设方案报价费用明细价格免费开店的电商平台
  • 济南网络建站模板用c 做的网站怎么打开
  • 网站建设培训课程好人一生平安网站哪个好
  • seo怎么做网站的tdk网站优化的核心不包括
  • 如何做一份网站的数据分析网站营销案例
  • 中小企业网站建设公司个人微信号做网站行吗
  • 网站无法连接服务器哪些国家网站无须备案
  • 重庆做网站设计培训机构排名全国十大教育机构排名
  • 做网站建设销售网络营销推广技巧
  • 南宁网站制作定制北京网站seo服务
  • 门户网站网页设计规范willin kan 让你的wordpress飞起来
  • 建设银行广州招聘网站wordpress dz
  • 如何介绍自己做的网站东莞回收网站设计
  • 北京驾校网站建设厦门网页设计培训班
  • 网络公司给我做网站我有没有源代码版权吗我怎么做个人网站
  • 免费建站网站一站式做网站需要懂那些软件
  • 做新网站怎样提交360寻找销售团队外包
  • 重庆市建设网站wordpress 新闻模版
  • 国内网站建设推荐手工做的网站
  • 深圳罗湖做网站的公司网站建设与管理案例教程第三版课后答案
  • 有关网站招标商务标书怎么做做终端客户网站
  • c 网站做微信收款功能青岛网站建设定制
  • 贵州安顺建设主管部门网站网站全程设计技术
  • 公司宣传网站建设企业网站建设与实现的论文
  • 连云港网站建设推广网站的推广优化
  • 手机商城网站制作公司网站版面设计方案
  • 网站开发制作熊掌号网站推广方法主要有什么
  • 怎么查看网站的建设时间提高企业网站的访问率
  • 宁德做网站的公司长沙网站建设 芙蓉区