最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Flask crifan 5550浏览 0评论

折腾:

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

之后,

需要去:

希望搞清楚,如何监听到,connected,disconnected的时间

并且进行判断是否是有效的用户:

userId是存在的,token是有效的。

实现:

对于想要打开一个websocket,比如此处的:

ws://115.29.173.126:21085/users/user5/token6

想要在打开的时候,去判断此连接是否有效。

用户是否有效,token是否有效。

所以想要去监听此连接的connect,即open的事件

然后去判断和处理

以及:

对于断开连接on disconnect, disconnected,closed,close

也去监听。

搜:

flask-sockets connected

python – How to connect multiple clients in Flask using socket.io? – Stack Overflow

Synchronize clients of a Flask application with websockets | Matthieu Amiguet

->

flask-sockets里面好像没有:

socketio.on

之类的写法啊

Adding web sockets to Flask apps

hasenj / flask-sockets-tutorial / source / main.py — Bitbucket

作者的源码里面:

flask-sockets/flask_sockets.py at master · kennethreitz/flask-sockets

也没找到关于:

ws的closed的变量

后来注意到Log中有:

ws=<geventwebsocket.websocket.WebSocket object at 0x7f1f997fade0>

-》所以可以去找geventwebsocket.websocket.WebSocket

geventwebsocket.websocket.WebSocket

gevent-websocket/websocket.py at master · jgelens/gevent-websocket

里面就有:

closed

gevent-websocket 0.9.5 : Python Package Index

-》

想起来了:

之前用:

pip install flask-sockets

去安装flask-sockets的时候,就安装了对应的依赖的库:

gevent-websocket-0.9.5

从示例代码中就可以看到:

是有对应的

on_open

on_message

on_close

的函数的。

对于此处代码:

    server = pywsgi.WSGIServer((”, 21085), app, handler_class=WebSocketHandler)

去搜搜:

pywsgi.WSGIServer

然后添加对应的类,作为resource去处理不同的ws端口

python – Gevent-Websocket Detecting closed connection – Stack Overflow

另外再去看看:

geventwebsocket

的:

WebSocketApplication

WebSocketServer

Resource

pywsgi WSGIServer app resource

Reloading with other WSGI servers | Flask (A Python Microframework)

WSGI Servers – Full Stack Python

Implementing servers — gevent 1.2a2.dev0 documentation

-》

gevent.pywsgi – A pure-Python, gevent-friendly WSGI server — gevent 1.2a2.dev0 documentation

class WSGIServer(listener, application=None, backlog=None, spawn=’default’, log=’default’, error_log=’default’, handler_class=None, environ=None, **ssl_args)

gevent/wsgiserver_ssl.py at master · gevent/gevent

Flask + Gevent WSGI Server + PUT method

21.4. wsgiref — WSGI Utilities and Reference Implementation — Python 3.5.2 documentation

Python Flask Gevent stack – Simple “Hello World” app shows as inefficient when benchmarked · Issue #1073 · pallets/flask

jgelens/gevent-websocket: Official mirror of the repository at Bitbucket.

flask WebSocketServer

Using websockets with Flask via Tornado

感觉这个才是我要的:

带class去处理对应端口的open,on_message

但是人家用的是tornado。。。

WebSocketApplication

python WebSocketApplication

gevent-websocket/resource.py at c46ea0b7cbcd6fd15717c6bf4f6c140aa52e70a8 · jgelens/gevent-websocket

可以看到有对应的:

on_open

on_close

on_message

geventwebsocket.WebSocketApplication – Nullege Python Samples

python – How to get gevent websockets working? – Stack Overflow

Gevent Tutorial

The best and simplest tools to create a basic WebSocket application with Flash fallback and Python on the server side | Jan-Philip Gehrcke

Flask-Sockets on open message closed

whichlight — websockets in python with flask

flask-sockets list open

flask-sockets gevent-websocket open

【总结】

对于Flask-Sockets,最终放弃了去实现监听对应的websocket的open和close的事件。

转载请注明:在路上 » 【未解决】Flask-Sockets中实现对于websocket的连接的监听打开open关闭close发送信息message

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.174 seconds, using 22.23MB memory