我在 cpanel 上有一个 Flask 应用程序。
from flask_socketio import SocketIO, emit
from flask_cors import CORS
CORS(app)
app.config['DEBUG'] = True
import eventlet
eventlet.monkey_patch()
socketio = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*")
@socketio.on('message')
def handle_message(message):
socketio.emit('message', message)
<main class="container">
<div id="messages"></div>
<input type="text" name="message" id="message" placeholder="Enter your message">
<button onclick="send()">Send</button>
</main>
<script>
const socket = io.connect('https://www.name.com:443');
socket.on('message', (message) => {
const ul = document.getElementById('messages');
const li = document.createElement('li');
li.appendChild(document.createTextNode(message));
ul.appendChild(li);
});
const send = () => {
const message = document.getElementById('message');
socket.emit('message', message.value);
document.getElementById('message').value = '';
}
</script>
它从passenger_wsgi.py运行
from app import app as application
浏览器控制台显示
Firefox can’t establish a connection to the server at wss://www.name.com/socket.io/?EIO=4&transport=websocket&sid=pF1NGCYlzyJPgkpDAAAF
&日志说
You need to use the eventlet server. See the Deployment section of the documentation for more information.
我认为我的网站正在使用 ngnix & 我找不到配置文件。它对我隐瞒了,所以没有什么危险发生
我有一个 ssl 证书并且该应用程序在我的本地主机中运行良好
我建议仔细阅读本文档 -> https://flask-socketio.readthedocs.io/en/latest/deployment.html
如果您使用
Passenger
网络服务器,不幸的是它受Flask-SocketIO
支持。请参阅 Flask-SocketIO
https://github.com/miguelgrinberg/Flask-SocketIO/issues/760 的作者 Miguel 不久前关于此事的回答。
您需要在文档中提到的特定部署选项之间进行选择 - uWSGI、Gunicorn 或嵌入式服务器。
希望对你有帮助!