|
1 | 1 | package com.xiaoju.framework.handler;
|
2 | 2 |
|
3 |
| -import com.alibaba.fastjson.JSON; |
4 |
| -import com.alibaba.fastjson.JSONArray; |
5 |
| -import com.alibaba.fastjson.JSONObject; |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
6 | 6 | import com.xiaoju.framework.entity.persistent.TestCase;
|
7 | 7 | import com.xiaoju.framework.mapper.TestCaseMapper;
|
8 | 8 | import com.xiaoju.framework.service.CaseBackupService;
|
9 | 9 | import com.xiaoju.framework.service.RecordService;
|
10 | 10 | import com.xiaoju.framework.util.BitBaseUtil;
|
11 |
| -import com.xiaoju.framework.util.TreeUtil; |
12 | 11 | import org.apache.poi.util.StringUtil;
|
13 | 12 | import org.slf4j.Logger;
|
14 | 13 | import org.slf4j.LoggerFactory;
|
15 | 14 | import org.springframework.util.StringUtils;
|
16 | 15 |
|
17 |
| -import javax.annotation.Resource; |
18 | 16 | import javax.websocket.Session;
|
19 |
| -import java.awt.*; |
20 | 17 | import java.util.*;
|
21 | 18 | import java.util.List;
|
22 | 19 | import java.util.concurrent.ConcurrentHashMap;
|
@@ -46,6 +43,8 @@ public abstract class Room {
|
46 | 43 | public static RecordService recordService;
|
47 | 44 | public static CaseBackupService caseBackupService;
|
48 | 45 |
|
| 46 | + ObjectMapper jsonMapper = new ObjectMapper(); |
| 47 | + |
49 | 48 | protected String testCaseContent;
|
50 | 49 | protected TestCase testCase;
|
51 | 50 |
|
@@ -190,18 +189,22 @@ private void broadcastMessage(String msg) {
|
190 | 189 | } else {
|
191 | 190 | int seperateIndex = msg.indexOf('|');
|
192 | 191 | String sendSessionId = msg.substring(0, seperateIndex);
|
193 |
| - JSONObject request = JSON.parseObject(msg.substring(seperateIndex + 1)); |
194 |
| - JSONArray patch = (JSONArray) request.get("patch"); |
195 |
| - long currentVersion = ((JSONObject) request.get("case")).getLong("base"); |
196 |
| - testCaseContent = ((JSONObject) request.get("case")).toJSONString().replace("\"base\":" + currentVersion, "\"base\":" + (currentVersion + 1)); |
197 |
| - for (Player p : players) { |
198 |
| - if (sendSessionId.equals(p.getClient().getSession().getId())) { //ack消息 |
199 |
| - String msgAck = "[[{\"op\":\"replace\",\"path\":\"/base\",\"value\":" + (currentVersion + 1) + "}]]"; |
200 |
| - p.getBufferedMessages().add(msgAck); |
201 |
| - } else { // notify消息 |
202 |
| - String msgNotify = patch.toJSONString().replace("[[{", "[[{\"op\":\"replace\",\"path\":\"/base\",\"value\":" + (currentVersion + 1) + "},{"); |
203 |
| - p.getBufferedMessages().add(msgNotify); |
| 192 | + try { |
| 193 | + JsonNode request = jsonMapper.readTree(msg.substring(seperateIndex + 1)); |
| 194 | + ArrayNode patch = (ArrayNode) request.get("patch"); |
| 195 | + long currentVersion = ((JsonNode) request.get("case")).get("base").asLong(); |
| 196 | + testCaseContent = ((JsonNode) request.get("case")).toString().replace("\"base\":" + currentVersion, "\"base\":" + (currentVersion + 1)); |
| 197 | + for (Player p : players) { |
| 198 | + if (sendSessionId.equals(p.getClient().getSession().getId())) { //ack消息 |
| 199 | + String msgAck = "[[{\"op\":\"replace\",\"path\":\"/base\",\"value\":" + (currentVersion + 1) + "}]]"; |
| 200 | + p.getBufferedMessages().add(msgAck); |
| 201 | + } else { // notify消息 |
| 202 | + String msgNotify = patch.toString().replace("[[{", "[[{\"op\":\"replace\",\"path\":\"/base\",\"value\":" + (currentVersion + 1) + "},{"); |
| 203 | + p.getBufferedMessages().add(msgNotify); |
| 204 | + } |
204 | 205 | }
|
| 206 | + } catch (Exception e) { |
| 207 | + LOGGER.error("json 操作失败。", e); |
205 | 208 | }
|
206 | 209 | }
|
207 | 210 | }
|
|
0 commit comments