赞
踩
1、maven依赖
<!-- webSocket --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency>
2、新建 WebSocketStompConfig 类
- /**
- * 开启WebSocket支持
- */
- @Configuration
- public class WebSocketStompConfig {
- @Bean
- public ServerEndpointExporter serverEndpointExporter()
- {
- return new ServerEndpointExporter();
- }
-
- }
3、新建 WebSocket 类
- /**
- * WebSocketServer
- */
- @Component
- @ServerEndpoint("/websocket/{username}")
- @Log4j2
- public class WebSocket {
- private Logger logger = LoggerFactory.getLogger(this.getClass());
-
- /**
- * 在线人数
- */
- public static int onlineNumber = 0;
- /**
- * 以用户的姓名为key,WebSocket为对象保存起来
- */
- private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
- /**
- * 会话
- */
- private Session session;
- /**
- * 用户名称
- */
- private String username;
-
- /**
- * 建立连接
- *
- * @param session
- */
- @OnOpen
- public void onOpen(@PathParam("username") String username, Session session) {
- onlineNumber++;
- logger.info("现在来连接的客户id:" + session.getId() + "用户名:" + username);
- this.username = username;
- this.session = session;
- logger.info("有新连接加入! 当前在线人数" + onlineNumber);
- try {
- //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息
- //先给所有人发送通知,说我上线了
- Map<String, Object> map1 = new HashMap<>();
- map1.put("messageType", 1);
- map1.put("username", username);
- sendMessageAll(JSON.toJSONString(map1), username);
-
- //把自己的信息加入到map当中去
- clients.put(username, this);
- //给自己发一条消息:告诉自己现在都有谁在线
- Map<String, Object> map2 =new HashMap<>();
- map2.put("messageType", 3);
- //移除掉自己
- Set<String> set = clients.keySet();
- map2.put("onlineUsers", set);
- sendMessageTo(JSON.toJSONString(map2), username);
- } catch (IOException e) {
- logger.info(username + "上线的时候通知所有人发生了错误");
- }
-
-
- }
-
- @OnError
- public void onError(Session session, Throwable error) {
- logger.info("服务端发生了错误" + error.getMessage());
- //error.printStackTrace();
- }
-
- /**
- * 连接关闭
- */
- @OnClose
- public void onClose() {
- onlineNumber--;
- //webSockets.remove(this);
- clients.remove(username);
- try {
- //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息
- Map<String, Object> map1 = new HashMap<>();
- map1.put("messageType", 2);
- map1.put("onlineUsers", clients.keySet());
- map1.put("username", username);
- sendMessageAll(JSON.toJSONString(map1), username);
- } catch (IOException e) {
- logger.info(username + "下线的时候通知所有人发生了错误");
- }
- logger.info("有连接关闭! 当前在线人数" + onlineNumber);
- }
-
- /**
- * 收到客户端的消息
- *
- * @param message 消息
- * @param session 会话
- */
- @OnMessage
- public void onMessage(String message, Session session) {
- try {
- logger.info("来自客户端消息:" + message + "客户端的id是:" + session.getId());
- JSONObject jsonObject = JSON.parseObject(message);
- String textMessage = jsonObject.getString("message");
- String fromusername = jsonObject.getString("username");
- String tousername = jsonObject.getString("to");
- //如果不是发给所有,那么就发给某一个人
- //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息
- Map<String, Object> map1 = new HashMap<>();
- map1.put("messageType", 4);
- map1.put("textMessage", textMessage);
- map1.put("fromusername", fromusername);
- if (tousername.equals("All")) {
- map1.put("tousername", "所有人");
- sendMessageAll(JSON.toJSONString(map1), fromusername);
- } else {
- map1.put("tousername", tousername);
- sendMessageTo(JSON.toJSONString(map1), tousername);
- }
- } catch (Exception e) {
- logger.info("发生了错误了");
- }
-
- }
-
-
- public void sendMessageTo(String message, String ToUserName) throws IOException {
- for (WebSocket item : clients.values()) {
- if (item.username.equals(ToUserName)) {
- item.session.getAsyncRemote().sendText(message);
- break;
- }
- }
- }
-
- public void sendMessageAll(String message, String FromUserName) throws IOException {
- for (WebSocket item : clients.values()) {
- item.session.getAsyncRemote().sendText(message);
- }
- }
-
- public static synchronized int getOnlineCount() {
- return onlineNumber;
- }
- }

4、前端访问地址 ws://地址/用户名
5、发送信息格 (to = All:所有人)
{
"message": "消息内容",
"to": "All",
"username": "发送人名称"
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。