websocket Web Socket过早终止

h6my8fg2  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(304)

我一直在尝试使用Python连接到skinport WebSocket。套接字文档位于此链接的底部。我正在使用websocket-client模块进行连接,但连接过早关闭。下面是迄今为止的代码。

import websocket

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, b, c):
    print("Connection closed")

def on_open(ws):
    print("Connection opened")
    ws.send("Hello, Server!")

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://skinport.com/saleFeed?app_id=730&currency=EUR",
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

我在控制台上获得以下跟踪/消息
--- request header --- GET /saleFeed?app_id=730&currency=EUR HTTP/1。1升级:WebSocket主机: www.example.com :G9 bSTZyU 7 TP 2SQIOZgbgg == Sec-WebSocket-版本:13连接:升级
---响应头--- HTTP/1。1 200 OK日期:Mon,10 Apr 2023 11:16:12 GMT Content-Type:text/html; charset=utf-8传输编码:分块连接:keep-alive CF-Ray:7b5a9387bebd6bfd-SIN Cache-Control:no-cache,no-store,must-revalidate,max-age=0严格运输安全:max-age=31536000;includeSubDomains;预加载通过:1.1 vegur CF-Cache-Status:动态内容安全策略:script-src 'self' 'unsafe-eval' 'unsafe-inline' https://platform.twitter.comhttps://syndication.twitter.comhttps://www.googletagmanager.comhttps://cdn.syndication.twimg.comhttps://www.google.comhttps://www.google-analytics.comhttps://maps.googleapis.comhttps://apis.google.comhttps://connect.facebook.nethttps://challenges.cloudflare.comhttps://static.cloudflareinsights.comwww.example www.example.com ;report-uri https://o298045.ingest.sentry.io/api/5193335/security/?sentry_key=98577efcbca24e6daef4a099b6611076 Expect-Ct:max-age=0源座席群集:?1 Pragma:no-cache Referrer-Policy:strict-origin-when-cross-origin X-Content-Type-Options:nosniff X-Dns-Prefetch-Control:关闭X-Download-Options:未打开X框架-选项:SAMEORIGIN X-Permitted-Cross-Domain-Policies:无X-Xss-保护:0设置Cookie:__cf_bm=kin41Kk7yGk9NxwTBggN4_Ga4scFC2391h_Ggv5rVeQ-1681125372-0-Ae23hoR65z9NdcPVYOlSk2y5PTPe9WtzqBTlWG1NvjVHoD5bac7WaPISitKfUEnLizFf45QQFxKbMurf47/I/rs=;路径=/;过期=星期一,4月23日11:46:12 GMT;domain= www.example.com ; HttpOnly; Secure Server:cloudflare --------------------握手状态200 OK -再见握手状态200 OK连接关闭
我不明白为什么没有交换消息就关闭了连接。on_message函数在此过程中不会被调用。

guz6ccqo

guz6ccqo1#

WebSocket连接端点为wss://skinport.com/socket.io/?EIO=4&transport=websocket
您还需要确保在连接后发送消息40。根据我的经验,如果你给予服务器任何意外的消息(包括on_open回调中的Hello Server!消息),服务器会关闭连接,所以只要确保模仿你在浏览器的开发工具中看到的东西。
下面是订阅列表提要的工作示例:

from time import sleep
import websocket

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, b, c):
    print("Connection closed")

def on_open(ws):
    print("Connection opened")
    ws.send("40")

    sleep(1)

    ws.send('42["saleFeedJoin",{"appid":730,"currency":"EUR","locale":"en"}]')

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://skinport.com/socket.io/?EIO=4&transport=websocket",
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

相关问题