简体   繁体   English

flask-socketio无法在AWS EC2上启动/无法正常工作

[英]flask-socketio does not start/works wierd on aws ec2

This is my flask code, 这是我的烧瓶代码,

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('connect')
def socketioconnect():
    emit('customresponse', {'msg':'connect emit'})

@socketio.on('cunstommsg')
def custommsgHandler(msg):
    emit("customresponse", {'msg':msg['msg']+'with handler emit'})

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000)

and when i run this code with "sudo python3 app.py", i can see only 当我使用“ sudo python3 app.py”运行此代码时,我只能看到

INFO:engineio:Server initialized for gevent.

also when i acess with web browser, nothing changes in both of console and browser 当我使用网络浏览器访问时,控制台和浏览器也没有任何变化

and inbound setting of my ec2 instance is open with port 80, 5000 并且我的ec2实例的入站设置通过端口80、5000打开

and this is my pip freeze 这是我的点子冻结

click==6.7
Flask==1.0.2
Flask-SocketIO==3.0.1
gevent==1.3.5
greenlet==0.4.14
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pkg-resources==0.0.0
python-engineio==2.2.0
python-socketio==2.0.0
six==1.11.0
Werkzeug==0.14.1

i tried this and it works: 我尝试了这个,它的工作原理:

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)
    socketio.run(app, host='0.0.0.0', port=5000)

but all of tutorials and SO answers say don't do like this 但是所有的教程和答案都说不要这样

this is client test code: 这是客户端测试代码:

  var socket = io();
  socket.on("customresponse", function(msg){
      console.log("cumstom response arrived"+ msg.msg);
      alert("cumstom response arrived" + msg.msg);
  });

  $(function () {
      $("#sendSIO").click(function() {
          console.log('sendSIO clicked');
          socket.emit("custommsg", {msg:'sendSIO clicked'});
      });
  });

when i connect, i can get "custom response arrived" in javascript console but when i clicked #sendSIO, although i can see "custommsg arrived" in flask console, client's "customresponse" handler does not work. 当我连接时,我可以在javascript控制台中获得“自定义响应到达”,但是当我单击#sendSIO时,尽管我在flask控制台中看到“ custommsg到达”,但是客户端的“ customresponse”处理程序不起作用。

is this problem related with run() of flask-socketio? 这个问题与flask-socketio的run()有关吗?

sorry for kind of untidy question, but im noob to socketio and there's no useful documents for this kind of issues. 抱歉,我提出了一个不完整的问题,但我对socketio不了解,也没有针对此类问题的有用文档。 Thanks! 谢谢!

It seems you want to run your application on port 80, so you should do this: 似乎您想在端口80上运行您的应用程序,因此您应该这样做:

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=80)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM