Giter Club home page Giter Club logo

Comments (17)

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

Could you tell me the URL? I'll try to access it.

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

You see the error message "Failed to get the input stream of the raw socket" when getInputStream() of java.net.Socket failed. See the implementation of openInputStream(Socket) in WebSocket.java.

The WebSocketException instance you receive in onError method contains the IOException instance that getInputStream() threw. Please check what the exception is saying. LIke this:

@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception
{
    // Print the stack trace of the IOException.
    cause.getCause().printStackTrace();
}

from nv-websocket-client.

hackerLeiWei avatar hackerLeiWei commented on August 17, 2024

Hi TakahikoKawasaki,I also encountered this problem and this is my code:

public void initWs() throws IOException, WebSocketException,
        URISyntaxException, NoSuchAlgorithmException {
    String token = "0EE86165533E49841A063472B8639223|8baaf6b1385145545c4ec4c4c151e0aa|1444295714";
    token = URLEncoder.encode(token, "UTF-8");
    String exampleUri;
    exampleUri = "wss://yggdrasil.bz-inc.com:11506/im?token=" + token;
    // exampleUri = "wss://echo.websocket.org";
    // exampleUri = "ws://echo.websocket.org";
    WebSocketFactory webSocketFactory = new WebSocketFactory();
    webSocketFactory
            .setSSLContext(
                    SSLContext.getInstance("Default"))
            .setSSLSocketFactory(
                    (SSLSocketFactory) SSLSocketFactory.getDefault());
    ws = webSocketFactory
            .setConnectionTimeout(TIMEOUT)
            .createSocket(exampleUri)
            .setPingInterval(20 * 1000)
            .addListener(webSocketAdapter);
    ws.connect();
}

nvcreatesocket
nvhandshakebuilder
nvwebsocket

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

Please read the JavaDoc of WebSocketFactory.createSocket(URI) for details about SSL configuration. Also, see the discussion in #6. In short, if you call setSSLContext(), calling setSSLSocketFactory() has no meaning. Moreover, if you want to use the default settings, you don't have to call setSSL*() functions at all.

Please check whether SSLContext.getInstance("Default") returns a valid SSLContext instance. I'm not sure SSLContext.getInstance(String) accepts "Default" as a valid value.

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

I released a new version 1.11. Please use it to see what an additional error message says after "Failed to get the input stream of the raw socket".

The following is the CHANGES made in version 1.11.

1.11 (2015-10-12)

  • Changed some error messages of WebSocketException.
    (Appended cause.getMessage() in cases where a cause is available.)

from nv-websocket-client.

hackerLeiWei avatar hackerLeiWei commented on August 17, 2024

That is ok, thank you so much!

from nv-websocket-client.

hackerLeiWei avatar hackerLeiWei commented on August 17, 2024

yeah , now I can connect our server by version 1.11. but some urls contain '|' can`t be created URI directly,so I changed your code. My connecting code:
nvhandshakebuilder

nv_bsk

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

Oh, sorry. It's my bug. I fixed the bug and released a new version 1.12. Please try the new version.

1.12 (2015-10-12)

  • Fixed a bug in WebSocketFactory. getRawPath() and getRawQuery()
    should be used instead of getPath() and getQuery().

from nv-websocket-client.

hackerLeiWei avatar hackerLeiWei commented on August 17, 2024

That is right! It is so easy for me to connect our server .We`re using your library in our IM system and it seems very good.Of course ,If there are some other troubles bother me, I will come back to ask for your help immediately.

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

I am also getting similar error. Below is error, Can you please help
01-02 02:46:50.420 3698-4278/com.neovisionaries.ws.client W/System.err: com.neovisionaries.ws.client.WebSocketException: Failed to get the input stream of the raw socket: Connection closed by peer
01-02 02:46:50.450 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.WebSocket.openInputStream(WebSocket.java:2831)
01-02 02:46:50.478 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.WebSocket.shakeHands(WebSocket.java:2789)
01-02 02:46:50.504 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:1867)
01-02 02:46:50.531 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.EchoClient.connect(EchoClient.java:118)
01-02 02:46:50.559 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.EchoClient.connectWebSocket(EchoClient.java:66)
01-02 02:46:50.589 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.MainActivity$PerformTask.doInBackground(MainActivity.java:35)
01-02 02:46:50.620 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.MainActivity$PerformTask.doInBackground(MainActivity.java:30)
01-02 02:46:50.645 3698-4278/com.neovisionaries.ws.client W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-02 02:46:50.672 3698-4278/com.neovisionaries.ws.client W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-02 02:46:50.697 3698-4278/com.neovisionaries.ws.client W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-02 02:46:50.727 3698-4278/com.neovisionaries.ws.client W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-02 02:46:50.759 3698-4278/com.neovisionaries.ws.client W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-02 02:46:50.783 3698-4278/com.neovisionaries.ws.client W/System.err: at java.lang.Thread.run(Thread.java:818)
01-02 02:46:50.810 3698-4278/com.neovisionaries.ws.client W/System.err: Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
01-02 02:46:50.926 3698-4278/com.neovisionaries.ws.client W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
01-02 02:46:50.957 3698-4278/com.neovisionaries.ws.client W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302)
01-02 02:46:50.988 3698-4278/com.neovisionaries.ws.client W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:598)
01-02 02:46:51.019 3698-4278/com.neovisionaries.ws.client W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:560)
01-02 02:46:51.047 3698-4278/com.neovisionaries.ws.client W/System.err: at com.neovisionaries.ws.client.WebSocket.openInputStream(WebSocket.java:2824)
01-02 02:46:51.058 3698-4278/com.neovisionaries.ws.client W/System.err: ... 12 more

Below is my code. Please let me know if something is wrong
private static WebSocket connect() throws Exception {
// Create a custom SSL context.
SSLContext context = NaiveSSLContext.getInstance("TLS");

    WebSocket socket = new WebSocketFactory()
            .setSSLContext(context) // Set the custom SSL context.
            .setConnectionTimeout(TIMEOUT)
            .createSocket(SERVER)
            .addProtocol("BB-GW-01")
            .addListener(new WebSocketAdapter() {
                // A text message arrived from the server.
                public void onTextMessage(WebSocket websocket, String message) {
                    //System.out.println(message);
                    Log.d("EchoClient", message);
                }
            })
            .addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);

    try {
        return socket.connect();
    } catch (OpeningHandshakeException e) {
        // Status line.
        StatusLine sl = e.getStatusLine();
        System.out.println("=== Status Line ===");
        System.out.format("HTTP Version  = %s\n", sl.getHttpVersion());
        System.out.format("Status Code   = %d\n", sl.getStatusCode());
        System.out.format("Reason Phrase = %s\n", sl.getReasonPhrase());

        // HTTP headers.
        Map<String, List<String>> headers = e.getHeaders();
        System.out.println("=== HTTP Headers ===");
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            // Header name.
            String name = entry.getKey();

            // Values of the header.
            List<String> values = entry.getValue();

            if (values == null || values.size() == 0) {
                // Print the name only.
                System.out.println(name);
                continue;
            }

            for (String value : values) {
                // Print the name and the value.
                System.out.format("%s: %s\n", name, value);
            }
        }
        return null;
    }
}

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

The stack trace says "Connection closed by peer". You may be able to find a hint in the log messages on the server side.

In addition, what is BB-GW-01 protocol? nv-websocket-client does not support the protocol.

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

Thanks for your response.
BB-GW-01 is subprotocol in websocket.

I am attaching screen shot of wireshark if you can make out what can be problem . 192.168.50.104 is my mobile IP and 192.168.50.1 is Server IP. Server is written using libwebsocket C library and we are able to connect using iPhone to the same server using wss.

I am able to connect on ws but not on wss using nvwebsocket client library
screen shot 2016-05-24 at 2 55 35 pm

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

Also sometime I get this in wireshark but both time I get same stack trace
screen shot 2016-05-24 at 4 54 08 pm

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on August 17, 2024

I'm afraid that the root cause is in the implementation of TLS. What if you give "TLSv1.2" to NaiveSSLContext.getInstance(String) instead of "TLS"?

FYI: javax.net.ssl.SSLContext.getInstance(String)

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

It make no difference whether i give TLSv1.2 or TLS. In my scenario I am connecting to an embedded Linux based device which provide webscoket server. It had custom certificate hence I am ignoring them using NavieContext. Now there are two scenario

  1. When Embedded device and Android both are in corporate network , I am able to connect to websocket server
  2. When Android directly connect to embedded device on device's wifi access point then it give SSL_handshake error.
    I am attaching snapshot of wireshark log for both cases. In case you want actual wireshark to make nay comment then let me know.
    success case
    failurecase

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

Hi,
In continuation to my earlier issue of not albe to connect to after some more research I had found out the reason. In SSL connection android SDK an unnecessary DNS lookup which causes delay. Please see link http://stackoverflow.com/questions/4737019/tls-connection-using-sslsocket-is-slow-in-android-os

In my case it was taking around 30 seconds. After increasing the timeout at I was successfully able to connect the server. My question is how I can do same in this websocket library or override some function of library. Any input on this will be of great help.

from nv-websocket-client.

ukotdiya01 avatar ukotdiya01 commented on August 17, 2024

Now I had get solution to the above problem. When hostname is IP address InetSocketAddress constructor fill addr field and make HostName file null. During actual SSL connection it tries to get hostname via reverse DNS lookup. As I only want to create tls connection via IP so I injected hostname into InetSocketAddress I modified toInetSocketAddress() function of Address.java

InetSocketAddress toInetSocketAddress()
{
try {
InetAddress addr = InetAddress.getByName(mHost);
Log.d("InetsocketAddress call","before call");
injectHostname(addr,mHost);
return new InetSocketAddress(addr, mPort);

    }
    catch ( Exception e)
    {
        Log.d("InetsocketAddress fail", e.getMessage());

        return new InetSocketAddress(mHost, mPort);
    }

}

private void injectHostname(InetAddress address, String host) {
try {
Field field = InetAddress.class.getDeclaredField("hostName");
field.setAccessible(true);
field.set(address, host);
} catch (Exception ignored) {
}
}

from nv-websocket-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.