Java 11中的WebSocket实现

时间:2019-06-27 21:36:54

标签: java websocket java-11 openjdk-11

因此,我有以下代码可用于连接到套接字。 但是我试图从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 ?
    }
}

感谢您的帮助。

0 个答案:

没有答案