|
@@ -0,0 +1,131 @@
|
|
|
+package com.neko.chat;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.neko.config.SpringConfigurator;
|
|
|
+import com.neko.domain.dto.messageDto.SendMessageContentDto;
|
|
|
+import com.neko.service.MessageService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+
|
|
|
+import javax.websocket.*;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
+
|
|
|
+@Component
|
|
|
+@ServerEndpoint(value = "/ws/{receiverId}/{userId}",configurator = SpringConfigurator.class)
|
|
|
+public class WebSocketServer {
|
|
|
+ private String receiverId;
|
|
|
+ private String token;
|
|
|
+ @Autowired
|
|
|
+ MessageService chatService;
|
|
|
+ //存放会话对象
|
|
|
+ public static ConcurrentMap<String, Session> sessionMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接建立成功调用的方法
|
|
|
+ */
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(Session session, @PathParam("userId") String userId) {
|
|
|
+ System.out.println("客户端:" + userId + "建立连接");
|
|
|
+ sessionMap.put(userId, session);
|
|
|
+ System.out.println("当前在线人数:" + sessionMap.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 收到客户端消息后调用的方法
|
|
|
+ *
|
|
|
+ * @param message 客户端发送过来的消息
|
|
|
+ */
|
|
|
+ @OnMessage
|
|
|
+ public void onMessage(String message, @PathParam("userId") String userId,@PathParam("receiverId") String receiverId) {
|
|
|
+// sendToClient(userId,friendId,message);
|
|
|
+ System.out.println("客服端接受到用户:" + userId + "向好友:"+receiverId+"发送的消息:" + message);
|
|
|
+ Map<String, String> map = JSON.parseObject(message, Map.class);
|
|
|
+ SendMessageContentDto sendMessageContent = new SendMessageContentDto(userId, receiverId, map.get("content"));
|
|
|
+
|
|
|
+ boolean success = chatService.sendMessage(sendMessageContent);
|
|
|
+ if (success){
|
|
|
+ System.out.println("发送消息成功");
|
|
|
+ }else {
|
|
|
+ System.out.println("发送消息失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接关闭调用的方法
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ */
|
|
|
+ @OnClose
|
|
|
+ public void onClose(@PathParam("userId") Integer userId) {
|
|
|
+ System.out.println("客户端:" + userId + "关闭连接");
|
|
|
+ sessionMap.remove(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 群发
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ */
|
|
|
+ public void sendToAllClient(String message) {
|
|
|
+ Collection<Session> sessions = sessionMap.values();
|
|
|
+ for (Session session : sessions) {
|
|
|
+ try {
|
|
|
+ System.out.println("服务端向所有客户端发送消息:" + message);
|
|
|
+ session.getBasicRemote().sendText(message);
|
|
|
+ } catch (Exception e) {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 私发
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ */
|
|
|
+ public static void sendToClient(Integer userId,Integer friendId,String message) {
|
|
|
+ Session session = sessionMap.get(friendId);
|
|
|
+ if (session != null){
|
|
|
+ try {
|
|
|
+ System.out.println("用户:" + userId + "向好友:"+friendId+"发送了消息:" + message);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("message",message);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
|
|
|
+ map.put("time",sdf.format(new Date()));
|
|
|
+ String jsonString = JSON.toJSONString(map);
|
|
|
+ session.getBasicRemote().sendText(jsonString);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AI发送的信息
|
|
|
+ */
|
|
|
+ public static void aiSendToClient(Integer userId,String message) {
|
|
|
+ Session session = sessionMap.get(userId);
|
|
|
+ if (session != null){
|
|
|
+ try {
|
|
|
+ System.out.println("AI向:" + userId +"发送了消息:" + message);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("message",message);
|
|
|
+ map.put("type","waring");
|
|
|
+ String jsonString = JSON.toJSONString(map);
|
|
|
+ session.getBasicRemote().sendText(jsonString);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|