首页>代码>基于springboot和websocket简单的聊天室>/chat-room-Strong-master/src/main/java/top/wonderheng/chatroom/service/WebSocket.java
package top.wonderheng.chatroom.service; import top.wonderheng.chatroom.vo.ContentVo; import top.wonderheng.chatroom.vo.Message; import com.google.gson.Gson; import org.springframework.stereotype.Component; import top.wonderheng.chatroom.vo.RandomCar; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.net.URLDecoder; import java.util.*; import java.util.concurrent.CopyOnWriteArraySet; @Component @ServerEndpoint("/webSocket") public class WebSocket { private Session session; private String username; private static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>(); private static Map<String, String> map = new HashMap<>(); private static RandomCar randomCar = new RandomCar(); @OnOpen public void onOpen(Session session) { this.session = session; webSockets.add(this); //获取用户名 String s = session.getQueryString(); String urlUsername = s.split("=")[1]; try { username = URLDecoder.decode(urlUsername, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } //把SessionID和用户名放进集合里面 map.put(session.getId(), username); System.out.println("有新的连接,总数:" + webSockets.size() + " sessionId:" + session.getId() + " " + username); String content = "\"" + username + "\" 开着超级酷炫的 \"" + randomCar.getMap() + "\" 进入了聊天室!"; Message message = new Message(content, map); send(message.toJson()); } @OnClose public void onClose() { webSockets.remove(this); map.remove(session.getId()); System.out.println("有新的断开,总数:" + webSockets.size() + " sessionId:" + session.getId()); String content = "\"" + username + "\" 离开了聊天室!"; Message message = new Message(content, map); send(message.toJson()); } private static Gson gson = new Gson(); @OnMessage public void onMessage(String json) { ContentVo contentVo = gson.fromJson(json, ContentVo.class); if (contentVo.getType() == 1) { //广播 Message message = new Message(); message.setContent(this.username, contentVo.getMsg()); message.setNames(map); send(message.toJson()); System.out.println(message.toJson()); } else { //单聊 Message message = new Message(); message.setContent(this.username, contentVo.getMsg()); message.setNames(map); String to = contentVo.getTo(); String tos[] = to.substring(0, to.length() - 1).split("-"); List<String> lists = Arrays.asList(tos); for (WebSocket webSocket : webSockets) { if (lists.contains(webSocket.session.getId()) && webSocket.session.getId() != this.session.getId()) { try { webSocket.session.getBasicRemote().sendText(message.toJson()); } catch (Exception e) { e.printStackTrace(); } } } } } public void send(String message) { for (WebSocket webSocket : webSockets) { try { webSocket.session.getBasicRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } } }
最近下载更多
Xiaobaiya11 LV2
3月4日
admin_z LV22
2023年12月21日
anninghh LV1
2023年12月4日
西瓜哥哥 LV4
2023年8月9日
gzryue LV6
2023年4月25日
天险无涯 LV15
2023年3月20日
微信网友_6393440821071872 LV2
2023年3月17日
zhaoxu123123 LV10
2022年12月4日
xiaoyuer2 LV8
2022年11月22日
万盛拓海 LV2
2022年10月20日
最近浏览更多
Xiaobaiya11 LV2
3月4日
uni-code_0123 LV1
1月31日
YhXyHx523 LV4
2023年12月24日
admin_z LV22
2023年12月21日
微信网友_6762641760833536
2023年12月4日
暂无贡献等级
anninghh LV1
2023年12月4日
skook7 LV2
2023年11月21日
杨最代1 LV5
2023年10月31日
lilong007 LV20
2023年10月19日
漫步的海星 LV4
2023年9月21日