|
|
@@ -1,7 +1,14 @@
|
|
|
package cn.edu.nju.plagdemo.api.socket;
|
|
|
|
|
|
+import cn.edu.nju.plagdemo.api.IPlagDetectApi;
|
|
|
+import cn.edu.nju.plagdemo.model.form.plag.PlagDetectionForm;
|
|
|
+import cn.edu.nju.plagdemo.utils.RestResult;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+
|
|
|
|
|
|
import javax.websocket.*;
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
@@ -9,11 +16,19 @@ import java.io.IOException;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-@ServerEndpoint("/test")
|
|
|
-@Component
|
|
|
+@ServerEndpoint("/plag")
|
|
|
+@Controller
|
|
|
@Slf4j
|
|
|
public class MyWebSocket {
|
|
|
|
|
|
+
|
|
|
+ private static IPlagDetectApi plagDetectApi;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setPlagDetectApi(IPlagDetectApi plagDetectApi) {
|
|
|
+ MyWebSocket.plagDetectApi = plagDetectApi;
|
|
|
+ }
|
|
|
+
|
|
|
//保存所有在线socket连接
|
|
|
private static Map<String, MyWebSocket> webSocketMap = new LinkedHashMap<>();
|
|
|
|
|
|
@@ -29,18 +44,25 @@ public class MyWebSocket {
|
|
|
this.session = session;
|
|
|
webSocketMap.put(session.getId(), this);
|
|
|
addCount();
|
|
|
- log.info("新的连接加入:{}", session.getId());
|
|
|
+ log.info("新的连接加入:{}", session.getId());
|
|
|
}
|
|
|
|
|
|
//接受消息
|
|
|
@OnMessage
|
|
|
- public void onMessage(String message, Session session) {
|
|
|
- log.info("收到客户端{}消息:{}", session.getId(), message);
|
|
|
- try {
|
|
|
- this.sendMessage("收到消息:" + message);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ public void onMessage(String message, Session session) throws IOException {
|
|
|
+ log.info("收到客户端{}消息:{}", session.getId(), message);
|
|
|
+ //此时的message是json格式的字符串
|
|
|
+ //example: {'fileId':'1','baseFileId':'1','fileType':'1','algorithmId':'1','params':'null'}
|
|
|
+ this.sendMessage("收到消息:" + message);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(message);
|
|
|
+ PlagDetectionForm plagDetectionForm = JSON.toJavaObject(jsonObject, PlagDetectionForm.class);
|
|
|
+ plagDetectApi.plagDetect(
|
|
|
+ plagDetectionForm.getFileId(),
|
|
|
+ plagDetectionForm.getBaseFileId(),
|
|
|
+ plagDetectionForm.getFileType(),
|
|
|
+ plagDetectionForm.getAlgorithmId(),
|
|
|
+ plagDetectionForm.getParams()
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
//处理错误
|
|
|
@@ -69,7 +91,8 @@ public class MyWebSocket {
|
|
|
MyWebSocket.webSocketMap.forEach((k, v) -> {
|
|
|
try {
|
|
|
v.sendMessage("这是一条测试广播");
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
});
|
|
|
}
|