B4J Question Unstable websocket

kostefar

Active Member
Licensed User
Longtime User
Dear All,

Upon request by Erel, here´s a new thread about this subject, continuing this one:

https://www.b4x.com/android/forum/t...t-accepts-all-certificates.88926/#post-562603

So Erel taught me how to bypass SSL authentication, which is required for the websocket (not the one in Erels code) that I´m connected to - copied and pasted from Erels code:


B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private ws As WebSocketClient
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
 
   ws.Initialize("ws")
   'accept all trust manager
   Dim ssl As JavaObject
   ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory", Array(True))
   Dim jo As JavaObject = ws
   Dim wsc As JavaObject
   wsc.InitializeNewInstance("org.eclipse.jetty.websocket.client.WebSocketClient", Array(ssl))
   jo.SetField("wsc", wsc)
 
   ws.Connect("wss://b4x.com:51041/smiley/ws")
   Wait For ws_Connected
   Log("connected")
 
End Sub


It did indeed solve the connection problem, but at random times I get various errors, such as:


B4X:
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:513)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.nio.channels.AsynchronousCloseException
    ... 17 more
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:520)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.ExecutionException: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper.Close(WebSocketClientWrapper.java:76)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$WSHandler.onWebSocketError(WebSocketClientWrapper.java:118)
    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:526)
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:520)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:513)
    ... 28 more
Caused by: java.nio.channels.AsynchronousCloseException
    ... 17 more
ws Connecting
ws error: 0 null
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:513)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.nio.channels.AsynchronousCloseException
    ... 17 more
org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:520)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.ExecutionException: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper.Close(WebSocketClientWrapper.java:76)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$WSHandler.onWebSocketError(WebSocketClientWrapper.java:118)
    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:526)
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:520)
    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:459)
    at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:540)
    at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
    at org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
    at org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
    at org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
    at org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.abort(HttpConnectionOverHTTP.java:166)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:152)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.close(HttpConnectionOverHTTP.java:143)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.eclipse.jetty.client.AbstractConnectionPool.close(AbstractConnectionPool.java:191)
    at org.eclipse.jetty.client.DuplexConnectionPool.close(DuplexConnectionPool.java:233)
    at org.eclipse.jetty.client.HttpDestination.close(HttpDestination.java:368)
    at org.eclipse.jetty.client.HttpClient.doStop(HttpClient.java:248)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:142)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:160)
    at org.eclipse.jetty.websocket.client.WebSocketClient.doStop(WebSocketClient.java:401)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89)
    at anywhersoftware.b4j.objects.WebSocketClientWrapper$2.run(WebSocketClientWrapper.java:83)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.eclipse.jetty.websocket.api.UpgradeException: 0 null
    at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:513)
    ... 28 more
Caused by: java.nio.channels.AsynchronousCloseException
    ... 17 more
ws Connecting
ws error: 0 null
connected

or

B4X:
java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:507)
    at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:527)
    at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
    at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:331)
    at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:372)
    at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.flush(FrameFlusher.java:153)
    at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.process(FrameFlusher.java:217)
    at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241)
    at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224)
    at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:382)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:614)
    at org.eclipse.jetty.websocket.client.io.WebSocketClientConnection.outgoingFrame(WebSocketClientConnection.java:72)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onConnectionStateChange(AbstractWebSocketConnection.java:473)
    at org.eclipse.jetty.websocket.common.io.IOState.notifyStateListeners(IOState.java:184)
    at org.eclipse.jetty.websocket.common.io.IOState.onReadFailure(IOState.java:498)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:666)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:511)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:289)
    at org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:149)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
    at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
    at java.lang.Thread.run(Thread.java:745)
ws error:
ws error: WebSocket Read EOF
ws Connecting
connected

I said in the other thread that it works in b4a, but that wasn´t entirely true I found although it can run for a longer time there without crashing the app.

I´ve requested to send the project directly to Erel zipped, since there could be some stuff in there revealing private data although I don´t think so but just to be sure.

Nevertheless, it could be that Erels solution will be able to help others when this thread gets updated.
 

kostefar

Active Member
Licensed User
Longtime User
My guess is that the problem is in the WebSocket server. You should catch this error and try to reconnect.

Thanks Erel,

Sounds right, since websockets I dealt with before never gave me this sort of problems.
In my code, there´s a time that fires upon errors or timeouts:

B4X:
Sub timerws_tick

    ws.Close
    Init_Ws
  
End Sub

Sub ws_Closed (Reason As String)
  
    Log("ws error: " & Reason)
    ws.close
    timerws.Enabled = True

End Sub

Sub ws_TextMessage (Message As String)
    timerws.Enabled = False
    timerws.Enabled = True
 
    Log (Message)




End Sub

I increased its interval from 10000 to 20000, which seemed to help running the socket for a longer time.
But it´s like there´s a limit to how many times b4j accepts to do these very frequent reconnects (that I actually also see happening in chromes developer tools inspector) before the app crashes.
Also, I figured that in some cases it connects, but does not receive data and just keeps on doing that after the timeout I´ve manually set on the websocket with the timer. I found out that restarting b4j (not just the project) helps.. maybe that can also contribute to finding the culprit here.
Could it be that there´s a socket buffer that is not getting emptied or something like that?
Any good ideas?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Restarting B4J has nothing to do with it.


I don't see any code that reconnects.

Like I said, I´d be really pleased if I could send this to you directly so that you can see it all for yourself. I do understand if what I´m doing is not clear from a few parts of the project. The reconnecting is the tick event that closes the connection when triggered, and connects again.

Here´s a "censored" version of it all. Some stuff there may seem strange, unnecessary and unorthodox, which is the result of trying alot of different things to get this working:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
Dim ws As WebSocketClient
Dim timerws As Timer
    Private fx As JFX
    Private MainForm As Form
End Sub
Sub timerws_tick

    ws.Close
    Init_Ws
    
End Sub
Sub ws_TextMessage (Message As String)
    timerws.Enabled = False
    timerws.Enabled = True
    
    Log (Message)


End Sub

Sub Init_Ws
    ws.Initialize("ws")
    timerws.Enabled = True
    Dim ssl As JavaObject
    ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory", Array(True))

    Dim jo As JavaObject = ws
    Dim wsc As JavaObject
    wsc.InitializeNewInstance("org.eclipse.jetty.websocket.client.WebSocketClient", Array(ssl))
    jo.SetField("wsc", wsc)
    ws.Connect("wss://***")
    Log("ws Connecting")
    Wait For ws_Connected
    Log("connected")
    ws.SendText($"***"$)
    Wait For ws_TextMessage (Message As String)
    Log (Message)
    ws.SendText($"***"$)
    Log ("send 2")
    Sleep (100)
    ws.SendText($"***"$)
    Log ("send 3")
    Sleep (100)
    ws.SendText($"***"$)
    Log ("send 4")
    Sleep (100)
    ws.SendText($"***"$)
    Log ("send 5")
    Sleep (100)
    ws.SendText($"***"$)
    Log ("send 6")
    Sleep (100)
    ws.SendText($"***"$)
    Log ("send 7")
    Wait For ws_TextMessage (Message As String)
    Log(Message)
    timerws.Enabled = False
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    ws.Initialize("ws")
    timerws.Initialize("timerws",20000)
    Init_Ws
End Sub
Private Sub ws_Connected
    Log ("ws closed")
End Sub
Sub ws_Closed (Reason As String)
    
    Log("ws error: " & Reason)
    ws.close
    timerws.Enabled = True

End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
Log (Error.Message & " " & StackTrace)

End Sub


Thanks in advance!
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
You should add Dim ws As WebSocketClient to Init_Ws.

Also add a few seconds sleep before you try to reopen the connection.

Thanks Erel, let´s try that.

So you want me to have ws Dim´ed both in global and in Init_Ws?
Otherwise it will not be known in

B4X:
Sub timerws_tick

    ws.Close
    Init_Ws
    
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Yes. This is the correct way to create a new instance.

Thanks, I´m testing it against a b4a version of the same so I´ll see if any of them crashes and also look at how much data they can swallow per day or hour if running stable, which should a good indication of how often sockets experience problems. I´ll get back with the results in some days.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
So in case somebody´s reading this, as promised I´m back. It seemed that nomatter what I did, the websockets would be so on and off that I decided to find other ways of obtaining the data needed. I agree that this is probably the host who has a problem, and I guess the websocket client implementation was never designed to deal with a sockethost like this. There really are times where it would just get into a loop of EOF, timeouts and other kinds of error messages as a result of each attempt to connect. Often restarting the project would change this behaviour but only for a while.
The problems were there with both b4j and b4a.
 
Upvote 0
Top