1.介绍
WebSocket是HTML5新增的协议,它的目的是在浏览器和服务器之间建立一个不受限的双向通信的通道。实时推送数据/通知到浏览器
方法一
- 引入WebSocket依赖包
````xmlorg.springframework.boot
spring-boot-starter-websocketorg.springframework.boot
spring-boot-starter-webflux - 编写 WebSocket 实现方法
@ServerEndpoint(value = "/websocket/{id}") @Component public class WebSocketServer { /** * 静态常量,记录连接个数,线程安全 */ private static int onlineCount = 0; /** * 存放客户端对应的WebSocket对象 */ private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>(); /** * 会话信息 */ private Session session; @OnOpen public void onOpen(Session session){ this.session = session; webSocketSet.add(this); addOnlineCount(); try { sendMessage("连接成功"); }catch (IOException ex){ System.out.println(ex.getMessage()); } } @OnMessage public void onMessage(String message, Session session, @PathParam("id")String id){ System.out.println("来自客户端的消息:" + message); //群发消息 for (WebSocketServer item : webSocketSet) { try { item.sendMessage(message); } catch (IOException e) { e.printStackTrace(); } } } @OnClose public void onClass(){ webSocketSet.remove(this); subOnlineCount(); System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); } @OnError public void onError(Session session,Exception ex){ ex.printStackTrace(); } /** * 发送消息 * @param message * @throws IOException */ private void sendMessage(String message) throws IOException { this.session.getBasicRemote().sendText(message); } private void sendInfo(String message){ System.out.println(message ); for (WebSocketServer item:webSocketSet ) { try{ item.sendMessage(message); }catch (IOException ex){ System.out.println(ex.getMessage()); } } } private synchronized void addOnlineCount(){ WebSocketServer.onlineCount ++; } private synchronized void subOnlineCount(){ WebSocketServer.onlineCount --; } public static synchronized long getOnlineCount(){ return WebSocketServer.onlineCount; } }
- 添加配置文件
````java
@Configuration
public class WebSocketConfig {@Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }
}
####方法二
- 编写消息处理类
public class WebSocketMessageHandler extends TextWebSocketHandler {
/**
* 静态常量,保存连接数量,线程安全
*/
private static long onlineCount = 0;
/**
* 保存客户端信息
*/
private static CopyOnWriteArraySet<WebSocketSession> webSocketSet = new CopyOnWriteArraySet();
/**
* 会话信息
*/
private WebSocketSession session;
public WebSocketMessageHandler(){
super();
}
/**
* 连接建立之后
* @param session
* @throws Exception
*/
public void afterConnectionEstablished(WebSocketSession session) throws Exception{
this.session = session;
webSocketSet.add(session);
addOnlineCount();
System.out.println("连接成功,当前连接数:"+getOnlineCount());
}
/**
* 处理收到的websocket信息
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload();
System.out.println("sadddddd");
sendMessage(payload);
}
public void handleTransportError(WebSocketSession session, Throwable var2) throws Exception{
}
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception{
webSocketSet.remove(session);
subOnlineCount();
System.out.println("连接断开,当前连接数:"+getOnlineCount());
}
private void sendMessage(String message) throws IOException {
for (WebSocketSession session: webSocketSet ) {
session.sendMessage(new TextMessage( message));
}
}
///////
private synchronized void addOnlineCount(){
onlineCount++;
}
private synchronized void subOnlineCount(){
onlineCount--;
}
private synchronized long getOnlineCount(){
return this.onlineCount;
}
}
- 位置文件
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(handler(),"/notice/{id}")//访问地址
.setAllowedOrigins("*");///解决跨域问题
}
public WebSocketHandler handler(){
return new WebSocketMessageHandler();
}
}
总结
1.网上找资料按照第一种实现,结果总是报错,比较靠谱的解释是aop拦截问题,使得ServerEndpointExporter
注册不进去;
2.第二种方法有效,一定不要忘记加依赖文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
原文地址:https://www.cnblogs.com/monkay/p/11130255.html
时间: 2024-11-05 22:49:24