wangwei 5 年之前
父节点
当前提交
ad1aba75f5
共有 1 个文件被更改,包括 63 次插入1 次删除
  1. 63 1
      src/main/java/cn/com/qmth/examcloud/ws/api/provider/WsCloudServiceProvider.java

+ 63 - 1
src/main/java/cn/com/qmth/examcloud/ws/api/provider/WsCloudServiceProvider.java

@@ -1,13 +1,28 @@
 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;
 
@@ -22,7 +37,54 @@ public class WsCloudServiceProvider implements WsCloudService {
 	@PostMapping("sendText")
 	@Override
 	public SendTextResp sendText(@RequestBody SendTextReq req) {
-		return null;
+
+		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);
+		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;
 	}
 
 }