B4J Question Why Doesn't This Websocket Connect?

cklester

Well-Known Member
Licensed User
I think this should connect. I've tested the URL with an online websocket connection tester, and the connection works there.

B4X:
Private Sub StartWebsocket
    wsc.Initialize("wsc")
    wsc.Connect("wss://ws-feed.exchange.coinbase.com")
    Wait For wsc_Connected
    Log("Websocket connected!")
End Sub

It never gets to the wsc_Connected event.

The websocket docs are here. I don't know what I'm missing...

Can anybody get this to work?
 

LucaMs

Expert
Licensed User
Longtime User
1639378005461.png


Use this very useful contextual help.
 
Upvote 0

cklester

Well-Known Member
Licensed User
Might I have different modules than you?

Here's mine for a very simple B4XPages app (see attached) to test this:

1639445981969.png


And here's the error log I get:

Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
2021-12-13 20:40:43.039:INFO::JavaFX Application Thread: Logging initialized @4508ms to org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:515)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:193)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:185)
at org.eclipse.jetty.client.HttpReceiver.terminateResponse(HttpReceiver.java:464)
at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:545)
at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:156)
at org.eclipse.jetty.client.HttpSender.terminateRequest(HttpSender.java:381)
at org.eclipse.jetty.client.HttpSender.abort(HttpSender.java:566)
at org.eclipse.jetty.client.HttpSender.anyToFailure(HttpSender.java:350)
at org.eclipse.jetty.client.HttpSender$CommitCallback.failed(HttpSender.java:717)
at org.eclipse.jetty.client.http.HttpSenderOverHTTP$HeadersCallback.failed(HttpSenderOverHTTP.java:310)
at org.eclipse.jetty.io.WriteFlusher$PendingState.fail(WriteFlusher.java:263)
at org.eclipse.jetty.io.WriteFlusher.onFail(WriteFlusher.java:516)
at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:532)
at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:530)
at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:394)
at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:121)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.nio.channels.ClosedChannelException
... 8 more
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:522)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:193)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:185)
at org.eclipse.jetty.client.HttpReceiver.terminateResponse(HttpReceiver.java:464)
at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:545)
at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:156)
at org.eclipse.jetty.client.HttpSender.terminateRequest(HttpSender.java:381)
at org.eclipse.jetty.client.HttpSender.abort(HttpSender.java:566)
at org.eclipse.jetty.client.HttpSender.anyToFailure(HttpSender.java:350)
at org.eclipse.jetty.client.HttpSender$CommitCallback.failed(HttpSender.java:717)
at org.eclipse.jetty.client.http.HttpSenderOverHTTP$HeadersCallback.failed(HttpSenderOverHTTP.java:310)
at org.eclipse.jetty.io.WriteFlusher$PendingState.fail(WriteFlusher.java:263)
at org.eclipse.jetty.io.WriteFlusher.onFail(WriteFlusher.java:516)
at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:532)
at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:530)
at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:394)
at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:121)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
at java.base/java.lang.Thread.run(Thread.java:834)
java.util.concurrent.ExecutionException: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1999)
at anywhersoftware.b4j.objects.WebSocketClientWrapper.Close(WebSocketClientWrapper.java:102)
at anywhersoftware.b4j.objects.WebSocketClientWrapper$WSHandler.onWebSocketError(WebSocketClientWrapper.java:151)
at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onError(JettyListenerEventDriver.java:112)
at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.incomingError(AbstractEventDriver.java:96)
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.handleException(WebSocketUpgradeRequest.java:528)
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:522)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:193)
at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:185)
at org.eclipse.jetty.client.HttpReceiver.terminateResponse(HttpReceiver.java:464)
at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:545)
at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:156)
at org.eclipse.jetty.client.HttpSender.terminateRequest(HttpSender.java:381)
at org.eclipse.jetty.client.HttpSender.abort(HttpSender.java:566)
at org.eclipse.jetty.client.HttpSender.anyToFailure(HttpSender.java:350)
at org.eclipse.jetty.client.HttpSender$CommitCallback.failed(HttpSender.java:717)
at org.eclipse.jetty.client.http.HttpSenderOverHTTP$HeadersCallback.failed(HttpSenderOverHTTP.java:310)
at org.eclipse.jetty.io.WriteFlusher$PendingState.fail(WriteFlusher.java:263)
at org.eclipse.jetty.io.WriteFlusher.onFail(WriteFlusher.java:516)
at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:532)
at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:530)
at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:394)
at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:121)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:515)
... 20 more
Caused by: java.nio.channels.ClosedChannelException
... 8 more
 

Attachments

  • test-websocket-to-coinbase.zip
    8.7 KB · Views: 208
Upvote 0

cklester

Well-Known Member
Licensed User
Yours works too. Check your firewall(s).

Crazy thing is, I can connect to a different websocket from another provider.

This one requires you to get a connection token first, but it's generally all the same:

B4X:
Public Sub WebSocketConnect As ResumableSub
    wait for (Main.Shrimpy.GetWebsocketToken) complete(result As String)
    If result.Contains("SocketTimeoutException: timeout") Then
        Return False
    End If
    Private wst As String = result.As(JSON).ToMap.Get("token")
    Main.WebSocket.Initialize("ws")
    Dim wsurl As String = "wss://ws-feed.shrimpy.io?token=" & wst
    Main.WebSocket.Connect(wsurl)
    wait for ws_Connected
    Log("Websocket connected")
    Return True
End Sub

That works for me! I can also subscribe and start streaming data. ?
 
Upvote 0

cklester

Well-Known Member
Licensed User
if that is so, then your ip could be banned.

I can test the websocket from a browser, so I think that means my IP is not banned.

So now I'm stuck. I don't understand why I can connect via websocket to one site but not to these two others.

If anybody has any other clues or hints for me, please let me know. I will slowly be pulling my hair out in the meantime.
 
Upvote 0

cklester

Well-Known Member
Licensed User
Just got this to connect: wss://quadency.com/api/v1/public/ws in my sample app (above), but it's not working in my main app.

At least that's progress!
 
Upvote 0
Top