Skip to content

Messages sent are received "one behind" when using WSS. #270

Open
@agilezebra

Description

@agilezebra

ws4py==0.5.1
CherryPy==18.6.0
Python 3.9.5 / Mac OS 11.4 (M1)

Messages are received as expected when I use WS. If I switch to WSS, messages sent by the client/browser are not received by the server until after a subsequent message is sent by the client (which in turn is also delayed). Once the second message is sent, the first message is received.

I note issue #191 which appears to be similar.

Output from attached test client and server:

WS mode
Chrome console:

on open
sent: one
sent: two
sent: three
Message received:one back
Message received:two back
Message received:three back

Python output:

2021-07-04 19:46:11,986:INFO: Using select as epoll is not available
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Listening for SIGTERM.
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Listening for SIGHUP.
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Listening for SIGUSR1.
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Bus STARTING
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Starting WebSocket processing
2021-07-04 19:46:11,986:INFO: [04/Jul/2021:19:46:11] ENGINE Started monitor thread 'Autoreloader'.
2021-07-04 19:46:12,089:INFO: [04/Jul/2021:19:46:12] ENGINE Serving on http://127.0.0.1:9000
2021-07-04 19:46:12,090:INFO: [04/Jul/2021:19:46:12] ENGINE Bus STARTED
2021-07-04 19:46:24,695:INFO: in /test
2021-07-04 19:46:24,696:INFO: 127.0.0.1 - - [04/Jul/2021:19:46:24] "GET /test HTTP/1.1" 101 - "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
2021-07-04 19:46:24,696:INFO: Managing websocket [Local => 127.0.0.1:9000 | Remote => 127.0.0.1:61575]
2021-07-04 19:46:24,935:INFO: received_message:one
2021-07-04 19:46:24,936:INFO: received_message:two
2021-07-04 19:46:24,937:INFO: received_message:three

WSS mode
Chrome console:

on open
sent: one
sent: two
sent: three
Message received:one back
Message received:two back

Python output:

2021-07-04 19:49:39,967:INFO: Using select as epoll is not available
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Listening for SIGTERM.
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Listening for SIGHUP.
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Listening for SIGUSR1.
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Bus STARTING
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Starting WebSocket processing
2021-07-04 19:49:39,967:INFO: [04/Jul/2021:19:49:39] ENGINE Started monitor thread 'Autoreloader'.
2021-07-04 19:49:40,073:INFO: [04/Jul/2021:19:49:40] ENGINE Serving on https://127.0.0.1:9000
2021-07-04 19:49:40,073:INFO: [04/Jul/2021:19:49:40] ENGINE Bus STARTED
2021-07-04 19:49:56,428:INFO: in /test
2021-07-04 19:49:56,429:INFO: 127.0.0.1 - - [04/Jul/2021:19:49:56] "GET /test HTTP/1.1" 101 - "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
2021-07-04 19:49:56,430:INFO: Managing websocket [Local => 127.0.0.1:9000 | Remote => 127.0.0.1:62492]
2021-07-04 19:49:56,641:INFO: received_message:one
2021-07-04 19:49:56,642:INFO: received_message:two

test.html

<!DOCTYPE HTML>

<html>

<head>
    <script type="text/javascript">
        function webSocketTest() {
            if ("WebSocket" in window) {
                var socket = new WebSocket("wss://local.example.com:9000/test");
                //var socket = new WebSocket("ws://local.example.com:9000/test");

                function send(text) {
                    socket.send(text)
                    console.log(`sent: ${text}`)
                }

                socket.onopen = function () {
                    console.log("on open")
                    send("one");
                    send("two");
                    send("three");
                };

                socket.onmessage = function (event) {
                    var received = event.data;
                    console.log(`Message received: ${received}`);
                };
            }
            else {
                console.log("No WebSocket support");
            }
        }
    </script>
</head>

<body>
    <div>
        <input id="run" type="button" value="Upgrade" onclick="webSocketTest();" />
    </div>
</body>

</html>

test.py

#!/usr/bin/env python3

import sys
import logging

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket


class TestWebSocket(WebSocket):
    def received_message(self, message):
        message = message.data.decode('utf8')
        logging.info("received_message:%s", message)
        self.send(f"{message} back")


class Root:
    @cherrypy.expose
    def test(self):
        logging.info("in /test")


def main():
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s:%(levelname)s: %(message)s")

    config = {
        "global": {
            "server.ssl_module": "builtin",
            "server.ssl_certificate": "/etc/ssl/certs/STAR_example_com.chain.pem",
            "server.ssl_private_key": "/etc/ssl/private/STAR_example_com.key",
            "server.socket_port": 9000,
            "log.screen": False,
        },
        "/test": {
            "tools.websocket.on": True,
            "tools.websocket.handler_cls": TestWebSocket,
        }
    }

    WebSocketPlugin(cherrypy.engine).subscribe()
    cherrypy.tools.websocket = WebSocketTool()
    cherrypy.quickstart(Root(), "/", config=config)


if __name__ == "__main__":
    sys.exit(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions