Python“pywbsocket” HTML5 websocket 服务器配置问题?

发布于 2024-12-09 03:08:58 字数 514 浏览 0 评论 0原文

您好,我正在尝试使用 python 的 pywebsocket HTML5 服务器,其中我使用给定的示例文件来回显收到的任何内容。

def web_socket_transfer_data(request):
    count = 0
    while (count < 1):
    line = request.ws_stream.receive_message()
    print line
    request.ws_stream.send_message(line)  

它在一个网页上运行良好,但如果我使用两个客户端(两个网页通过相同端口连接到同一套接字服务器),该脚本的工作原理是相同的。

我想做的是让套接字服务器广播的那些消息对于任何回显的消息都应该由两个古老的网页监听。但不幸的是它不起作用。我很困惑,因为两个页面都在监听同一个套接字,那么为什么它不起作用。

是否需要做任何解决方法或修改,以便我可以使套接字服务器传输消息或广播到所有连接的客户端。

请帮忙...

Hello I am trying to use python's pywebsocket HTML5 server, where i use the given example file which echos back whatever is received.

def web_socket_transfer_data(request):
    count = 0
    while (count < 1):
    line = request.ws_stream.receive_message()
    print line
    request.ws_stream.send_message(line)  

It works fine with one webpage, but if i use two clients (two webpages connection to the same socket server over same ports), the script works the same.

What i am trying to do is to have those messages broadcasted by the socket server of for any message echoed back, should be listened by both cient webpages. But unforutnally it does not work. I am confused as both pages are listening over same socket, then why is it not working.

Is there any workaround or modification i need to do so that i can make the socket server transfer message or broadcast to all its connected clients.

Please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-12-16 03:08:58

除多播套接字外,所有连接都是 1:1 连接。您必须在某处维护打开的套接字列表,并将消息发送给每个套接字。创建套接字时连接到的端口相同仅仅意味着每个客户端连接到相同的服务。

下面介绍了如何将每个连接添加到列表中并向所有连接广播。当然,我强烈建议使用代码来处理死连接和/或关闭的连接,并将它们从列表中删除,我只是在这里讨论一般想法(特别是因为我已经用 Python 编写了一些代码)我将此代码片段基于 pywbsocket 示例代码,这就是它与您的不同的原因。请注意,我根本没有考虑线程安全,这应该是一个问题。

connections = []

def web_socket_do_extra_handshake(request):
    connections.append(request);
    pass

def web_socket_transfer_data(request):
    while True:
        line = msgutil.receive_message(request)
        for connection in connections[:]:
            msgutil.send_message(connection, line)
        if line == "Bye":
            return

With the exception of multicast sockets, all are 1:1 connections. You have to maintain the list of open sockets somewhere, and send the message to each of them. The port you connect to when creating the socket being the same merely means each client is connected to the same service.

Here's how you might add each connection to a list and broadcast to all of them. Of course, I'd strongly recommend having code to handle dead and/or closed connections and remove them from the list as well, I'm just going for the general idea here (especially since I haven't coded in Python for a few years. I based this code snippet on the pywbsocket example code, which is why it's different from yours. Note that I haven't considered thread safety at all either, which should be a concern)

connections = []

def web_socket_do_extra_handshake(request):
    connections.append(request);
    pass

def web_socket_transfer_data(request):
    while True:
        line = msgutil.receive_message(request)
        for connection in connections[:]:
            msgutil.send_message(connection, line)
        if line == "Bye":
            return
最舍不得你 2024-12-16 03:08:58

我认为,问题在于连接变量在每次新调用时都会重置。

因此,我更改了代码以仅在尚未定义的情况下初始化连接变量:

try:
    connections
except: 
    connections = []

现在它应该可以工作了。

The problem is that the connection variable is reset on every new call, I think.

So I changed the code to initialize the connections variable only if it's not defined yet:

try:
    connections
except: 
    connections = []

Now it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文