【已解决】使用Flask-Sockets去实现Flask中的Websocket

折腾:

【已解决】flask中实现websocket

去用

Flask-SocketIO去实现websocket

但是出现各种错误,最终还是无法解决。

现在去换用:

GitHub – kennethreitz/flask-sockets: Elegant WebSockets for your Flask apps.

看看是否可用。

去写了点测试代码:

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/run.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from runningfast.app import app
from runningfast.app import gLog
if __name__ == ‘__main__’:
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer((”, 21085), app, handler_class=WebSocketHandler)
    gLog.debug("server=%s", server)
    server.serve_forever()

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/runningfast/app.py

from flask_sockets import Sockets
sockets = Sockets(app)
@sockets.route(‘/echo’)
def echo_socket(ws):
    gLog.debug("ws=%s", ws)
    while not ws.closed:
        message = ws.receive()
        ws.send(message)
@app.route(‘/’)
def hello():
    return ‘Hello World!’

然后去安装:

(RunningFast) ➜  staging pip install flask-sockets
Collecting flask-sockets
  Downloading Flask_Sockets-0.2.1-py2.py3-none-any.whl
Requirement already satisfied (use –upgrade to upgrade): gevent in /root/Envs/RunningFast/lib/python2.7/site-packages (from flask-sockets)
Collecting gevent-websocket (from flask-sockets)
  Downloading gevent-websocket-0.9.5.tar.gz
Requirement already satisfied (use –upgrade to upgrade): Flask in /root/Envs/RunningFast/lib/python2.7/site-packages (from flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): greenlet>=0.4.9 in /root/Envs/RunningFast/lib/python2.7/site-packages (from gevent->flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): itsdangerous>=0.21 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask->flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask->flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask->flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/RunningFast/lib/python2.7/site-packages (from Flask->flask-sockets)
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/RunningFast/lib/python2.7/site-packages (from Jinja2>=2.4->Flask->flask-sockets)
Building wheels for collected packages: gevent-websocket
  Running setup.py bdist_wheel for gevent-websocket … done
  Stored in directory: /root/.cache/pip/wheels/d8/34/b8/cdbfc642f5351911099477cbea15e0a0c6db72117b7c7f312c
Successfully built gevent-websocket
Installing collected packages: gevent-websocket, flask-sockets
Successfully installed flask-sockets-0.2.1 gevent-websocket-0.9.5

安装了:

flask-sockets-0.2.1 gevent-websocket-0.9.5

再去修改启动配置:

加上gunicorn的-k flask_sockets.worker

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/supervisor.conf

[program:runningfast_staging]
directory=/root/RunningFast/staging
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21085 -k flask_sockets.worker run:app &

再去想办法去测试

然后直接用:

Simple Web Socket Client

就可以正常去打开ws开头的地址,去测试websocket了:

-》

真的是:

异常的简单啊。。。。

【总结】

去参考:

GitHub – kennethreitz/flask-sockets: Elegant WebSockets for your Flask apps.

去写上代码:

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/run.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from runningfast.app import app
from runningfast.app import gLog
if __name__ == ‘__main__’:
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer((”, 21085), app, handler_class=WebSocketHandler)
    gLog.debug("server=%s", server)
    server.serve_forever()

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/runningfast/app.py

from flask_sockets import Sockets
sockets = Sockets(app)
@sockets.route(‘/echo’)
def echo_socket(ws):
    gLog.debug("ws=%s", ws)
    while not ws.closed:
        message = ws.receive()
        ws.send(message)
@app.route(‘/’)
def hello():
    return ‘Hello World!’

然后修改配置:

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/supervisor.conf

[program:runningfast_staging]
directory=/root/RunningFast/staging
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21085 -k flask_sockets.worker run:app &

即可正常运行Flask的websocket了。

然后,再去测试:

此处用:

Chrome的插件:

Simple Web Socket Client即可打开和发送和接受消息了:

接着再去:

【已解决】如何利用Flask-Sockets实现websocket的地址中带参数

以及:

【未解决】Flask-Sockets中实现对于websocket的连接的监听打开open,关闭close,发送信息messge



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量