SocketIO +烧瓶检测断开 [英] SocketIO + Flask Detect Disconnect

查看:44
本文介绍了SocketIO +烧瓶检测断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个不同的问题,但意识到它简化了这一点:您如何检测客户端何时从页面断开(关闭其页面或单击链接)(换句话说,套接字连接关闭)?我想制作一个具有更新用户列表的聊天应用,并且在Python上使用Flask.当用户连接时,浏览器发送一个 socket.emit()并带有事件和用户名,以告知服务器新用户的存在,此后服务器将向所有客户端发送socket.emit(),以便所有客户端都可以将此新用户追加到他们的用户列表中.但是,我希望客户端也将包含其用户名的消息发送到Disconnect上的服务器.我不知道如何正确设置触发器.注意:我只是在页面上使用带有脚本标签的简单html文件,我不确定如何添加JS文件以与页面一起使用,尽管我可以确定是否需要这样做.

I had a different question here, but realized it simplifies to this: How do you detect when a client disconnects (closes their page or clicks a link) from a page (in other words, the socket connection closes)? I want to make a chat app with an updating user list, and I’m using Flask on Python. When the user connects, the browser sends a socket.emit() with an event and username passed in order to tell the server a new user exists, after which the server will message all clients with socket.emit(), so that all clients will append this new user to their user list. However, I want the clients to also send a message containing their username to the server on Disconnect. I couldn’t figure out how to get the triggers right. Note: I’m just using a simple html file with script tags for the page, I’m not sure how to add a JS file to go along with the page, though I can figure it out if it’s necessary for this.

推荐答案

弄清楚了. socket.on('disconnect')确实是正确的,但是默认情况下,它每分钟仅对每个用户执行一次ping操作,这意味着花很长时间才能看到该事件.

Figured it out. socket.on('disconnect') did turn out to be right, however by default it pings each user only once a minute or so, meaning it took a long time to see the event.

这篇关于SocketIO +烧瓶检测断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆