我正在尝试使用 socket.io 在 python 端使用 socketIO-client 0.5.1 库在 node.js 应用程序和 python 之间进行通信。下面是两个代码块:
var http = require('http').createServer();
http.listen(6000);
var io = require('socket.io').listen(http, { log: false });
var events = require('events');
var eventEmitter = new events.EventEmitter();
var counter = 0;
io.sockets.on('connection', function (socket) {
eventEmitter.on('observe', function(message){
socket.emit('sendVoltage', {});
});
socket.on('voltage', function (data) {
console.log(counter + " - " + data.voltage);
if (counter++ < 500) {
eventEmitter.emit('observe', '');
}
});
eventEmitter.emit('observe', '');
});
#!/usr/bin/python
import time, signal, sys
from Adafruit_ADS1x15 import ADS1x15
from socketIO_client import SocketIO
ADS1115 = 0x01 # 16-bit ADC
adc = ADS1x15(ic=ADS1115)
socketIO = SocketIO('chair01', 6000)
def on_voltage_response(*args):
print 'on_voltage_response', args
def on_sendVoltage(*args):
voltage = adc.readADCSingleEnded(0, 4096, 250) / 1000
print "Emitting voltage"
socketIO.emit('voltage', {'voltage': voltage}, on_voltage_response)
socketIO.wait_for_callbacks(seconds=1)
print "Voltage emitted"
socketIO.on('sendVoltage', on_sendVoltage)
socketIO.wait(seconds=1)
当sendVoltage
检测到消息时,on_sendVoltage
函数会被执行,电压消息被发出,但控制永远不会从发出返回。下一条消息进来了,下一条等等,再也没有回来了。在某些时候,它会因递归调用过多而死亡。
我究竟做错了什么 ?