| | |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import com.java110.core.log.LoggerFactory; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.websocket.*; |
| | |
| | | * @Version 1.0 |
| | | * add by wuxw 2020/5/25 |
| | | **/ |
| | | @ServerEndpoint("/ws/parkingArea/{paId}/{clientId}") |
| | | @ServerEndpoint("/ws/parkingArea/{boxId}/{clientId}") |
| | | @Component |
| | | public class ParkingAreaWebsocket { |
| | | |
| | |
| | | */ |
| | | private String clientId = ""; |
| | | |
| | | private String paId = ""; |
| | | private String boxId = ""; |
| | | |
| | | /** |
| | | * 连接建立成功调用的方法 |
| | | */ |
| | | @OnOpen |
| | | public void onOpen(Session session, @PathParam("clientId") String clientId, @PathParam("paId") String paId) { |
| | | public void onOpen(Session session, @PathParam("clientId") String clientId, @PathParam("boxId") String boxId) { |
| | | this.session = session; |
| | | this.clientId = clientId; |
| | | this.paId = paId; |
| | | this.boxId = boxId; |
| | | if (webSocketMap.containsKey(clientId)) { |
| | | webSocketMap.remove(clientId); |
| | | webSocketMap.put(clientId, this); |
| | |
| | | */ |
| | | @OnMessage |
| | | public void onMessage(String message, Session session) throws Exception { |
| | | logger.info("用户消息:" + paId + ",客户端:" + clientId + ",报文:" + message); |
| | | logger.info("用户消息:" + boxId + ",客户端:" + clientId + ",报文:" + message); |
| | | //可以群发消息 |
| | | //消息保存到数据库、redis |
| | | if (StringUtil.isEmpty(message)) { |
| | |
| | | /** |
| | | * 发送设备监控信息 |
| | | */ |
| | | public static void sendInfo(String message, String paId) throws IOException { |
| | | logger.info("发送消息到:" + paId + ",报文:" + message); |
| | | public static void sendInfo(String message, String boxId) throws IOException { |
| | | logger.info("发送消息到:" + boxId + ",报文:" + message); |
| | | for (ParkingAreaWebsocket server : webSocketMap.values()) { |
| | | if (paId.equals(server.paId)) { |
| | | if (boxId.equals(server.boxId)) { |
| | | webSocketMap.get(server.clientId).sendMessage(message); |
| | | } |
| | | } |