|
@@ -12,45 +12,126 @@ import javax.websocket.Session;
|
|
|
import javax.websocket.server.PathParam;
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
+import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
|
|
|
|
+/**
|
|
|
+ * websocket ServerEndpoint
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2019年11月27日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
@ServerEndpoint("/{path}")
|
|
|
@Component
|
|
|
public class WebSocketServerEndpoint {
|
|
|
|
|
|
+ private static final ExamCloudLog WS_LOG = ExamCloudLogFactory.getLog("WS");
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisClient redisClient;
|
|
|
+
|
|
|
@OnOpen
|
|
|
public void onOpen(Session session, @PathParam("path") String path) {
|
|
|
- System.out.println("onOpen; session=" + session + "; path=" + path);
|
|
|
+
|
|
|
+ if (WS_LOG.isDebugEnabled()) {
|
|
|
+ WS_LOG.debug("[onOpen]. path=" + path + "");
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = getRequestParameter(session, "key");
|
|
|
+ String token = getRequestParameter(session, "token");
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(key)) {
|
|
|
+ WS_LOG.error("[onOpen-FAIL]. path=" + path + ". key is blank");
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(token)) {
|
|
|
+ WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is blank");
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ User user = redisClient.get(key, User.class);
|
|
|
+
|
|
|
+ if (null == user) {
|
|
|
+ WS_LOG.error("[onOpen-FAIL]. path=" + path + ". no login.");
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
+ } else if (!token.equals(user.getToken())) {
|
|
|
+ WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is wrong.");
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
- session.getBasicRemote().sendText(DateUtil.chinaNow() + "| fuck");
|
|
|
+ session.getBasicRemote().sendText("Hello");
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ WS_LOG.error("[onOpen-FAIL]. path=" + path + "", e);
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
+ String sessionId = buildSessionId(user, path);
|
|
|
+
|
|
|
+ SessionHolder.setSession(sessionId, session);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建 sessionId
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param user
|
|
|
+ * @param path
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String buildSessionId(User user, String path) {
|
|
|
+ return user.getKey() + ":" + path;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求参数
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param session
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getRequestParameter(Session session, String key) {
|
|
|
Map<String, List<String>> params = session.getRequestParameterMap();
|
|
|
- String key = params.get("key").get(0);
|
|
|
- String token = params.get("token").get(0);
|
|
|
- System.out.println("key=" + key);
|
|
|
- System.out.println("token=" + token);
|
|
|
+ List<String> list = params.get(key);
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String value = StringUtils.join(list, ",");
|
|
|
+ return value;
|
|
|
}
|
|
|
|
|
|
@OnClose
|
|
|
public void onClose(Session session, @PathParam("path") String path) {
|
|
|
- System.out.println("onClose; session=" + session + "; path=" + path);
|
|
|
+ WS_LOG.debug("[onClose]. path=" + path);
|
|
|
+ SessionHolder.delSession(session);
|
|
|
}
|
|
|
|
|
|
@OnMessage
|
|
|
public void onMessage(Session session, @PathParam("path") String path, String message) {
|
|
|
- System.out.println("onMessage; session=" + session + "; path=" + path);
|
|
|
- System.out.println("onMessage; message=" + message);
|
|
|
+ WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
|
|
|
}
|
|
|
|
|
|
@OnError
|
|
|
public void onError(Session session, @PathParam("path") String path, Throwable t) {
|
|
|
- System.out.println("onError; session=" + session + "; path=" + path);
|
|
|
- t.printStackTrace();
|
|
|
+ WS_LOG.error("[onError]. path=" + path, t);
|
|
|
+ SessionHolder.delSession(session);
|
|
|
}
|
|
|
|
|
|
}
|