abourget / gevent-socketio

Official repository for gevent-socketio
http://readthedocs.org/docs/gevent-socketio/en/latest/
BSD 3-Clause "New" or "Revised" License
1.21k stars 331 forks source link

running gevent-socketio in flask #210

Open jasdevsidhu opened 9 years ago

jasdevsidhu commented 9 years ago

Hi developers

This my main.py

                    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"

and the following is my nginx configuration

                    server {
            listen 80;
            server_name 5.196.12.71;
            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:5000;
            }
             access_log  /var/log/nginx/permit_access.log;
          error_log   /var/log/nginx/permit_error.log;
            location /socket.io {
            proxy_pass http://127.0.0.1:5000/socket.io;
            proxy_redirect off;
            proxy_buffering off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            }
            location /templates {
            alias  /home/www/flask_project/templates/;
            }
           location /script {
            alias  /home/www/flask_project/script/;
            }
           location /static {
         proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            }
            location /templates {
            alias  /home/www/flask_project/templates/;
            }
           location /script {
            alias  /home/www/flask_project/script/;
            }
           location /static {
            alias  /home/www/flask_project/static/;
            }

        }

and to run my app, I use the following command gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker main2:app

Each time my app uses the webscoket it get's the following

File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run result = self._run(_self.args, *_self.kwargs) File "/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 124, in handle handler.handle() File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 184, in handle result = self.handle_one_request() File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 321, in handle_one_request self.handle_one_response() File "/usr/local/lib/python2.7/dist-packages/socketio/handler.py", line 170, in handle_one_response self.transport.do_exchange(socket, request_method) File "/usr/local/lib/python2.7/dist-packages/socketio/transports.py", line 241, in do_exchange websocket = self.handler.environ['wsgi.websocket'] KeyError: 'wsgi.websocket' <Greenlet at 0x31dc050: <bound method SocketIOServer.handle of <SocketIOServer at 0x31c4410 fileno=5 address=127.0.0.1:8000>>(<socket at 0x31c4310 fileno=[Errno 9] Bad file des, ('127.0.0.1', 39100))> failed with KeyError

Can anyone please help and guide me out, I am totally new to this technology

jdeschenes commented 9 years ago

Have a look at #29. It's the same problem you had.