-
Notifications
You must be signed in to change notification settings - Fork 288
Messages sent are received "one behind" when using WSS. #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Same problem here. Any feedback on this? The only workaround I have is to close the WebSocket right after sending. |
The issue still exists with CherryPy 18.10.0 and ws4py 0.6.0 Here's a self-contained test script. You only need to provide your import logging
import os
import time
from threading import Thread
import cherrypy
import ws4py
from ws4py.client.threadedclient import WebSocketClient
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket
logging.basicConfig( level=logging.DEBUG, format="%(asctime)s:%(levelname)s: %(message)s" )
logger = logging.getLogger()
logger.info( f"ws4py version: {ws4py.__version__}" )
logger.info( f"cherrypy version: {cherrypy.__version__}" )
root = os.path.dirname( os.path.abspath( __file__ ) )
USE_SSL = True
HOST = "127.0.0.1"
PORT = 9876
URL = f"wss://{HOST}:{PORT}/ws" if USE_SSL else f"ws://{HOST}:{PORT}/ws"
class EchoClient( WebSocketClient ):
def opened( self ):
self.send( "Let's count!" )
self.send( 1 )
def send( self, payload, binary=False ):
logger.info( f"EchoClient send: {payload}" )
return super().send( str( payload ), binary )
def closed( self, code, reason=None ):
logger.info( ( "Closed down", code, reason ) )
def received_message( self, m ):
logger.info( f"EchoClient received_message: {m}" )
try:
number = int( str( m ).strip() )
if number < 10:
# increase number and send it back
self.send( number + 1 )
else:
self.close( 1000, "Done" )
except ( TypeError, ValueError ):
return
class BroadcastWebSocketHandler( WebSocket ):
def opened( self ):
logger.info( "WebSocket opened" )
self.send( "Hello from BroadcastWebSocketHandler" )
cherrypy.engine.publish( 'websocket-broadcast', "New client connected" )
def received_message( self, message ):
logger.info( f"BroadcastWebSocketHandler received_message: {message}" )
cherrypy.engine.publish( 'websocket-broadcast', str( message ) )
class Root:
@cherrypy.expose
def ws( self ):
pass
def wait_for_cherrypy_engine_started():
while ( cherrypy.engine.state != cherrypy.engine.states.STARTED ):
logger.info( "waiting for CherryPy engine to be STARTED" )
time.sleep( 0.5 )
logger.info( "CherryPy engine started" )
def run_echo_client():
wait_for_cherrypy_engine_started()
try:
ws = EchoClient( URL )
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
logger.info( "KeyboardInterrupt: closing WebSocket client" )
except Exception:
logger.exception( "Error in WebSocket client" )
def run_cherrypy_server():
config = {
"global" : {
"server.ssl_module" : "builtin" if USE_SSL else None,
"server.ssl_certificate" : os.path.join( root, "public.pem" ) if USE_SSL else None,
"server.ssl_private_key" : os.path.join( root, "private.pem" ) if USE_SSL else None,
"server.socket_port" : PORT,
"server.socket_host" : HOST,
"log.screen" : False,
'engine.autoreload.on' : False,
},
"/ws" : {
"tools.websocket.on" : True,
"tools.websocket.handler_cls" : BroadcastWebSocketHandler,
'tools.websocket.protocols' : [ 'some-protocol' ],
'tools.gzip.on' : False,
'tools.caching.on' : False,
'tools.sessions.on' : False,
},
}
WebSocketPlugin( cherrypy.engine ).subscribe()
cherrypy.tools.websocket = WebSocketTool()
cherrypy.quickstart( Root(), "/", config=config )
def main():
t = Thread( target=run_echo_client, daemon=True, name="WebSocketClient" )
t.start()
run_cherrypy_server()
if __name__ == "__main__":
main() Output for
Output for
... and that's it. Since the server does not receive |
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:
Python output:
WSS mode
Chrome console:
Python output:
test.html
test.py
The text was updated successfully, but these errors were encountered: