|
@@ -0,0 +1,92 @@
|
|
|
|
+package cn.com.qmth.examcloud.ws.api.provider;
|
|
|
|
+
|
|
|
|
+import javax.websocket.Session;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.logging.log4j.ThreadContext;
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+
|
|
|
|
+import com.google.gson.JsonElement;
|
|
|
|
+import com.google.gson.JsonParser;
|
|
|
|
+import com.google.gson.JsonSyntaxException;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.UserType;
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
|
|
+import cn.com.qmth.examcloud.ws.api.WsCloudService;
|
|
|
|
+import cn.com.qmth.examcloud.ws.api.request.SendTextReq;
|
|
|
|
+import cn.com.qmth.examcloud.ws.api.response.SendTextResp;
|
|
|
|
+import cn.com.qmth.examcloud.ws.starter.core.MessageOut;
|
|
|
|
+import cn.com.qmth.examcloud.ws.starter.core.SessionInfo;
|
|
|
|
+import cn.com.qmth.examcloud.ws.starter.core.WebSocketHelper;
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
+
|
|
|
|
+@Api(tags = "websocket服务")
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("api/cloud/ws")
|
|
|
|
+public class WsCloudServiceProvider implements WsCloudService {
|
|
|
|
+
|
|
|
|
+ private static final long serialVersionUID = 4784116669909937047L;
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "发送消息")
|
|
|
|
+ @PostMapping("sendText")
|
|
|
|
+ @Override
|
|
|
|
+ public SendTextResp sendText(@RequestBody SendTextReq req) {
|
|
|
|
+
|
|
|
|
+ Long rootOrgId = req.getRootOrgId();
|
|
|
|
+ UserType userType = req.getUserType();
|
|
|
|
+ Long userId = req.getUserId();
|
|
|
|
+ String path = req.getPath();
|
|
|
|
+ String eventId = req.getEventId();
|
|
|
|
+ String content = req.getContent();
|
|
|
|
+
|
|
|
|
+ User user = new User();
|
|
|
|
+ user.setUserType(userType);
|
|
|
|
+ user.setUserId(userId);
|
|
|
|
+ user.setRootOrgId(rootOrgId);
|
|
|
|
+ String key = user.buildKey();
|
|
|
|
+
|
|
|
|
+ Session session = WebSocketHelper.getSession(path, key);
|
|
|
|
+
|
|
|
|
+ if (null == session) {
|
|
|
|
+ throw new StatusException("100001", "no ws session about path [" + path + "]");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SessionInfo sessionInfo = WebSocketHelper.getSessionInfo(session);
|
|
|
|
+
|
|
|
|
+ if (null == sessionInfo) {
|
|
|
|
+ throw new StatusException("100002", "no ws session info");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ MessageOut out = new MessageOut(path, sessionInfo.getSessionId());
|
|
|
|
+ out.setEventId(eventId);
|
|
|
|
+
|
|
|
|
+ JsonElement jsonEl = null;
|
|
|
|
+ if (StringUtils.isNotBlank(content)) {
|
|
|
|
+ try {
|
|
|
|
+ jsonEl = JsonParser.parseString(content);
|
|
|
|
+ } catch (JsonSyntaxException e) {
|
|
|
|
+ throw new StatusException("100003", "message is not a json string");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ out.setContent(jsonEl);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ ThreadContext.put("TRACE_ID", sessionInfo.getSessionId());
|
|
|
|
+ WebSocketHelper.sendText(session, path, out);
|
|
|
|
+ } finally {
|
|
|
|
+ ThreadContext.put("TRACE_ID", ThreadLocalUtil.getTraceId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SendTextResp resp = new SendTextResp();
|
|
|
|
+ resp.setSessionId(sessionInfo.getSessionId());
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|