往websocket发送消息,先发送暂停后发送一个新的开始命令, 当taskAction为2时是暂停 为 1是开始根据一下代码 补充代码 public void handle(String msg){ try { String[] ms = msg.split(","); if (ms.length == 3 && ms[0].startsWith("$W") && ms[2].startsWith("$C")) { DemoWebSocketClient client = new DemoWebSocketClient(new URI(url)); client.connect(); String nowChannel = ms[2].substring(2, 4); String o = redisTemplate.opsForValue().get("channel:freq:" + nowChannel); if(o != null && o != " "){ if (channel == null) { //TODO 往websocket发送消息,消息发送成功后将channel赋值 client.send(transferDtoToJson(1, Double.parseDouble(o))); channel = nowChannel; } else if (!nowChannel.equals(channel)) { //TODO 往websocket发送消息,先发送暂停后发送一个新的开始命令,消息发送成功后将channel赋值 channel = nowChannel; } } String[] jd = ms[0].substring(2).split("\\*"); double angle = Integer.parseInt(jd[0]) / 100.00D; String[] result = LonLatUtil.calLocationByDistanceAndLocationAndDirection(angle, lon, lat, radius); String value = lon + "," + lat + ";" + result[0] + "," + result[1]; redisTemplate.convertAndSend("vhfDf", value); redisTemplate.opsForValue().set("now:vhf:info", value, 1L, TimeUnit.SECONDS); } } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-01-18 12:01:48 浏览: 125
补充代码如下:
```java
else if (ms.length == 3 && ms[0].startsWith("$W") && ms[2].startsWith("$C")) {
DemoWebSocketClient client = new DemoWebSocketClient(new URI(url));
client.connect();
String nowChannel = ms[2].substring(2, 4);
String o = redisTemplate.opsForValue().get("channel:freq:" + nowChannel);
if(o != null && o != " "){
if (channel == null) {
//往websocket发送消息,消息发送成功后将channel赋值
String startMsg = transferDtoToJson(1, Double.parseDouble(o));
client.send(startMsg);
channel = nowChannel;
} else if (!nowChannel.equals(channel)) {
//往websocket发送消息,先发送暂停后发送一个新的开始命令,消息发送成功后将channel赋值
String pauseMsg = transferDtoToJson(2, 0);
client.send(pauseMsg);
String startMsg = transferDtoToJson(1, Double.parseDouble(o));
client.send(startMsg);
channel = nowChannel;
}
}
String[] jd = ms[0].substring(2).split("\\*");
double angle = Integer.parseInt(jd[0]) / 100.00D;
String[] result = LonLatUtil.calLocationByDistanceAndLocationAndDirection(angle, lon, lat, radius);
String value = lon + "," + lat + ";" + result[0] + "," + result[1];
redisTemplate.convertAndSend("vhfDf", value);
redisTemplate.opsForValue().set("now:vhf:info", value, 1L, TimeUnit.SECONDS);
}
```
在第一个if分支中,如果`channel`为空,则需要发送开始命令,待命令发送成功后再将`channel`赋值为当前频道`nowChannel`。如果`channel`不为空且与当前频道不同,则需要先发送暂停命令,待命令发送成功后再发送开始命令,并将`channel`赋值为当前频道`nowChannel`。具体的,暂停命令和开始命令的参数分别为2和1,频道的频率需要根据实际情况进行获取。
阅读全文