websocket客户端类,工具类中调用到service层去

maven

<dependency>
    <groupId>org.java-websocket</groupId>
    <artifactId>Java-WebSocket</artifactId>
    <version>1.3.5</version>
</dependency>

代码

@Slf4j
@Component
public class WESWebSocketClient extends WebSocketClient {

    @Autowired
    private WebSocketProcessor webSocketProcessorFor;

    private static WebSocketProcessor webSocketProcessor;

    @PostConstruct
    public void init() {
        webSocketProcessor = webSocketProcessorFor;
    }

    public WESWebSocketClient() throws URISyntaxException {
        super(new URI("ws://127.0.0.1:9102/ws"));
    }
public WESWebSocketClient(String url) throws URISyntaxException {
    super(new URI(url));
}

@Override
public void onOpen(ServerHandshake shake) {
    log.info("握手...");
}

@Override
public void onMessage(String paramString) {
    PbStationExtendInfo pbStationExtendInfo = JSONUtil.toBean(paramString, PbStationExtendInfo.class);
    log.info("接收到消息:"+paramString);
    webSocketProcessor.processor(pbStationExtendInfo);
}

@Override
public void onMessage(ByteBuffer bytes) {
    log.info("ByteBuffer 接收到消息:"+bytes);
    super.onMessage(bytes);
}

@Override
public void onClose(int paramInt, String paramString, boolean paramBoolean) {
    log.info("关闭...paramInt:"+paramInt+" paramString:"+paramString+" paramBoolean:"+paramBoolean);
}

@Override
public void onError(Exception e) {
    log.info("异常"+e);

}

public void sendMsg(String msg) throws InterruptedException {
    this.send(msg);
}

}

posted @ 2023-04-11 20:30  DaleyZou  阅读(83)  评论(0编辑  收藏  举报