1

I am making use of websocket to connect to a monitor on LAN. I am using the following code to connect to the ip 192.168.0.1 on the port 5000:

const socket = new WebSocket("ws://192.168.0.1:5000", ['soap', 'xmpp']);

This is an image of the packets sends and received from 192.168.0.1

wireshark connection

In the first two we have a request sent and the ACK for that request, but the WebSocket is failing to read the ACK and thus results in a timeout error after 4 minutes(as shown in the last three lines).

WebSocket connection to 'ws://192.168.0.1:5000/' failed: WebSocket opening handshake timed out

I have tried the HW-group application and it worked fine. Here is an image of the packets sent and received from the application:

enter image description here

(The first three lines are for disconnecting and the last three are connecting)

I have no idea why my WebSocket is resulting in a timeout error and the socket from the other app is connecting normally. If anyone have any input on this situation please let me know.

Elias Khazzaka
  • 163
  • 2
  • 11
  • Did you try `let socket = new WebSocket(...)`? Not sure whether the `const` is a problem here ... – Nechoj Dec 09 '21 at 21:17
  • I tried changing it to `let socket...` but still the same. it is sending and receiving the handshake packet, as far as I can tell, but it is not registering that it is receiving the `ACK` back. – Elias Khazzaka Dec 10 '21 at 06:48
  • Did you try without the protocol specification: `new WebSocket("ws://192.168.0.1:5000");`? Any difference? You could also test your client on `https://socketsbay.com/test-websockets` – Nechoj Dec 10 '21 at 09:06
  • yes, even without the protocol specification I am getting the same problem. I also tested the client using the link you provided and the connection was successful. – Elias Khazzaka Dec 10 '21 at 10:16
  • Ok, so client side and firewall on the machine seems ok. It appears that your end-point (monitor) requires some special handling. My advice is try to research whether you can find out what the exact specification of the monitor is and what is required to connect to it. – Nechoj Dec 10 '21 at 10:31
  • The monitor is Philips display with support of SICP protocol, Model is 43BDL4050D Platform: Dragon 1.0. and as per their [documentation](https://www.keren.nl/dynamic/media/1/documents/Drivers/The%20SICP%20Commands%20Document%20V2_05%20released.pdf) `Control commands can be sent from a host controller via the RS232/Ethernet (port 5000) connection.` – Elias Khazzaka Dec 10 '21 at 10:42
  • The connection is indeed Ethernet and on the port 5000 – Elias Khazzaka Dec 10 '21 at 10:43
  • 1
    Ha! This is not a websocket protocol, but an ordinary proprietary serial protocol. Use the `py-serial` lib (https://pyserial.readthedocs.io/en/latest/shortintro.html) and try to send the required bytes, as defined in the specification. A websocket connection is initiated by a http request and then downgraded to a socket connection - completely different! – Nechoj Dec 10 '21 at 10:48
  • do you know how can I implement the `py-serial` library from my HTML page? – Elias Khazzaka Dec 10 '21 at 10:55
  • It is **not possible** to communicate from a web page via javascript to a TCP/IP socket. It is always the server that communicates. You need a server instance that delivers the html page and at the same time has the connection to the monitor. The server then is kind of proxy between html page and monitor. – Nechoj Dec 10 '21 at 10:57
  • An alternative would be to implement a server script that has two interfaces: one websocket and another serial to the monitor. The html page could connect to that proxy server, send commands via websocket, which the proxy then translates into serial commands to the monitor and vice versa. – Nechoj Dec 10 '21 at 11:00
  • For the server script I have tried to implement PHP with the `socket_create()` function. but it is entering a loop and I couldn't manage to solve it. plus using php i have to install xampp or some other software to make it run. is there something better than php that i can link from my html page and act as a server script? – Elias Khazzaka Dec 13 '21 at 09:42
  • @Nechoj do you have any suggestion on how should I approach this problem? the page is going to work locally. – Elias Khazzaka Dec 13 '21 at 12:37
  • I am not relly into php. But in general, creating a socket and serving it requires a loop that will block other operation. Therefore, you will need a thread that creates and manages the socket connection while the main server can serve the html pages as before. This contribution https://stackoverflow.com/questions/34143283/php-threads-sharing-a-central-socket-object handles the subject. – Nechoj Dec 15 '21 at 08:51
  • @Nechoj I have tried something similar to the link you gave me but it didn't work. Therefore I have considered using python to make an app that makes use of the `pyserial` you mentioned in an earlier comment. Thank you for your time. – Elias Khazzaka Dec 15 '21 at 14:45
  • In case you want to replace the php server with a python based server, tornado is a good choice. Also, if you want to keep the php server and have a second server that exposes both a websocket interface and a socket to the monitor and acts as a proxy, tornado can do this as well. – Nechoj Dec 15 '21 at 15:35
  • do you have any link for an example i can follow? also I was rebuilding the application in python because it is ending up in a loop when using php, and I didn't know how to connect my html to anything other than php. – Elias Khazzaka Dec 15 '21 at 16:13
  • Here is a simple tutorial: https://www.velotio.com/engineering-blog/python-tornado-guide Maybe you start with such a simple request handler. If that works, you can add the code to open a socket to the monitor during processing of the get-request (or post-request), send some bytes to the monitor, then close the socket again and then return the result to the http client. – Nechoj Dec 15 '21 at 16:53
  • Okay, I have two more questions, first, do I need to make the python file an executable for it to start running in the background (then when the user wants to use the page he needs to run the .exe file, this helps if he doesn't have python installed)? and how can I communicate with this file? I am sorry but I am very confused about the whole operation. – Elias Khazzaka Dec 15 '21 at 17:01
  • Well, you are adding more complexity. Normally, a Python program is started like `python my_program.py` and this procedure requires an installation of Python with the required packages being present on the computer. There is a possibility to package all required files into an executable .exe and deploy that on other computers without a Python installation. This is achieved using a tool called `py-installer`. – Nechoj Dec 15 '21 at 17:07
  • If your app just needs to run locally, why don't you program a dektop app, e.g., using PyQt5? Do you really need a web server? – Nechoj Dec 15 '21 at 17:32
  • yes the app is running locally, and no I don't really need a web server. I was just trying to look for an easy way to complete the app since I am more experienced in web. I'll take a look at PyQt5 and continue from there. I was trying to make my app using python tkinter and it is hard since it is my first time. – Elias Khazzaka Dec 15 '21 at 22:24
  • Then you might be interested in `fbs`: https://build-system.fman.io/ – Nechoj Dec 16 '21 at 08:50

0 Answers0