pythonnode.jswebsocket

Websockets - trying to convert Phython Code to Node


I'm trying to copy the features of this library https://github.com/rnbguy/pysphere/blob/master/misphere.py which connects via websockets to the Mi Sphere 360 camera.

The important code is here

ms_ip = '192.168.42.1'
ms_tcp_port = 7878
ms_uk_port = 8787
ms_fs_port = 50422

def init(self):
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    self.socket.connect((self.ms_ip, self.ms_tcp_port))
    self.socket_1 = socket.socket()
    self.recv_handle_live = True
    self.recv_thread = threading.Thread(target=self.recv_handler)
    self.recv_thread.daemon = True
    self.session = Session()
    self.session.conf = {}
    self.session.locks = {}
    self.last_send = 0

I'm trying to do this with the ws library in Node

const ip = '192.168.42.1'
const port = '7878'
const url = `ws://${ip}:${port}`
const websocket = new WebSocket(url)

However I'm not getting a connection to the camera. (It's connected via Wifi, but the websocket never sends a connection message)

I'm wondering if it's to do with the protocols, which are listed in the Phython code.

I see in the websocket documentation you can define protocols in the connection, but I can't find any documentation about what those protocols are and how you add them.

i.e.

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

Anyone know how I add something like this in the Node websocket connection?


Solution

  • A webSocket connection cannot be made to a plain TCP socket server.

    A plain TCP socket is not the same as a webSocket. Your Python code appears to be making a plain TCP socket connection.

    Your node.js code is attempting to make a webSocket connection. A webSocket connection can only be made to a webSocket server that speaks the webSocket protocol (which runs on top of a plain TCP socket).

    If your Python code is working, then you apparently need to make just a plain TCP socket connection which you can see how to do in the Net module.

    For further description of the differences between a plain TCP connection and a webSocket connection see these:

    Computer refuses websocket connections

    What's the difference between WebSocket and plain socket communication?

    Is there a way to do a tcp connection to an IP with javascript?

    Socket server not connect with JavaScript socket client