创建websocket客户端调用接收另一个websocket客户端的数据

创建自己的websocket客户端

package com.gapf.app.v1.controller;

import com.gapf.entity.service.ext.RunService;
import com.gapf.entity.service.impl.ext.RunServiceImpl;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 考试
 *
 * @author wyy ws://192.168.2.182:8080/api/gapf-app/webSocket/sankeRun/
 */
@ServerEndpoint("/webSocket/sankeRun/{uwbId}")
@Component
@Slf4j
public class SnakeRunWebSocket {

  @OnOpen
  public void onOpen(Session session, @PathParam("uwbId") Long uwbId) {
    log.info("客户端连接考试: {}", session.getId());
    //    session.getAsyncRemote().sendText("1111");
    RunService ex = new RunServiceImpl();
    Thread thread =
        new Thread(
            () -> {
              ex.snakeRunWebSocket(session, uwbId);
            });
    thread.start();
  }

  /**
   * 客户端关闭
   *
   * @param session session
   */
  @OnClose
  public void onClose(Session session) {
    log.info("客户端连接断开, id为:{}", session.getId());
  }

  /**
   * 发生错误
   *
   * @param throwable e
   */
  @OnError
  public void onError(Throwable throwable) {
    throwable.printStackTrace();
  }

  /**
   * 收到客户端发来消息
   *
   * @param message 消息对象
   */
  @OnMessage
  public void onMessage(String message, Session session) {
    log.info("服务端收到客户端发来的消息: {}", message);
    // 收到消息
  }
}

调用别人的websocketke客户端接收数据返回给自己的客户端

 @SneakyThrows
  @Override
  public void snakeRunWebSocket(Session session, Long uwbId) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpResponse response;
    InputStream is = null;
    BufferedReader br = null;
    GaUwbMapper gaUwbMapper = SpringUtils.getBean(GaUwbMapper.class);
    MybatisRedisCache cache = SpringUtils.getBean(MybatisRedisCache.class);
    Object object = cache.getObject("uwb_token");
    String token = object.toString();
    GapfConfig bean = SpringUtils.getBean(GapfConfig.class);
    GaUwb uwb = gaUwbMapper.selectById(uwbId);
    String tag = uwb.getMac();
    //    String token = "0a8f3553a77ca193efb16ee07c35661e5d73ea65";
    // type|开头,以|分隔,选项值:coord(定位信息),sos(sos消息),power(电量信息),powerAlert(低电量报警),fenceAlert(围栏报警),passthrough(自定义透传),distanceAlert(距离报警),lifeStatus(心率), forceRemove(强拆报警)
    String type = "coord";
    String webUrl = "ws://track.ubitraq.com:8083/websocket/tag_+";
    String url = webUrl + "/tag_" + tag + "_2D" + token + "_type" + "%7c" + type;
    //    String url = "ws://192.168.2.41:8080/app/webSocket/sankeRun/1";
    try {
      WebSocketClient webSocketClient =
          new WebSocketClient(new URI(url), new Draft_6455()) {
            @Override
            public void onOpen(ServerHandshake handshakedata) {
              log.info("[websocket] 连接成功");
            }

            @Override
            public void onMessage(String message) {
              session.getAsyncRemote().sendText(message);
              log.info("[websocket] 收到消息={}", message);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
              log.info("[websocket] 退出连接");
            }

            @Override
            public void onError(Exception ex) {
              log.info("[websocket] 连接错误={}", ex.getMessage());
            }
          };
      webSocketClient.connect();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {

    }
  }

你可能感兴趣的:(java开发,websocket)