相关疑难解决方法(0)

格式化消息以从python客户端发送到socket.io node.js服务器

我正在尝试通过向服务器发送自定义事件,使用Socket.io 0.7让Python客户端与Node.js服务器通信.

基于我在GitHub上找到的Socket.io引用,以及下面的WebSocket Python库.

到目前为止,这是我的代码:

节点服务器

io.sockets.on('connection', function (socket) {
  socket.on('newimg', function(data) {
        console.log(data);
  });
});
Run Code Online (Sandbox Code Playgroud)

Python客户端

def handshake(host, port):
    u = urlopen("http://%s:%d/socket.io/1/" % (host, port))
    if u.getcode() == 200:
        response = u.readline()
        (sid, hbtimeout, ctimeout, supported) = response.split(":")
        supportedlist = supported.split(",")
        if "websocket" in supportedlist:
            return (sid, hbtimeout, ctimeout)
        else:
            raise TransportException()
    else:
        raise InvalidResponseException()


try:
    (sid, hbtimeout, ctimeout) = handshake(HOSTNAME, PORT) #handshaking according to socket.io spec.
Except Exception as e:
    print e
    sys.exit(1)
ws …
Run Code Online (Sandbox Code Playgroud)

python websocket node.js socket.io

20
推荐指数
2
解决办法
2万
查看次数

标签 统计

node.js ×1

python ×1

socket.io ×1

websocket ×1