实时数据页面增加缓存数据

This commit is contained in:
yuyang 2021-08-30 16:36:31 +08:00
parent a4efc46b12
commit 630a1a8c55

View File

@ -1,13 +1,17 @@
package com.xhpc.wxma.socket;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.wxma.domain.WebSocketClient;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -20,13 +24,18 @@ public class OrderNotificationWebSocket {
static final ConcurrentHashMap<String, List<WebSocketClient>> webSocketClientMap= new ConcurrentHashMap<>();
//与某个客户端的连接会话需要通过它来给客户端发送数据
private Session session;
//连接用户id
private String userId;
/**
* 连接建立成功时触发绑定参数
* @param session 与某个客户端的连接会话需要通过它来给客户端发送数据
*/
@OnOpen
public void onOpen(Session session,@PathParam("userId") String userId){
this.userId =userId;
this.session = session;
WebSocketClient client = new WebSocketClient();
client.setSession(session);
client.setUri(session.getRequestURI().toString());
@ -78,8 +87,33 @@ public class OrderNotificationWebSocket {
}
}
/**
* 收到客户端消息后调用的方法
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
Map<String,Object> map =new HashMap<>();
map.put("code", 200);
map.put("message", "实时数据");
map.put("userId", this.userId);
Map<String, Object> data = new HashMap<>();
data.put("amountCharged","-");
data.put("gunNumber","-");
data.put("chargingOrderId","-");
data.put("soc","-");
data.put("chargingTime","-");
data.put("electricCurrent","-");
data.put("voltage", "-");
data.put("power", "-");
data.put("chargingDegree", "-");
data.put("remainingTime","-");
data.put("serialNumber","-");
data.put("parkingInstructions","在非充电情况下占用车位按照0.30元/分钟收费");
map.put("data", data);
JSONObject json = new JSONObject(map);
sendMessage(this.userId,json.toString());
}
}