我有一个运行线程并通过websocket发出信号的python服务器:
def read_serial():
while True:
...
socketio.emit("fire", {'n': 1}, namespace='/test')
app = Flask(__name__)
app.config['SECRET_KEY'] = '32ed21l31f9031kd93'
app.debug = True
socketio = SocketIO(app)
...
@socketio.on('connect', namespace='/test')
def test_connect():
emit('my response', {'data': 'Connected'})
...
if __name__ == '__main__':
thread = Thread(target = read_serial)
thread.start()
socketio.run(app, use_reloader=True)
在客户端,我有下一个代码:
var socket = io.connect('http://' + document.domain + ':' + location.port + "/test");
socket.on('fire', function(data){
fireTimer(data.n);
});
连接正在运行,但事件未按预期进行。