map1 = Maps.newHashMap(); map1.put("messageType", 1); map1.put("userId", userId); sendMessageAll(JSON.toJSONString(map1), userId); //把自己的信息加入到map当中去 clients.put(userId, this); System.out.println("有连接关闭! 当前在线人数" + onlineNumber); //logger.info("有连接关闭! 当前在线人数" + clients.size()); //给自己发一条消息:告诉自己现在都有谁在线 Map map2 = Maps.newHashMap(); map2.put("messageType", 3); //移除掉自己 Set set = clients.keySet(); map2.put("onlineUsers", set); sendMessageTo(JSON.toJSONString(map2), userId); } catch (IOException e) { System.out.println(userId + "上线的时候通知所有人发生了错误"); //logger.info(userId+"上线的时候通知所有人发生了错误"); } } @OnError public void onError(Session session, Throwable error) { //logger.info("服务端发生了错误"+error.getMessage()); //error.printStackTrace(); System.out.println("服务端发生了错误:" + error.getMessage()); } /** * 连接关闭 */ @OnClose public void onClose() { onlineNumber--; //webSockets.remove(this); clients.remove(userId); try { //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息 Map map1 = Maps.newHashMap(); map1.put("messageType", 2); map1.put("onlineUsers", clients.keySet()); map1.put("userId", userId); sendMessageAll(JSON.toJSONString(map1), userId); } catch (IOException e) { System.out.println(userId + "下线的时候通知所有人发生了错误"); //logger.info(userId+"下线的时候通知所有人发生了错误"); } //logger.info("有连接关闭! 当前在线人数" + onlineNumber); //logger.info("有连接关闭! 当前在线人数" + clients.size()); System.out.println("有连接关闭! 当前在线人数" + onlineNumber); } /** * 收到客户端的消息 * * @param message 消息 * @param session 会话 */ @OnMessage public void onMessage(String message, Session session) { try { //logger.info("来自客户端消息:" + message+"客户端的id是:"+session.getId()); System.out.println("来自客户端消息:" + message + " | 客户端的id是:" + session.getId()); JSONObject jsonObject = JSON.parseobject(message); String textMessage = jsonObject.getString("message"); String fromuserId = jsonObject.getString("userId"); String touserId = jsonObject.getString("to"); //如果不是发给所有 , 那么就发给某一个人 //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息 Map map1 = Maps.newHashMap(); map1.put("messageType", 4); map1.put("textMessage", textMessage); map1.put("fromuserId", fromuserId); if (touserId.equals("All")) { map1.put("touserId", "所有人"); sendMessageAll(JSON.toJSONString(map1), fromuserId); } else { map1.put("touserId", touserId); System.out.println("开始推送消息给" + touserId); sendMessageTo(JSON.toJSONString(map1), touserId); } } catch (Exception e) { e.printStackTrace(); //logger.info("发生了错误了"); } } /** * 给指定的用户发送消息 * * @param message * @param TouserId * @throws IOException */ public void sendMessageTo(String message, String TouserId) throws IOException { for (WebSocket item : clients.values()) { System.out.println("给指定的在线用户发送消息,在线人员名单:【" + item.userId.toString() + "】发送消息:" + message); if (item.userId.equals(TouserId)) { item.session.getAsyncRemote().sendText(message); break; } } } /** * 给所有用户发送消息 * * @param message 数据 * @param FromuserId * @throws IOException */ public void sendMessageAll(String message, String FromuserId) throws IOException { for (WebSocket item : clients.values()) { System.out.println("给所有在线用户发送给消息 , 在线人员名单:【" + item.userId.toString() + "】发送消息:" + message); item.session.getAsyncRemote().sendText(message); } } /** * 给所有在线用户发送消息 * * @param message 数据 * @throws IOException */ public void sendMessageAll(String message) throws IOException { for (WebSocket item : clients.values()) { System.out.println("服务器给所有在线用户发送消息 , 当前在线人员为【" + item.userId.toString() + "】发送消息:" + message); item.session.getAsyncRemote().sendText(message); } } /** * 获取在线用户数 * * @return */ public static synchronized int getOnlineCount() { return onlineNumber; }}
TestController.java(前端控制器)#

文章插图
package com.cyb.socket.websocket;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.io.IOException;@Controller@RequestMapping("testMethod")public class TestController { @Autowired private WebSocket webSocket; /** * 给指定的在线用户发送消息 * @param userId * @param msg * @return * @throws IOException */ @ResponseBody @GetMapping("/sendTo") public String sendTo(@RequestParam("userId") String userId,@RequestParam("msg") String msg) throws IOException { webSocket.sendMessageTo(msg,userId); return "推送成功"; } /** * 给所有在线用户发送消息 * @param msg * @return * @throws IOException * @throws IOException */ @ResponseBody @PostMapping("/sendAll") public String sendAll(@RequestBody String msg) throws IOException, IOException { webSocket.sendMessageAll(msg); return "推送成功"; }}
推荐阅读
-
「印度」清一色机械化重装部队出现,印指挥官不再嘴硬,抱怨邻国隐藏实力
-
幽默笑话大王|我的话让司机懵了!,幽默笑话:开车被蹭了
-
『版权』独家版权即将到期,网易阿里抖音强敌环伺,腾讯音乐要往哪里走
-
躺倒鸭|腾讯开“卖”八位数QQ靓号充值超级会员即可使用
-
中国生物技术网|革命性突破!斯坦福团队找到关节软骨再生的方法!或将造福广大关节炎患者
-
-
现代出版社|称其为圣人,咋回事?,桓公曾拜管仲于马前
-
夏天的冰箱应该开多少档?冰箱夏季开到几档可以制冷_1
-
烹饪|凉拌土豆丝怎么做好吃?切丝焯水,开胃脆爽,上桌几分钟光盘
-
-
中国与西方神话的不同 从中西方神话故事看中国与西方的文化差异有哪些
-
-
燃气热水器打不着火是什么原因,燃气热水器不能打火怎么办
-
-
『OWNCAR奥卡』200元汽油开350公里油耗高?多少公里才正常?
-
十二星座魔羯座吧|化解尴尬的星座,心地善良,为他人着想,打圆场
-
华夏文物卖了468万,画一个地球又拍卖了3600万,马云随手写了2字
-
乳腺癌互助圈TB|揭开癌症转移之谜!原来与这种基因有关,它还影响着免疫治疗效果
-
-