Open WebSocket connection with authentication cookie

前端 未结 1 1563
独厮守ぢ
独厮守ぢ 2021-01-06 10:27

I have the same issue as Websockets and cookies in Android, and I have been trying to solve it as the first comment suggested,

WebSocketClient( URI se

相关标签:
1条回答
  • 2021-01-06 11:14

    So I actually managed to solve it, and it turned out that it was not the cookie that was the actual issue, but rather that the websocket is not initialized with a valid sslcontext. This was solved rather easily by:

    WebSocketOrderClient webSocketOrderClient = new WebSocketOrderClient(uri, new Draft_17(), cmap, TIMEOUT);
    SSLContext sslContext = null;
    sslContext = SSLContext.getInstance( "TLS" );
    sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
    
    webSocketOrderClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
    webSocketOrderClient.connectBlocking();
    

    with WebSocketOrderClient:

    private class WebSocketOrderClient extends WebSocketClient {
        public WebSocketOrderClient( URI serverUri, Draft draft, Map<String, String> headers, int timeout) {
            super( serverUri, draft, headers, timeout );
        }
        @Override
        public void onOpen( ServerHandshake handshakedata ) {
            Log.w("connected", "true");
        }
        @Override
        public void onMessage( String message ) {
            Log.w( "got: ", message );
        }
        @Override
        public void onClose( int code, String reason, boolean remote ) {
            Log.w( "Disconnected", ""+code  );
        }
        @Override
        public void onError( Exception ex ) {
            ex.printStackTrace();
        }
    }
    

    Hope this helps anyone who might run into this problem in the future.

    0 讨论(0)
提交回复
热议问题