首页>代码>基于springboot+websocket的简单在线聊天室案例>/online_chat_room/src/main/java/com/lianchua/chatoline/websocket/ChatRoomServerEndpoint.java
package com.lianchua.chatoline.websocket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import static com.lianchua.chatoline.utils.WebSocketUtils.LIVING_SESSIONS_CACHE; import static com.lianchua.chatoline.utils.WebSocketUtils.sendMessage; import static com.lianchua.chatoline.utils.WebSocketUtils.sendMessageAll; /** * 聊天室 * * @author Levin * @since 2018/6/26 0026 */ @RestController @ServerEndpoint("/chat-room/{username}") public class ChatRoomServerEndpoint { private static final Logger log = LoggerFactory.getLogger(ChatRoomServerEndpoint.class); @OnOpen public void openSession(@PathParam("username") String username, Session session) { LIVING_SESSIONS_CACHE.put(username, session); Date date = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a hh:mm:ss"); String time = df.format(date); String message = time + " 欢迎用户[" + username + "] 来到聊天室!\n"; log.info(message); sendMessageAll(message); } @OnMessage public void onMessage(@PathParam("username") String username, String message) { log.info(message); Date date = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a hh:mm:ss"); String time = df.format(date); sendMessageAll(time + "用户[" + username + "] : " + message + "\n"); System.gc(); } @OnClose public void onClose(@PathParam("username") String username, Session session) { //当前的Session 移除 LIVING_SESSIONS_CACHE.remove(username); //并且通知其他人当前用户已经离开聊天室了 sendMessageAll("用户[" + username + "] 已经离开聊天室了!\n"); try { session.close(); } catch (IOException e) { e.printStackTrace(); } } @OnError public void onError(Session session, Throwable throwable) { try { session.close(); } catch (IOException e) { e.printStackTrace(); } // throwable.printStackTrace(); } @GetMapping("/chat-room/{sender}/to/{receive}") public void onMessage(@PathVariable("sender") String sender, @PathVariable("receive") String receive, String message) { sendMessage(LIVING_SESSIONS_CACHE.get(receive), "[" + sender + "]" + "-> [" + receive + "] : " + message); System.out.println(message); } }

JiangYing009 LV8
2024年4月23日
微信网友_5930136709730304 LV4
2022年11月30日
天气预报 LV11
2022年8月14日
Kevin_001 LV6
2022年7月3日
姜广坤 LV14
2022年6月1日
liuxie LV12
2022年5月12日
caratttt LV2
2022年4月15日
XXXinXXccv LV2
2022年2月15日
lf624645770 LV3
2021年8月27日
breakCY LV15
2021年8月18日

13133117021 LV5
2024年12月24日
ma406805131 LV19
2024年12月17日
haomc052829 LV4
2024年12月7日
c12s34g
2024年10月14日
暂无贡献等级
wwwrrr
2024年9月4日
暂无贡献等级
3263394665 LV9
2024年8月31日
大溪地
2024年7月18日
暂无贡献等级
gnnhka LV10
2024年7月6日
3334004690 LV10
2024年6月24日
IrvinXi
2024年4月18日
暂无贡献等级