|
@@ -1,8 +1,15 @@
|
|
package shop.alien.store.config;
|
|
package shop.alien.store.config;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
+import shop.alien.entity.result.R;
|
|
|
|
+import shop.alien.entity.store.LifeMessage;
|
|
|
|
+import shop.alien.entity.store.vo.WebsocketVo;
|
|
|
|
+import shop.alien.mapper.LifeMessageMapper;
|
|
|
|
|
|
import javax.websocket.*;
|
|
import javax.websocket.*;
|
|
import javax.websocket.server.PathParam;
|
|
import javax.websocket.server.PathParam;
|
|
@@ -21,6 +28,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
@ServerEndpoint(value = "/socket/{sendId}")
|
|
@ServerEndpoint(value = "/socket/{sendId}")
|
|
public class WebSocketProcess {
|
|
public class WebSocketProcess {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private LifeMessageMapper lifeMessageMapper;
|
|
|
|
+
|
|
/*
|
|
/*
|
|
* 持有每个webSocket对象,以key-value存储到线程安全ConcurrentHashMap,
|
|
* 持有每个webSocket对象,以key-value存储到线程安全ConcurrentHashMap,
|
|
*/
|
|
*/
|
|
@@ -56,8 +66,25 @@ public class WebSocketProcess {
|
|
* 接收到客户端消息时触发
|
|
* 接收到客户端消息时触发
|
|
*/
|
|
*/
|
|
@OnMessage
|
|
@OnMessage
|
|
- public void onMessage(String message, @PathParam("sendId") String sendId) throws Exception {
|
|
|
|
- log.info("receive a message from client id={},msg={}", sendId, message);
|
|
|
|
|
|
+ public R<String> onMessage(String message, @PathParam("sendId") String sendId) throws Exception {
|
|
|
|
+ try {
|
|
|
|
+ WebsocketVo websocketVo = JSONObject.parseObject(message, WebsocketVo.class);
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("type", websocketVo.getType());
|
|
|
|
+ jsonObject.put("text", websocketVo.getText());
|
|
|
|
+ sendMessage(websocketVo.getReceiverId(), jsonObject.toJSONString());
|
|
|
|
+
|
|
|
|
+ LifeMessage lifeMessage = new LifeMessage();
|
|
|
|
+ lifeMessage.setSenderId(websocketVo.getSenderId());
|
|
|
|
+ lifeMessage.setReceiverId(websocketVo.getReceiverId());
|
|
|
|
+ lifeMessage.setContent(websocketVo.getText());
|
|
|
|
+ lifeMessage.setType(websocketVo.getType());
|
|
|
|
+ lifeMessageMapper.insert(lifeMessage);
|
|
|
|
+ return R.success("发送成功");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("WebSocketController_sendMsgToClientById Error Mgs={}", e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return R.fail("发送失败");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|