无法在Heroku上部署Flask Websockets应用程序

时间:2016-09-09 19:50:58

标签: python heroku flask websocket

我写过一个使用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("");
}

非常感谢任何建议!

1 个答案:

答案 0 :(得分:0)

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

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