Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Convert web socket from JS to python

I'm trying to convert this small JavaScript websocket application to one for Python so i can run it on a raspberry but i just cant figure it out. This is how far I got so far but all I get is "connection refused". I don't have a lot of experience with websockets so I wouldn't know where to go from here

JavaScript:

let websocketClient
        try {
            websocketClient = new WebSocket("ws://127.0.0.1:15881/v2/feedbacks?app_id=123&app_name=hackvest")
        } catch (e) {
            console.log("PlayerSocket", e)
        }
        websocketClient.onopen = function() {
            console.log("open");
        };
        websocketClient.onmessage = function(e) {
            websocketClient.send(JSON.stringify({
                "Submit": [{
                    "Type": "turnOff",
                    "Key": ""
                }]
            }));
            console.log(e)
        };
        websocketClient.onclose = function(e) {
            console.log("close");
            console.log(e)
        };
        var n = JSON.stringify({
            Submit: [{
                Type: "turnOff",
                Key: ""
            }]
        })

Python:

import websocket
import _thread
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    print("open")


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.create_connection("ws://127.0.0.1:15881/v2/feedbacks?app_id=com.bhaptics.designer2&app_name=hackvest",
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close)

    ws.run_forever()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Base on websocket-client example, it would look like this:

import websocket
import _thread
import time

def on_message(ws, message):
    print(message)
    message_to_send = {
        "Submit": [{
            "Type": "turnOff",
            "Key": ""
        }]}
    ws.send(message_to_send)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    _thread.start_new_thread(run, ())

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://127.0.0.1:15881/v2/feedbacks?app_id=123&app_name=hackvest",
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close)

    ws.run_forever()

Before executing, run: pip3 install websocket-client!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!