在Heroku上部署Flask Websockets应用时遇到麻烦



我已经编写了一个使用websockets的Flask应用程序,它在本地运行良好。

但是当我尝试通过Heroku部署它并在浏览器中打开它时,我得到这些错误:

在Chrome: WebSocket connection to 'wss://flask-realtime-chat-app.herokuapp.com/chat' failed: Error during WebSocket handshake: Unexpected response code: 404

在Firefox中:Firefox can’t establish a connection to the server at ws://flask-realtime-chat-app.herokuapp.com/chat.

下面是我的代码的相关部分:

import json
from flask import Flask, send_from_directory, request
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@app.route("/")
def index():
    return send_static("index.html")
@app.route("/static/<path>")
def send_static(path):
    return send_from_directory("../client/static", path)
connections = []
messages = []
usernames = {}
@sockets.route("/chat")
def chat_socket(web_socket):
    global connections, usernames
    connections = add_socket(web_socket, connections)
    while not web_socket.closed:
        data = json.loads(web_socket.receive())
        connections = remove_closed_sockets(connections)
        messages.append(data)
        json_message = json.dumps(data)
        send_message(json_message, connections)
def add_socket(web_socket, connections):
    return connections + [web_socket]
def remove_closed_sockets(connections):
    return [c for c in connections if not c.closed]
def send_message(message, connections):
    for socket in connections:
        socket.send(message)
if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
    server.serve_forever()

前端:

if (window.location.protocol == "https:") {
  var ws_scheme = "wss://";
} else {
  var ws_scheme = "ws://";
}
var socket = new WebSocket(ws_scheme + location.host + "/chat");
socket.onopen = function (event) {
    console.log("Websocket open!");
}
socket.onclose = function (event) {
    console.log("Websocket closed!");
}
socket.onmessage = function (event) {
    var incomingMessage = JSON.parse(event.data);
    if (incomingMessage["message"]) {
        addMessageToChatScreen(incomingMessage);
    } else if (incomingMessage["usernames"]) {
        usernames = incomingMessage["usernames"];
    }
}

function sendMessage() {
    var data = JSON.stringify({
        uid: uid,
        message: $("#message_input").val()
    });
    socket.send(data);
    $("#message_input").val("");
}

任何建议将不胜感激!

在procfile中提供一个worker类。这里我使用eventlet。

web: gunicorn --worker-class eventlet -w 1 --pythonpath app:app

相关内容

  • 没有找到相关文章

最新更新