我将使用socket.io与node.js服务器进行客户端python。 我找到了socketIO-client,看起来不错。
当我启动我的客户端时,他从服务器接收消息(连接了多少客户端)。 但几秒钟后,就会出现异常:
(slevin@localhost 52)./test.py There is 2 client currently connected Traceback (most recent call last): File "./test.py", line 9, in <module>
socketIO.wait() File "/usr/lib/python2.7/site-packages/socketIO_client/__init__.py", line 175, in wait
self._process_events() File "/usr/lib/python2.7/site-packages/socketIO_client/__init__.py", line 192, in _process_events
for packet in self._transport.recv_packet(): File "/usr/lib/python2.7/site-packages/socketIO_client/transports.py", line 80, in recv_packet
for packet_text in self.recv(): File "/usr/lib/python2.7/site-packages/socketIO_client/transports.py", line 150, in recv
yield self._connection.recv() File "/usr/lib/python2.7/site-packages/websocket.py", line 596, in recv
opcode, data = self.recv_data() File "/usr/lib/python2.7/site-packages/websocket.py", line 606, in recv_data
frame = self.recv_frame() File "/usr/lib/python2.7/site-packages/websocket.py", line 637, in recv_frame
self._frame_header = self._recv_strict(2) File "/usr/lib/python2.7/site-packages/websocket.py", line 746, in
_recv_strict
bytes = self._recv(shortage) File "/usr/lib/python2.7/site-packages/websocket.py", line 732, in _recv
raise WebSocketTimeoutException(e.message) websocket.WebSocketTimeoutException: timed out
我没有找到引发此异常的地方。
备注:如果我连接了很多客户端,没有问题,所以我认为“socketIO.wait()”就是问题所在。为什么我的客户超时?
这是我的客户代码:
#!/usr/bin/python
from socketIO_client import SocketIO
def nbclient_update(*args):
print 'There is %d client currently connected' % (args[0])
with SocketIO('localhost', 80) as socketIO:
socketIO.on('nbClient', nbclient_update);
socketIO.wait()
这是我的服务器代码:
var express = require('express');
var http = require('http');
var fs = require('fs');
var app = express();
var nbClient = 0;
app.get('/', function(req, res) {
fs.readFile('./index.html', 'utf-8', function(error, content) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
// Server load
var server = http.createServer(app);
// Socket.io load
var io = require('socket.io').listen(server);
io.set('destroy upgrade', false);
io.sockets.on('connection', function (socket) {
++nbClient;
socket.emit('nbClient', nbClient);
socket.broadcast.emit('nbClient', nbClient);
socket.on('disconnect', function() {
--nbClient;
socket.broadcast.emit('nbClient', nbClient);
});
});
server.listen(80, "0.0.0.0");
Cound很高兴你有个主意。 非常感谢。