A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/79569082/revpi-connection-using-websockets below:

python - RevPi connection using Websockets

I am trying to connect a revpi to a windows pc to exchange input events using websockets. The server(revpi) looks as such:

@app.route('/init', methods=['GET'])
def initialize():
    rpi.io.O_Unlock.value = False
    rpi.mainloop(blocking=False)

    sio.emit("initialValues", {
        "I_Slot1": rpi.io.I_Slot1.value,
        "I_Slot2": rpi.io.I_Slot2.value,
        "I_Lock": rpi.io.I_Lock.value
    })

    rpi.io.I_Slot1.reg_event(slot_master_handler)
    rpi.io.I_Slot2.reg_event(slot_slave_handler)
    rpi.io.I_Lock.reg_event(lock_event_handler)

    return jsonify({"message": "RevPi initialized and event system running."})

def slot_master_handler(name, I_Slot1):
    print(f"{name} = {I_Slot1}")
    sio.emit("master", {name: I_Slot1})

def slot_slave_handler(name, I_Slot2):
    print(f"{name} = {I_Slot2}")
    sio.emit("slave", {name: I_Slot2}) 

and the client(windows) as such:

def __init__(self, procImage):
        
        
        ...

        # register events
        self.sio = socketio.Client()
        self.register()
        self.start()
        self._send_request("init")

    def register(self):
        """Initialize the Socket.IO client and register event handlers."""

        @self.sio.event
        def connect():
            print("Connected to WebSocket server")

        @self.sio.event
        def disconnect():
            print("Disconnected from WebSocket server")

        @self.sio.on("initialValues")
        def on_initial_values(data):
            # Handle initial values using the same event methods
            print(f"received value = {data}")
            self._slotMasterEvent("I_Slot1", data["I_Slot1"])
            self._slotSlaveEvent("I_Slot2", data["I_Slot2"])
            self._lockEvent("I_Lock", data["I_Lock"])

        @self.sio.on("master")
        def on_master(data):
            print(f"[Client] Master button: {data}")
            self._slotMasterEvent("I_Slot1", data["I_Slot1"])

        @self.sio.on("slave")
        def on_slave(data):
            print(f"[Client] Slave button: {data}")
            self._slotSlaveEvent("I_Slot2", data["I_Slot2"])

        @self.sio.on("lock")
        def on_lock(data):
            print(f"[Client] Lock event: {data}")
            self._lockEvent("I_Lock", data["I_Lock"])

Now here's the problem: the initial values get logged on the client, but when I change a value after that, the server sends the message but the client never receives it.

Server:


emitting event "initialValues" to all [/]
anWfkgucTrjUfcgIAAAA: Sending packet MESSAGE data 2["initialValues",{"I_Slot1":false,"I_Slot2":false,"I_Lock":false}]
192.168.4.101 - - [11/Apr/2025 16:09:59] "GET /init HTTP/1.1" 200 195 0.006046
I_Slot1 = True
emitting event "master" to all [/]
anWfkgucTrjUfcgIAAAA: Sending packet MESSAGE data 2["master",{"I_Slot1":true}]
I_Slot1 = False
emitting event "master" to all [/]
anWfkgucTrjUfcgIAAAA: Sending packet MESSAGE data 2["master",{"I_Slot1":false}]
I_Slot1 = True
emitting event "master" to all [/]
anWfkgucTrjUfcgIAAAA: Sending packet MESSAGE data 2["master",{"I_Slot1":true}]

Client:


received value = {'I_Slot1': True, 'I_Slot2': False, 'I_Lock': False}
I_Slot1=True
I_Slot2=False
I_Lock=False

#### The change should show here ####

Feels like im doing something wrong on the client side.. any ideas? Thanks.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4