Spring Boot笔记-WebSocket的使用
程序结构如下:
关键源码如下:
WebSocketConfig.java
package com.example.demo.config;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();}}
MyController.java
package com.example.demo.controller;import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.WsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyController {@AutowiredWsService wsService;@PostMapping("/msg")public Object sendMsg(@RequestBody String req){JSONObject jsonObject = JSONObject.parseObject(req);System.out.println(jsonObject.getString("msg"));wsService.sendInfo(jsonObject.getString("msg"));return "success";}
}
WsService.java
package com.example.demo.service;import com.example.demo.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class WsService {@Autowiredpublic WebSocketServer webSocketServer;@Asyncpublic void sendInfo(String message) {webSocketServer.sendInfo(message);}
}
WebSocketServer.java
package com.example.demo.websocket;import org.springframework.stereotype.Component;import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;@Component
@ServerEndpoint("/ws/{token}")
public class WebSocketServer {//每个客户端有一个tokenprivate String token = "";private static HashMap<String, Session> map = new HashMap<>();@OnOpenpublic void onOpen(Session session, @PathParam("token") String token){map.put(token, session);this.token = token;System.out.println("新连接:" + session);}@OnClosepublic void onClose(Session session){map.remove(this.token);System.out.println("连接关闭:" + session);}@OnErrorpublic void onError(Session session, Throwable error){System.out.println("错误:" + session + "," + error);}public void onMessage(String message, Session session){System.out.println("接收到消息:" + message);}//群发public void sendInfo(String message){for (String token : map.keySet()) {Session session = map.get(token);try {session.getBasicRemote().sendText(message);}catch (IOException e) {e.printStackTrace();}}}
}
application.properties
server.port=8880
源码打包下载地址:
https://github.com/fengfanchen/Java/tree/master/WebServiceDemo
Spring Boot笔记-WebSocket的使用相关推荐
- spring boot 集成 websocket 实现消息主动推送
前言 http协议是无状态协议,每次请求都不知道前面发生了什么,而且只可以由浏览器端请求服务器端,而不能由服务器去主动通知浏览器端,是单向的,在很多场景就不适合,比如实时的推送,消息通知或者股票等信息 ...
- SpringBoot2.x系列教程(四十五)Spring Boot集成WebSocket实现技术交流群功能
在上篇文章中,我们了解了WebSocket的基本功能及相关概念.本篇文章中我们以具体的实例来演示,在Spring Boot中整合WebSocket,同时实现一个场景的业务场景功能. 针对在Spring ...
- Spring boot 实现 WebSocket服务端
Spring boot 实现 WebSocket服务端 这里写目录标题 Spring boot 实现 WebSocket服务端 声明 准备工作 1. 配置 2. WebSocket服务类 3. 编写客 ...
- Spring Boot 集成 WebSocket通信信息推送!
一.什么是websocket? WebSocket 协议是基于 TCP 的一种新的网络协议. 它实现了客户端与服务器之间的全双工通信,学过计算机网络都知道,既然是全双工,就说明了服务器可以主动发送信息 ...
- Spring Boot 整合 WebSocket 使用记录
这里写自定义目录标题 前言 WebSocket 简介 WebSocket 客户端(javascript前端)实现 javascript 实现 window.location获取URL中各部分 http ...
- 实时通信:使用Spring Boot实现Websocket
在开发Web应用程序时,有时我们需要将服务器事件下推到已连接的客户端. 但是,HTTP并非旨在允许这样做. 客户端打开与服务器的连接并请求数据. 服务器不会打开与客户端的连接并推送数据. 为了解决此限 ...
- Spring Boot笔记-普通异常错误截取及构造错误页面
在web开发过程中,数据问题会有异常,会抛到页面上,这样造成用户体验差. 目前这个笔记是使用Spring Boot获取异常,并构造新的界面 程序运行截图如下: 另外一个: 这样的话就达到了页面友好的效 ...
- spring boot 使用 websocket tomcat刚启动就关闭到问题
因为没有任何日志打印(也可能是我logback配置的问题),翻百度谷歌到的好多文章也没有结论,翻阅官方文档中文翻译版,也没发现说明,再去看英文版,也没任何资料. 最后无奈只好调试进源码,首先找断点位置 ...
- java websocket注解_【websocket】spring boot 集成 websocket 的四种方式
集成 websocket 的四种方案 1. 原生注解 pom.xml org.springframework.boot spring-boot-starter-websocket WebSocketC ...
最新文章
- 如何使用Nsight Compute?
- wince 6中硬键盘 输入法解决
- fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因
- 【数据库】Oracle用户、授权、角色管理
- Python自动化办公——xlrd、xlwt读写Excel
- LeetCode 31. 下一个排列(线性扫描)
- 淘宝自动发货申请资格
- 矩阵公式(转置公式+求导公式)
- 时间序列分析:平稳时间序列建模
- 数据库之如何解决SQL Server 2008 R2连接不上服务器?
- 资源共享的两阶段交叉效率DEA模型及matlab应用:地区科技投入产出效率案例分析,文后有网盘链接
- Unity3D 用脚本来操作游戏对象1(创建游戏对象)
- AndroidStudio 之Safe Delete 安全删除
- 有一张厚度为0.1mm的纸,假设它足够大,重复将其对折,问对折多少次之后,其厚度可达(再对折一次就超过)珠穆朗玛峰的高度?(C语言)
- apple watch怎么改铃声
- React 面试题 回答
- Python编程从入门到实践 动手试一试 代码合集
- SVG可伸缩的矢量图形 1
- QQ留言代码,网页QQ留言
- H.323 and Associated Protocols