格式化消息以从 python 客户端发送到 socket.io node.js 服务器 [英] Formatting messages to send to socket.io node.js server from python client

查看:27
本文介绍了格式化消息以从 python 客户端发送到 socket.io node.js 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get a Python client talking to a Node.js server using Socket.io 0.7, by sending a custom event to the server.

基于我在 GitHub 上找到的 Socket.io 参考,以及关注 WebSocket Python 库.

Based on the Socket.io reference I have found on GitHub, and the following WebSocket Python library.

这是我目前的代码:

节点服务器

io.sockets.on('connection', function (socket) {
  socket.on('newimg', function(data) {
        console.log(data);
  });
});

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 = websocket.create_connection("ws://%s:%d/socket.io/1/websocket/%s" % (HOSTNAME, PORT, sid))
print ws.recv()
ws.send("2::")
ws.send("5:1::{'name':'newimg', 'args':'bla'}")
print ws.recv()
print "Closing connection"
ws.close()

节点控制台输出

debug - client authorized
info  - handshake authorized 12738935571241622933
debug - setting request GET /socket.io/1/websocket/12738935571241622933
debug - set heartbeat interval for client 12738935571241622933
debug - client authorized for 
debug - websocket writing 1::
debug - websocket received data packet 2::
debug - got heartbeat packet
debug - websocket received data packet 5:1::{'name':'newimg', 'args':'bla'}
debug - acknowledging packet automatically
debug - websocket writing 6:::1
info  - transport end
debug - set close timeout for client 12738935571241622933
debug - cleared close timeout for client 12738935571241622933
debug - cleared heartbeat interval for client 12738935571241622933
debug - discarding transport 

Python 控制台输出

Done
1::
6:::1
Closing connection

现在似乎没有触发套接字事件,尽管服务器以 ACK 响应.所以消息被正确接收,但我假设,socket.io 的格式不正确以触发事件.

Now it seems the socket event is not being triggered, despite the server responding with ACK. So the message is being correctly received but I am assuming, not formatted properly for socket.io to trigger an event.

我不认为框架是必要的,但是 Archie1986 似乎不同意他对此的回应:Python 中的 Socket.IO 客户端库

I didn't think framing was necessary, however Archie1986 seems to disagree on his response to this: Socket.IO Client Library in Python

我可能做错了什么?

推荐答案

已解决.我需要使用双引号.单引号不是有效的 JSON.呜呜呜.

Resolved. I needed to use double quotes. Single quotes are not valid JSON. Woops.

ws.send("5:1::{'name':'newimg', 'args':'bla'}")

变成:

ws.send('5:1::{"name":"newimg", "args":"bla"}')

这篇关于格式化消息以从 python 客户端发送到 socket.io node.js 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆