服务器未向客户端广播

时间:2019-07-08 22:45:04

标签: python websocket

我正在尝试使用WebSocket broadcast to all clients using Python答案中提供的代码向所有连接的客户端广播消息,但是该代码似乎从未进入for循环。我没有任何错误,但是即使有连接的客户端,LAN上也没有任何问题。

        print()
        print("1 -", ReturnStatus)
        if ReturnStatus:
            print("2 - Sending Data", MessageText)
            for client in self.server.connections.itervalues():
                print("3 -", client, MessageText, self.address)
                client.sendMessage(MessageText)

来自服务器的标准输出:

1 - True
2 - Sending Data Line, 4, Heat

1 - True
2 - Sending Data Control,  3

1 - True
2 - Sending Data Control,  2

1 - True
2 - Sending Data Control,  3

1 - True
2 - Sending Data Line, 3, Remote

1 - True
2 - Sending Data Line, 3, Outside

请注意,for循环中没有打印值。

附加说明:我在try块中输入了以下结果:

            try:
                for client in clients:
                    print(client)
            except Exception as e:
                print("Exception {}".format(e))

结果:

Exception name 'clients' is not defined

然后:

            try:
                for client in self.server.connections.itervalues():
                    client.sendMessage(MessageText)

结果:

Exception 'dict' object has no attribute 'itervalues'

值得注意的是Python版本是3.5.3。

1 个答案:

答案 0 :(得分:0)

我找到了答案。显然,Python3已删除itervalues。这是工作代码:

            for client in self.server.connections.values():
                client.send_message(MessageText)