第一步,在客户端配置

<script>
var websocket;
if ('WebSocket' in window) {
websocket = new WebSocket('ws://127.0.0.1:8080/websocket');//配置Websocket服务器端地址
} else {
alert("你的浏览器暂不支持websocket,请更换其他浏览器再试");
} websocket.onopen = function (event) {
console.log("建立连接");
};
websocket.onclose = function (event) {
console.log("关闭连接");
};
websocket.onmessage = function (event) {
console.log("收到消息," + event.data);
// 弹窗提示与播放提示音乐
var msg = event.data;
if (msg === "秒杀已结束") {
$('#seckill-box').html("秒杀结束");
}
};
websocket.onerror = function (event) {
console.log("websocket异常");
};
/**
* readyState状态如下:
* CONNECTING:值为0,表示正在连接;
OPEN:值为1,表示连接成功,可以通信了;
CLOSING:值为2,表示连接正在关闭;
CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
*/ // 向服务端发送消息(必须为open状态时可发送)
if (websocket.readyState === 1) {
websocket.send("服务端你好");
} </script>

第二步,在服务端配置

@Configuration
public class WebSocketConfig { @Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
} }
@ServerEndpoint("/websocket")
@Component
@Slf4j
public class WebSocketService { /**
* 记录当前websocket的连接数(保证线程安全)
*/
private static LongAdder connectAccount = new LongAdder(); /**
*存放每个客户端对应的websocketServer对象(需保证线程安全)
*/
private static CopyOnWriteArraySet<WebSocketService> webSocketSet = new CopyOnWriteArraySet<>();
/**
* 与客户端的连接对象
*/
private Session session; /**
* 连接成功调用的方法
* @param session
*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this);
connectAccount.increment();
log.info("有新的连接接入,当前连接数为{}", connectAccount);
} /**
* 连接关闭时调用
*/
@OnClose
public void onClose() {
webSocketSet.remove(this);
connectAccount.decrement();
log.info("有连接关闭,当前连接数为{}", connectAccount);
} /**
* 收到客户端消息时调用
* @param message
*/
@OnMessage
public void onMessage(String message) {
log.info("收到客户端发来的消息,message -> {}", message);
} /**
* 服务端向客户端发送消息
* @param message
*/
public void sendMessage(String message) {
for (WebSocketService webSocketService : webSocketSet) {
try {
log.info("【websocket消息】 广播消息, message={}", message);
webSocketService.session.getBasicRemote().sendText(message);
} catch (IOException e) {
log.info("向客户端发送消息失败, {}", e.getMessage());
}
}
} }

之后就可以在你需要推送消息的地方调用WebSocketService服务了。

最新文章

  1. 查找数组中重复项的index
  2. NHibernate系列文章二十二:NHibernate查询之HQL查询(附程序下载)
  3. C# 采用事务批量插入数据
  4. Slides - 在线制作效果精美的幻灯片(PPT)
  5. storm,hbase和storm-kafka-0.8-plus兼容性问题
  6. struts2 权限拦截器 拦截没有登陆的请求
  7. POJ3617 Best Cow Line
  8. linux浏览器,邮件客户端,输入法,双屏设置,应用软件,scrot -s截图,office
  9. phpStorm 各种快捷键
  10. 河南多校大一训练赛 C 青蛙的约会
  11. 我的学习之路_第三十四章_jsp
  12. Catalan数——卡特兰数
  13. Jenkins插件安装
  14. C#---ASP页面的下拉框模糊查询功能
  15. Python开发【第七篇】:面向对象二
  16. js处理时间时区问题
  17. DevOps简介
  18. 如何让浏览器在访问链接时不要带上referer
  19. unity,实现屏幕后处理的两种方法
  20. Intellij IDEA工具的常用快捷键

热门文章

  1. Prism学习笔记-模块之间通信的几种方式
  2. UML_2_浅谈UML的概念和模型之UML九种图
  3. springboot-elasticsearch项目启动报错:&#39;elasticsearchTemplate&#39; that could not be found
  4. web.xml文件的的param-name
  5. 数组被遗忘的内置对象--》Array.find()
  6. python 并发编程 协程 greenlet模块
  7. HTML标签--&gt;段落,格式,文本
  8. Kick Start 2019 Round D
  9. map member functions
  10. mysql整理-常用sql语句