aminoacid Well-Known Member Licensed User Longtime User Nov 19, 2022 #1 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!
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 aminoacid Dec 2, 2022 Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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!
Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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!
Erel B4X founder Staff member Licensed User Longtime User Nov 20, 2022 #2 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. Upvote 0
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.
aminoacid Well-Known Member Licensed User Longtime User Nov 20, 2022 #3 Erel said: 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. Click to expand... 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
Erel said: 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. Click to expand... 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.
Erel B4X founder Staff member Licensed User Longtime User Nov 21, 2022 #4 Wait for it to happen again. Don't kill the process. Instead get a thread stack dump with jcmd or jstack and upload it. Upvote 0
Wait for it to happen again. Don't kill the process. Instead get a thread stack dump with jcmd or jstack and upload it.
aminoacid Well-Known Member Licensed User Longtime User Nov 22, 2022 #5 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 jstack.txt 17.2 KB · Views: 419 Upvote 0
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.
Erel B4X founder Staff member Licensed User Longtime User Nov 23, 2022 #6 Try the attached library (v1.12). Use SendTextAsync instead of SendText. Attachments jWebSocketClient.zip jWebSocketClient.zip 5.9 KB · Views: 463 Upvote 0
aminoacid Well-Known Member Licensed User Longtime User Nov 23, 2022 #7 Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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
Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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.
aminoacid Well-Known Member Licensed User Longtime User Dec 2, 2022 #8 Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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
Erel said: Try the attached library (v1.12). Use SendTextAsync instead of SendText. Click to expand... 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!