B4J Question [SOLVED] Simple WebSockets Client implementation issue

aminoacid

Active Member
Licensed User
Longtime User
I have implemented a very simple websockets client to connect to a server which delivers a JSON string every minute. I have been using the following sub (based on Erel's MQTT Connect & Reconnect sub posted elsewhere) and it works fine for a few days, but once-in-a-while the application will hang and become completely unresponsive, requiring a restart.


B4X:
Sub ws_ConnectAndReconnect
    ws.Initialize("ws")
    Do While working
        If ws.Connected Then ws.Close
        ws.Connect(wsURL)
        wait for ws_Connected
        Sleep(5)
        If ws.Connected Then
            ws.SendText(tmsg)
            Do While working And ws.Connected
                Sleep(5000)
            Loop
        Else
            Log("Cannot Connect")
        End If
        ws.Close
        Sleep(15000)
    Loop
End Sub

The example application here: https://www.b4x.com/android/forum/threads/jwebsocketclient-library.40985/#content
uses a "WebSocketHandler" class to connect to the server, but I did not feel it was necessary to use a separate class just to process a simple one-way feed from the server. Somewhere in the forum I read that it was necessary to use this class but I can't understand why one cannot directly use the library functions like I have done above, especially since it does work (at least for a while). Can anyone confirm that it is necessary to use a WebSocketHandler class even for a simple application like mine?
Thanks!
 
Solution
Try the attached library (v1.12). Use SendTextAsync instead of SendText.

Hi Erel,
The linux boxes have been running for more than a week now with no issues. I would say that using the updated Library with SendTextAsync fixed the issue. Thanks for your help!

aminoacid

Active Member
Licensed User
Longtime User
Are you running it in release mode? UI app or non-UI?
Are you monitoring the logs?

You can check the program state with jcmd or jstack when the program hangs.

Yes, I am running a Non-UI application in release mode on a Linux box. I am monitoring the logs and for troubleshooting purposes, created an independent 1-minute timer that displays a message on the Log every minute. The application will sometimes run for several days with no issues and then hang. The log shows that the timer has stopped and there have been several ws disconnects and reconnects. That's why I am suspecting my ConnectAndReconnect sub and am wondering whether it is necessary to handle it using the code you use in your "WebSocketHandler" class instead.
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Erel, It finally happened again and as per your suggestion, I ran "jstack -F -l <pid>" to connect to a hung process. This is what I got.

B4X:
luser@wais:~$ sudo /opt/jdk/jdk1.8.0_241/bin/jstack -F -l 9598
Attaching to process ID 9598, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 25.241-b07
Deadlock Detection:

No deadlocks found.

However running "jstack -l <pid>" to connect to a running process, gives me a stack dump, which I have uploaded to this message (jstack.txt).

I am running Java Version 1.8.0_241-b07.

As mentioned earlier, the 1-minute "debug" timer that I added to the code is enabled at App start and simply displays a log message every minute. There is No code to disable the timer anywhere in the program, but yet the logs show that the timer has also stopped when the program "hangs". That's what is so puzzling about this issue and leads me to conclude that the program has hung.

I'll leave the program in this state without restarting until I hear from you.

Thanks for your help.
 

Attachments

  • jstack.txt
    17.2 KB · Views: 70
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Try the attached library (v1.12). Use SendTextAsync instead of SendText.

Done... I have this running on 10 Linux Boxes, but sometimes it takes a few days for the issue to manifest itself. I'll let you know. Thanks again for your help on this.
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Try the attached library (v1.12). Use SendTextAsync instead of SendText.

Hi Erel,
The linux boxes have been running for more than a week now with no issues. I would say that using the updated Library with SendTextAsync fixed the issue. Thanks for your help!
 
Upvote 0
Solution
Top