Python Socket.io客户端因消息“收到类型1的意外数据包”而暂停

时间:2019-03-11 10:01:39

标签: python node.js socket.io raspberry-pi

我的目标是通过服务器在多个树莓派之间进行实时通信。我需要服务器才能实时访问日志记录。

我正在使用here中的python Socket.io库连接到NodeJS Express / socket.io服务器。最终,我打算在一台实际的服务器上运行NodeJS服务器,但是我目前正在一个树莓派3上运行该服务器和客户端。

我已经设法使通信双向进行,但是经过几分钟的不活动(在有数据流的情况下不会发生),python脚本将记录“接收到的类型为1的意外数据包”,并且完全停止工作。我已经使用常规的html / js客户端测试了该连接,但这种情况并未发生。

我还使用python socket.io库提供的后台任务功能来侦听Pi的GPIO引脚上的按钮按下情况。

任何想法可能导致进程停止吗? “断开连接”事件不会触发。我一直在搜索消息,但是到目前为止还没有发现任何东西,但是我也是Python的新手...

我认为这与多个流程有关吗?当我注释掉start_background_task(buttons)行时,这似乎没有发生。

谢谢!!我在客户端python上找不到关于SocketIO的更多信息。

app.js(nodeJS)

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(client) {
    console.log('Client connected...');
    client.on('button', function(data){
      console.log(data);
    });
});

server.listen(4200);

client.py

import socketio
import RPi.GPIO as GPIO

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

sio = socketio.Client()

@sio.on('connect')
def on_connect():
    print('connected to server')

@sio.on('disconnect')
def on_disconnect():
    print('disconnected from server')

def buttons():
    while True:
        if GPIO.input(10) == GPIO.HIGH:
            print("Button was pushed!")
            sio.emit('button', {"pi":1, "button": 1})

if __name__ == '__main__':
    sio.connect('http://localhost:4200')
    sio.start_background_task(buttons)

编辑:修复了重复的函数名称。结果没有变化。

0 个答案:

没有答案