简体   繁体   English

python socket.io客户端无法接收广播消息

[英]python socket.io client can't receive broadcasting messages

I building a Socket.io App. 我正在构建一个Socket.io应用程序。

This is the Node.Js Server Code, running in AWS instance: 这是在AWS实例中运行的Node.Js服务器代码:

var server = require('http').createServer();
var io = require('socket.io')(server);

io.on('connection', function(client){

    io.sockets.emit("welcome"); //This is received by everyone

    client.on('message', function(msg){

        console.log("message arrived"); //This is executed
        io.sockets.emit("welcome"); //This is not received by Python Client

    });
});
server.listen(8090);

I have different clients, running on web page with Javascript and one Python client running in my local computer. 我有不同的客户端,使用Javascript在网页上运行,而一个Python客户端在本地计算机上运行。

This is the Python client: 这是Python客户端:

from socketIO_client import SocketIO

socket_url = "http://xxxxxxxxxxxxxx.eu-central- 1.compute.amazonaws.com"

socketIO = SocketIO(socket_url, 8090, verify=False)

def welcome():
    print('welcome received')

socketIO.on('welcome', welcome)
socketIO.wait(seconds=1)

while True:
    pass

The problem: 问题:

Python client receives the "welcome" only when the socket start, but when other clients send the "message" to the server, and it re-transmit the "welcome" to all clients, the Python client does not receive it. Python客户端仅在套接字启动时才接收“欢迎”消息,但是当其他客户端将“消息”发送给服务器,并且将“欢迎消息”重新发送给所有客户端时,Python客户端不会接收到它。 The others clients receive this specific "welcome", so the problem is the Python client. 其他客户端收到此特定的“欢迎”消息,因此问题出在Python客户端。

I am using https://pypi.python.org/pypi/socketIO-client for the Python client. 我正在使用https://pypi.python.org/pypi/socketIO-client作为Python客户端。 And Socket.io npm version 1.7.2 due to this issue https://github.com/invisibleroads/socketIO-client/issues/159 以及由于此问题的Socket.io npm版本1.7.2 https://github.com/invisibleroads/socketIO-client/issues/159

I found the problem. 我发现了问题。 It was the line: 一行:

socketIO.wait(seconds=1)

SocketIO was responding only for 1 second, so everything that arrive after 1 second is ignored. SocketIO仅响应1秒,因此将忽略1秒后到达的所有内容。

socketIO.wait()

Solve the problem 解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM