Comments (13)
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.
Thank you for your request.
from nv-websocket-client.
+1 on this request. Otherwise, this lib looks like a very clean implementation.
from nv-websocket-client.
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.
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.
Okay. I see.
from nv-websocket-client.
That sounds very promising. Thanks!
from nv-websocket-client.
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.
@vchau Had any chance to try this out yet?
from nv-websocket-client.
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 asWebSocket.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.
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.
The issue remains. It's due to name resolution. See #35
from nv-websocket-client.
@or-else The new version 1.22
includes the fix. Thank you.
from nv-websocket-client.
Related Issues (20)
- getting below when i try subscribe my payload from Server sometimes
- getting below errors after socket opened and when i try to subscribe my payload from Server sometimes HOT 3
- why getListenerManager() getStateManager() and others are private ? HOT 1
- Denied app upload to PlayStore HOT 3
- How can I have a listener for each sendText Method? HOT 1
- Very misleading error message if jdk.crypto.ec module is missing
- NV WebSocket Client & Quarkus & GraalVM Native Image Problems HOT 1
- Race between WebSocketAdapter.onConnected() and .onTextMessage().
- websocket client send performance
- While multiple send, download is blocked
- -
- Crashes on API < 24 with new AGP that is needed for API 33 HOT 8
- Websocket disconnects automatically after 30 seconds HOT 3
- Handshake failure with basic Authorization
- Websocket makes two connections HOT 1
- how to get session id to send messages
- Throws Exception even if the url is valid
- Does the library use some Tracking link?
- Upgrade SHA-1 to SHA-256 in HandshakeReader
- Ability to replace threads
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nv-websocket-client.