Git Product home page Git Product logo

Comments (13)

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024 4

I added connectAsynchronously() method to WebSocket and onConnectError(WebSocket,WebSocketException) method to WebSocketListener, and released a new version (1.8). Below is the description about connectAsynchronously(). Asynchronous calls cannot be easier than this.


Asynchronous Opening Handshake

The simplest way to call connect() method asynchronously is to use connectAsynchronously() method. The implementation of the method creates a thread and calls connect() method in the thread. When the connect() call failed, onConnectError() of WebSocketListener would be called.
Note that onConnectError() is called only when connectAsynchronously() was used and the connect() call executed in the background thread failed. Neither direct synchronous connect() nor connect(ExecutorService) (described below) will trigger the callback method.

// Perform an opening handshake asynchronously.
ws.connectAsynchronously();

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024

Thank you for your request.

from nv-websocket-client.

vchau avatar vchau commented on July 29, 2024

+1 on this request. Otherwise, this lib looks like a very clean implementation.

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024

I added connect(ExecutorService) and connectable() methods to WebSocket class and released a new version (1.7) of nv-websocket-client. The following is an excerpt from README.md about Asynchronous Opening Handshake. If the API does not meet your expectations, let me know.


Asynchronous Opening Handshake

connect(ExecutorService) method is an asynchronous version of connect(). The method performs a WebSocket opening handshake asynchronously using the given ExecutorService.

// Prepare an ExecutorService.
ExecutorService es = Executors.newSingleThreadExecutor();

// Perform an opening handshake asynchronously.
Future<WebSocket> future = ws.connect(es);

try
{
    // Wait for the opening handshake to complete.
    future.get();
}
catch (ExecutionException e)
{
    if (e.getCause() instanceof WebSocketException)
    {
        ......
    }
}

The implementation of connect(ExecutorService) method creates a Callable<WebSocket> instance by calling connectable() method and passes the instance to submit(Callable) method of the given ExecutorService. What the implementation of call() method of the Callable instance does is just to call the synchronous connect().

from nv-websocket-client.

blunden avatar blunden commented on July 29, 2024

I will have to look some more on this later. I think we both hoped the lib would provide something like a simple connect method that would be asynchronous without the application having to create ExecutorServices etc.

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024

Okay. I see.

from nv-websocket-client.

blunden avatar blunden commented on July 29, 2024

That sounds very promising. Thanks!

from nv-websocket-client.

blunden avatar blunden commented on July 29, 2024

Reading this now I realize I might have overstated the need for it to be asynchronous. I don't mind that it locks until the connect is finished, in fact I need it to do so in most cases. The problem was that it performed the network request on the UI thread when used on Android.

I will try out the new version and see if it solves the problem.

from nv-websocket-client.

blunden avatar blunden commented on July 29, 2024

@vchau Had any chance to try this out yet?

from nv-websocket-client.

vchau avatar vchau commented on July 29, 2024

Yes, I have. We're going through testing right now but looks very promising. A couple of recommendation that I found:

  • WebSocketFactory.webcreateSocket() seems to also be doing some network activity as that also triggered the same exception as WebSocket.connect()
  • WebSocket.onError() callback can be invoked multiple times if there is network lost. Don't trigger your reconnect routine on this unless you want a flood of reconnect attempt. onDisconnected() seems better suited.
  • If you have custom TLS configurations, see issue #6

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024

Released a new version, 1.20. Socket.connect() was moved from WebSocketFactory.createSocket() to WebSocket.connect(). So, probably WebSocketFactory.createSocket() can be called in the main thread. If Android still complains about createSocket(), we have to give up calling createSocket() in the main thread. Please see #26 for details.

from nv-websocket-client.

or-else avatar or-else commented on July 29, 2024

The issue remains. It's due to name resolution. See #35

from nv-websocket-client.

TakahikoKawasaki avatar TakahikoKawasaki commented on July 29, 2024

@or-else The new version 1.22 includes the fix. Thank you.

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.