python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.06k stars 504 forks source link

websocket over stomp received Session closed error #1474

Open chj113 opened 3 days ago

chj113 commented 3 days ago

I connected springboot websocket server by python client(javascript client stomp.js on chrome is ok), but receive Session closed ERROR...,I need help,thanks..

python:3.8 websockets:13.0

python log

DEBUG:websockets.client:> GET /websocket HTTP/1.1
DEBUG:websockets.client:> Host: xxx.com
DEBUG:websockets.client:> Upgrade: websocket
DEBUG:websockets.client:> Connection: Upgrade
DEBUG:websockets.client:> Sec-WebSocket-Key: jAjJP5WRdLlo2EVBLgrncQ==
DEBUG:websockets.client:> Sec-WebSocket-Version: 13
DEBUG:websockets.client:> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
DEBUG:websockets.client:> User-Agent: Python/3.8 websockets/12.0
DEBUG:websockets.client:< HTTP/1.1 101
DEBUG:websockets.client:< Server: nginx
DEBUG:websockets.client:< Date: Fri, 28 Jun 2024 09:39:51 GMT
DEBUG:websockets.client:< Connection: upgrade
DEBUG:websockets.client:< Vary: Origin
DEBUG:websockets.client:< Vary: Access-Control-Request-Method
DEBUG:websockets.client:< Vary: Access-Control-Request-Headers
DEBUG:websockets.client:< Upgrade: websocket
DEBUG:websockets.client:< Sec-WebSocket-Accept: j7eoOgoDCJ5Zw9lP7dt5vKB1ff0=
DEBUG:websockets.client:< Sec-WebSocket-Extensions: permessage-deflate;client_max_window_bits=15
DEBUG:websockets.client:< X-Content-Type-Options: nosniff
DEBUG:websockets.client:< X-XSS-Protection: 1; mode=block
DEBUG:websockets.client:= connection is OPEN
DEBUG:websockets.client:> TEXT 'CONNECT\naccept-version:1.2,1.1,1.0\nheart-beat...Z5Zj_dHBLqaenQg\n\n\x00' [258 bytes]
DEBUG:websockets.client:< TEXT 'CONNECTED\nversion:1.2\nheart-beat:10000,4000\n...-111.222.111.222\n\n\x00' [91 bytes]
Connected to STOMP server
DEBUG:websockets.client:% sending keepalive ping
DEBUG:websockets.client:> PING 7e 7e 1d 92 [binary, 4 bytes]
DEBUG:websockets.client:< PONG 7e 7e 1d 92 [binary, 4 bytes]
DEBUG:websockets.client:% received keepalive pong
DEBUG:websockets.client:% sending keepalive ping
DEBUG:websockets.client:> PING c4 f5 ce 9a [binary, 4 bytes]
DEBUG:websockets.client:< TEXT '\n' [1 byte]
DEBUG:websockets.client:< PONG c4 f5 ce 9a [binary, 4 bytes]
<<<

DEBUG:websockets.client:% received keepalive pong
DEBUG:websockets.client:< TEXT 'ERROR\nmessage:Session closed.\ncontent-length:0\n\n\x00' [49 bytes]
DEBUG:websockets.client:< TEXT '\n' [1 byte]
DEBUG:websockets.client:< CLOSE 1002 (protocol error) [2 bytes]
DEBUG:websockets.client:= connection is CLOSING
DEBUG:websockets.client:> CLOSE 1002 (protocol error) [2 bytes]
<<< ERROR
message:Session closed.
content-length:0

python code

import logging
import websockets
from websockets.legacy.client import connect

logging.basicConfig()
logger = logging.getLogger('websockets')
logger.setLevel(logging.DEBUG)

websockets_logger = logging.getLogger('websockets.protocol')
websockets_logger.setLevel(logging.DEBUG)

url = 'wss:.....'
token = '...'

async def wss_connect():
    async for websocket in connect(uri=url,ping_interval=5):
        try:
            connect_frame = (
                'CONNECT\n'
                'accept-version:1.2,1.1,1.0\n'
                'heart-beat:4000,10000\n'
                f'Authorization:{token}\n'
                '\n'
                '\x00'
            )
            await websocket.send(connect_frame)
            response = await websocket.recv()
            if response.startswith('CONNECTED'):
                print("Connected to STOMP server")
                while True:
                    message = await websocket.recv()
                    print(f"<<< {message}")
            else:
                print("Failed to connect to STOMP server")
        except websockets.ConnectionClosed as error:
            print("error", error)
            print("Connection closed, attempting to reconnect...")
            await asyncio.sleep(5)

if __name__ == "__main__":
    asyncio.run(wss_connect())

springboot server:

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(1);
        taskScheduler.setThreadNamePrefix("wss-heartbeat-thread-");
        taskScheduler.initialize();
        registry.enableSimpleBroker("/stream")
                .setTaskScheduler(taskScheduler)
                .setHeartbeatValue(new long[]{10000, 4000});
    }
aaugustin commented 2 days ago

The server is closing the connection, as shown here in the log:

DEBUG:websockets.client:< CLOSE 1002 (protocol error) [2 bytes]

Maybe the server logs say why it's closing the connection?

chj113 commented 8 hours ago

No error found in server logs.Other stomp clients like stompjs connect to the same server work well. After I turned off the server's heartbeat, this error no longer exists.but the connection closed after a few minites.

chj113 commented 6 hours ago

The server is closing the connection, as shown here in the log:

DEBUG:websockets.client:< CLOSE 1002 (protocol error) [2 bytes]

Maybe the server logs say why it's closing the connection?

server log like this: LoggingWebSocketHandlerDecorator closed with CloseStatus[code=1002, reason=null] It looks like a protocol error. https://stackoverflow.com/questions/65269411/websocket-closes-with-protocol-error-1002 Can y give me a demo that websockets works over stomp?thanks...