1

所以,我有以下代码用于连接到套接字。但我试图从 WebSocketListener 调用(或监听)OnText 事件,以获取从 websocket 接收的数据。任何帮助深表感谢。

public class WebSocketListener implements WebSocket.Listener { 
    @Override
    public void onOpen(WebSocket webSocket) {
        webSocket.request(1);
        _logger.trace("Connected to WebSocket.");
        _logger.debug("WebSocket Listener has been opened for requests.");
        WebSocket.Listener.super.onOpen(webSocket);
    }

    @Override
    public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
        _logger.trace("onText: ", data);
        webSocket.request(1);
        _logger.info("onText() completed.");

        JSONObject response = new JSONObject(data.toString());
        //
        // How to return it to the class requesting/listening on it?
        //

        return new CompletableFuture().newIncompleteFuture().thenAccept(System.out::println);
        //return WebSocket.Listener.super.onText(webSocket, data, last);
    }

    @Override
    public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) {
        _logger.debug("WebSocket Listener has been closed with statusCode: {}, and cause: {}",statusCode, reason);
        webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
        return new CompletableFuture<Void>();
        //return WebSocket.Listener.super.onClose(webSocket, statusCode, reason);
    }

    @Override
    public void onError(WebSocket webSocket, Throwable error) {
        _logger.error("A " + error.getCause() + " exception was thrown.");
        _logger.error("Message: " + error.getLocalizedMessage());
        webSocket.abort();
    }

    @Override
    public CompletionStage<?> onPing(WebSocket webSocket, ByteBuffer message){
        webSocket.request(1);
        _logger.trace("Ping: Client ---> Server");
        _logger.trace(message.asCharBuffer().toString());
        _logger.trace("Ping completed.");
        // return new CompletableFuture().completedFuture​("Ping completed.").thenAccept(System.out::println);
        return new CompletableFuture().newIncompleteFuture().thenAccept(System.out::println);
    }

    @Override
    public CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message){
        webSocket.request(1);
        _logger.trace("Pong: Client ---> Server");
        _logger.trace(message.asCharBuffer().toString());
        //return new CompletableFuture().completedFuture​("Pong completed.").thenAccept(System.out::println);
        _logger.trace("Pong completed.");
        return new CompletableFuture().newIncompleteFuture().thenAccept(System.out::println);
    }
}

以下是 WebSocketClient,它将处理从 WebSocket API 创建套接字、连接、重新连接、登录、注销、getSession。

private static CompletableFuture<WebSocket> _websocket_cf;
private static WebSocket _webSocket;
public WebSocketClient() {
        try {
            _secure = true;

            WebSocketListener wsListener = new WebSocketListener();
            TrustingHttpClient client = HttpUtil.getDefaultHttpClient().addTrustedHost(10.200.60.15);

            _websocket_cf = client
                    .newWebSocketBuilder()
                    .buildAsync(URI.create("wss://10.200.60.15/remoteapi"), wsListener); //
            _webSocket = _websocket_cf.join();
            _logger.info("WebSocket created.");
        }
        catch (Exception ex) {
            _logger.error("Failed to create WebSocket. ", ex);
        }
    }

    public WebSocket getWebSocket() { return _webSocket; }

.....
}

不,我想在此方法中获取从 OnText 事件接​​收到的数据。

Public class SocketExample {
    private WebSocketClient _wsc;

    private void getData(){
          _wsc = new WebSocketClient();
          WebSocket ws = _wsc.getWebSocket();

          // get the data received in OnText event here ?
    }
}

谢谢你的帮助。

4

0 回答 0