我的websocket客户端正在尝试与远程wss服务器对话,并且在此输出中失败:
[my-user@my-server]$ python my_websocket_client.py
ws-client connecting...
[Errno 111] Connection refused
conn closed
exception in main: 'NoneType' object has no attribute 'status'
ws-client connect status is not ok.
trying to reconnect
ws-client connecting...
[Errno 111] Connection refused
conn closed
exception in main 'NoneType' object has no attribute 'status'
...and只是重复了一遍又一遍。
以下是相关代码(客户端):
def on_error(ws, error):
logger.error("on error is: %s" % error)
def reconnect():
global reconnect_count
logger.warning("ws-client connect status is not ok.\ntrying to reconnect for the %d time" % reconnect_count)
reconnect_count += 1
if reconnect_count < RECONNECT_MAX_TIMES:
thread.start_new_thread(connect, ())
def on_message(ws, message):
message_json = json.loads(message)
payload = base64_decode_as_string(message_json["payload"])
# handler payload
try:
message_handler(payload)
except Exception as e:
logger.error("handler message, a business exception has occurred,e:%s" % e)
send_ack(message_json["messageId"])
def on_close(obj):
logging.critical("Connection closed!")
obj.close()
global connect_status
connect_status = 0
def connect():
logger.info("ws-client connecting...")
ws.run_forever(sslopt=SSL_OPT, ping_interval=PING_INTERVAL_SECONDS, ping_timeout=PING_TIMEOUT_SECONDS)
def send_ack(message_id):
json_str = json.dumps({"messageId": message_id})
ws.send(json_str)
def main():
header = {"Connection": "Upgrade",
"username": ACCESS_ID,
"password": gen_pwd()}
websocket.setdefaulttimeout(CONNECT_TIMEOUT_SECONDS)
global ws
ws = websocket.WebSocketApp(get_topic_url(),
header=header,
on_message=on_message,
on_error=on_error,
on_close=on_close)
thread.start_new_thread(connect, ())
while True:
time.sleep(CHECK_INTERVAL_SECONDS)
global reconnect_count
global connect_status
try:
if ws.sock.status == 101:
# annoying
# print("ws-client connect status is ok.")
reconnect_count = 1
connect_status = 1
except Exception:
connect_status = 0
reconnect()
if __name__ == '__main__':
main()
另外,ws.sock
是None
。
我认为,原因是服务器试图将连接回客户端到一个较高的端口号;然而,只有一些像80,443这样的端口可以到达客户端。
我在代码中看到它使用run_forever
。文献资料说这个函数有代理的参数,但是文档没有给出这个过程的概述,不清楚如何实现这个过程,也没有从概念上展示它看起来是什么样子。
如何使服务器向端口443上的代理发送消息,该代理反过来与我的websocket客户端进行对话,以帮助它克服其他端口号的不可用性?
或者,更好的是,如何让客户机告诉服务器只在端口443上连接到它?
注意:我之所以问这个问题,是因为在任何可用的文档中,有些概念上的东西我都不理解,也不清楚。如果是的话,我就不会问了。
发布于 2022-09-29 20:46:54
服务器管理员不得不打开端口。
他打开了客户连接的端口,比如说1234。这是令人惊讶的,因为我认为WS服务器在随机的高端口上连接回客户端。要么他打开了一个传出端口,要么WS让服务器尝试使用相同的端口号。我不知道,但这就是修好它的原因。
https://stackoverflow.com/questions/73746815
复制相似问题