| | |
| | | @Slf4j |
| | | @ServerEndpoint("/web/WebSocket/{param}") |
| | | @Component |
| | | /** |
| | | * @Description: 用于测试的websocket |
| | | * @Param: |
| | | * @return: |
| | | * @Author: 下雨听风 |
| | | * @Date: 2020/9/30 |
| | | */ |
| | | |
| | | public class BSWebsocketServer { |
| | | |
| | | public static DeviceService deviceService; |
| | |
| | | public static SensorService sensorService; |
| | | |
| | | /** |
| | | * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 |
| | | * concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。 |
| | | */ |
| | | private static ConcurrentHashMap<String, BSWebsocketServer> webSocketMap = new ConcurrentHashMap<>(); |
| | | /** |
| | |
| | | channel.basicConsume(queue, false, new DefaultConsumer(channel) { |
| | | @Override |
| | | public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { |
| | | //对从MQ中取出的数据做转换,并且发送风速到客户端 |
| | | Map message = (Map) JSON.parse((String) JSON.parse(new String(body))); |
| | | sendWindInfo(message); |
| | | try{ |
| | | //对从MQ中取出的数据做转换,并且发送风速到客户端 |
| | | Map message = (Map) JSON.parse((String) JSON.parse(new String(body))); |
| | | sendWindInfo(message); |
| | | |
| | | //判断是否接收到客户端发送的mac,如果接收到则返回指定mac设备信息 |
| | | if(mac!=null&&(!mac.equals(0))) |
| | | sendDeviceInfo(message,deviceMap); |
| | | //判断是否接收到客户端发送的mac,如果接收到则返回指定mac设备信息 |
| | | if(mac!=null&&(!mac.equals(0))) |
| | | sendDeviceInfo(message,deviceMap); |
| | | |
| | | //手动确认 |
| | | channel.basicAck(envelope.getDeliveryTag(), true); |
| | | //手动确认 |
| | | channel.basicAck(envelope.getDeliveryTag(), true); |
| | | |
| | | //判断socket是否已经断开 |
| | | if (!webSocketMap.containsKey(accountId)) { |
| | | RabbitMQUtils.closeConnectionChannel(connection, channel); |
| | | } |
| | | //判断socket是否已经断开 |
| | | if (!webSocketMap.containsKey(accountId)) { |
| | | RabbitMQUtils.closeConnectionChannel(connection, channel); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage()); |
| | | RabbitMQUtils.closeConnectionChannel(connection, channel); |
| | | } |
| | | } |
| | | }); |
| | | } catch (IOException e) { |