0

以下是我的python代码

from gevent import monkey;monkey.patch_all()
from flask import Flask,render_template, url_for, request, redirect, flash,jsonify,session,Markup
from socketio import socketio_manage
from socketio.namespace import BaseNamespace
from socketio.server import SocketIOServer

app=Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'

class ChatNamespace(BaseNamespace):
  def recv_connect(self):
    print "successfully connected"
    self.emit('show_result','successfully connect')
  def on_receive_message(self,msg):
    print "message is "+msg["data"]
    self.emit('show_result2',msg["data"])
@app.route('/')
def index():
   #print "in the function"
   return render_template('index.html')

@app.route("/socket.io/<path:path>")
 def run_socketio(path):
  socketio_manage(request.environ, {'/test': ChatNamespace})
  return 'ok'

if __name__=='__main__':
  #app.run(debug=True, port=80, host='0.0.0.0')
   app.debug=True
   #app.run()
   SocketIOServer(('0.0.0.0', 5000), app,resource="socket.io").serve_forever()
print "successfull listen to socket"

以下是nginx配置服务器{

location / {
  proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  try_files $uri @proxy;
}
location @proxy {
    proxy_pass http://127.0.0.1:8000;
}
location /templates {
    alias  /home/www/flask_project/templates/;
}
 location /script {
    alias  /home/www/flask_project/script/;
  }
  location /static {
    alias  /home/www/flask_project/static/;
  }

 }

每次运行应用程序时,我都会使用以下命令
gunicorn main2:app -b localhost:5000

我知道我缺少很多信息来在实时服务器上运行这个 gevent-socketio 应用程序。谁能帮帮我,我对这种网络套接字技术完全陌生

4

1 回答 1

0

Have you tried FlaskSocketio??

If you are trying to do it without the extension, you will need the socketio gunicorn worker to run your app.

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app
于 2015-04-26T00:26:55.963 回答