如何设置WebSocket客户端代理,使服务器消息通过端口443Map回websocket客户端?

ih99xse1  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(174)

我的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'

......它只是一遍又一遍地重复。
下面是相关代码(客户端):

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上连接回它?
注意:我问这个问题是因为有些概念上的东西我不理解,在任何可用的文档中也不清楚。如果是的话,我就不会问了。

gijlo24d

gijlo24d1#

服务器管理员不得不打开端口。
他打开了客户端连接的端口,比如说1234。这很令人惊讶,因为我以为WS服务器在一个随机的高端口上连接回客户端。要么是他打开了一个传出端口,要么是WS让服务器尝试使用同一个端口号。我不知道,但这就是解决问题的方法。

相关问题