WebSocket连接失败:WebSocket握手期间出错:意外响应代码:500

时间:2018-02-18 16:55:43

标签: javascript python websocket wsgi

当我尝试在我的网站上登录(注册工作正常)时,我收到此错误:与'ws:// localhost:5001 / socket'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:500

我之前没有使用相同的代码获得此代码,并且它与我的朋友使用相同的代码。我也尝试过改变端口。我在javascript文件中得到了这段代码:

function connectsocket(email)
{
var ws = new WebSocket("ws://" + document.domain + ":5001/socket");

ws.onopen = function() {
    console.log("Connection with ws open");
    console.log("email for socket: " + email);
    ws.send(email);
};

ws.onmessage = function(response) {
    console.log("Message received from server");
    console.log("response: " + response);
    console.log("response: " + response.data);
    if (response.data == "sign_out") {
        sign_out();
    }
};


ws.onclose = function() {
    console.log("WebSocket closed");
};

ws.onerror = function() {
    console.log("Error");
};
}

然后我在我的服务器文件中得到了这段代码:

@app.route('/socket')
def socket():
if request.environ.get("wsgi.websocket"):
ws = request.environ["wsgi.websocket"]
while True:
    try:
        cur_email = ws.receive()
        if cur_email in current_sockets:
            current_sockets[cur_email].send("sign_out")
        current_sockets[cur_email] = ws

    except WebSocketError as e:
        print(str(e))
..
def run_wsgi():
    app.debug = True
    http_server = WSGIServer(('', 5001), app, 
    handler_class=WebSocketHandler)
    http_server.serve_forever()


if __name__ == '__main__':
    run_wsgi()

0 个答案:

没有答案