package com.bjsxt.server; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import com.bjsxt.vo.ContenVo; import com.bjsxt.vo.Message; import com.google.gson.Gson; @ServerEndpoint("/chatSocket") public class ChatSocket { private static Set<ChatSocket> sockets = new HashSet<ChatSocket>(); //储存所有的通信通道 private static List<String> names = new ArrayList<String>(); //储存所有的联系人下姓名 private static Map<String, Session> map = new HashMap<String, Session>(); //将联系人与通信通道同时保存 private Session session; //当前聊天人session private String username; //当前聊天人姓名 private Gson gson = new Gson(); /** * 打开通道时调用 * @param session * @throws IOException */ @SuppressWarnings("unused") @OnOpen public void open(Session session) throws Exception { this.session = session; sockets.add(this); String queryString = session.getQueryString(); //获取请求连接后面的值,输入汉字会有乱码,实际项目中使用账户ID this.username = queryString.substring(queryString.indexOf("=") + 1); names.add(this.username); map.put(this.username, session); Message message = new Message(); message.setAlert(this.username + "进入聊天室!!"); message.setNames(names); broadcast(sockets, gson.toJson(message)); } /** * 发送消息时调用 * @param session * @param msg * @throws IOException */ @OnMessage public void receive(Session session, String msg) throws Exception { ContenVo contenVo = gson.fromJson(msg, ContenVo.class); if (contenVo.getType().equals("1")) { //群聊 Message message = new Message(); message.setSendMsg(contenVo.getMsg()); message.setFrom(this.username); message.setDate(new Date().toLocaleString()); broadcast(sockets, gson.toJson(message)); }else if(contenVo.getType().equals("2")){ //单聊 String to = contenVo.getTo(); Session session2 = this.map.get(to); Message message = new Message(); message.setSendMsg("<font color=\"#FF0000\" >私聊:"+contenVo.getMsg()+"<font>"); message.setFrom(this.username); message.setDate(new Date().toLocaleString()); session.getBasicRemote().sendText(gson.toJson(message)); session2.getBasicRemote().sendText(gson.toJson(message)); } } /** * 关闭网页时调用 * @param session * @throws IOException */ @OnClose public void close(Session session) throws Exception { sockets.remove(this); names.remove(this.username); Message message = new Message(); message.setAlert(this.username + "退出聊天室!!"); message.setNames(names); broadcast(sockets, gson.toJson(message)); } /** * 群聊时将消息循环推送到前台 * @param chatSockets * @param msg * @throws IOException * @throws CloneNotSupportedException */ public void broadcast(Set<ChatSocket> chatSockets, String msg) throws Exception { for (ChatSocket chatSocket : chatSockets) { chatSocket.session.getBasicRemote().sendText(msg); } } }

忘了密码来摸鱼 LV1
2023年5月29日
wenwen520_li LV1
2022年11月19日
杨豫川 LV12
2022年5月11日
912299793 LV21
2022年3月18日
berylzZz LV1
2022年3月13日
JulyMagnolia LV4
2021年11月27日
1ling1 LV2
2021年6月24日
thinkerkkk LV2
2021年6月22日
YZN212 LV3
2021年6月1日
dalamama LV1
2021年4月27日

473228892
2月10日
暂无贡献等级
ma406805131 LV19
2024年12月24日
nghiavudev
2024年11月3日
暂无贡献等级
teamather
2024年11月3日
暂无贡献等级
dapeioo
2024年10月8日
暂无贡献等级
zhoubin2000
2024年6月14日
暂无贡献等级
磨磨唧唧999
2024年6月14日
暂无贡献等级
pangzihao
2024年6月3日
暂无贡献等级
liqizuiai
2024年5月15日
暂无贡献等级
xiexin LV1
2024年4月9日