- 前言
- 一、先创建好SpringBoot框架
- 二、使用步骤
- 1.使用maven引入依赖
- 2.创建服务端
- 创建WebSocketServer
- 创建WebSocketConfig
- 3.创建客户端-web版本
- web版连接演示
- 4.SpringBoot作为客户端 带断线重连
- 1.创建MyWebSocketClient
- 2.新建工具类解析ByteBuffer 数据 ByteUtils
- 新建WebSocketConfig
- 4.演示
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
SpringBoot中使用Websocket
提示:以下是本篇文章正文内容,下面案例可供参考
一、先创建好SpringBoot框架略
二、使用步骤 1.使用maven引入依赖代码如下:
2.创建服务端 创建WebSocketServerorg.springframework.boot spring-boot-starter-websocket org.projectlombok lombok org.apache.commons commons-lang3 3.8.1
package com.panbl.websocketdemo.websocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
@ServerEndpoint("/ws/{userId}")
public class WebSocketServer {
private static int onlineCount = 0;
private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap> devWebSocketMap = new ConcurrentHashMap<>();
private Session session;
private String userId = "";
@OnOpen
public void onOpen(Session session,@PathParam("userId") String userId) {
this.session = session;
//userId = session.getId()+"_"+userId; //拼接
this.userId=userId;
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
//加入set中
webSocketMap.put(userId,this);
}else{
//加入set中
webSocketMap.put(userId,this);
//在线数加1
addOnlineCount();
}
log.info("用户连接:"+userId+",当前在线用户为:" + getOnlineCount());
sendMessage("{"status":0,"msg":"连接成功"}");
}
@OnClose
public void onClose() {
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
//从set中删除
subOnlineCount();
}
log.info("用户退出:"+userId+",当前在线用户为:" + getOnlineCount());
}
@OnMessage
public void onMessage(String message, Session session) {
log.info("用户消息:"+userId+",报文:"+message);
//可以群发消息
//消息保存到数据库、redis
if(StringUtils.isNotBlank(message)){
try {
}catch (Exception e){
e.printStackTrace();
}
}
}
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误:"+this.userId+",原因:"+error.getMessage());
error.printStackTrace();
}
public void sendMessage(String message) {
try {
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendInfo(String message, String userId) {
log.info("发送消息到:"+userId+",报文:"+message);
if(StringUtils.isNotBlank(userId) && webSocketMap.containsKey(userId)){
webSocketMap.get(userId).sendMessage(message);
}else{
log.error("用户"+userId+",不在线!");
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
创建WebSocketConfig
package com.lst.cabinet.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 {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
以上为服务端代码。
3.创建客户端-web版本
websocket通讯
【socket开启者的ID信息】:
【客户端向服务器发送的内容】:
【操作】:
【操作】:


