问题描述
我用 socket.io
制作了一个非常简单的应用程序.现在,它显示所有连接 id:当我们打开一个新的浏览器选项卡时,它会添加一个 id;当我们关闭浏览器选项卡时,它会删除 id.
I have made a very simple app with socket.io
. Now, it display all the connection ids: when we open a new browser tab, it adds an id; when we close a browser tab, it removes the id.
现在,我想为任何套接字设置一个超时事件:如果标签的 Keep alive!
按钮在 3 小时内没有被点击(这可能是因为标签没有被重新-访问了3小时),我们认为这个连接是semi-dead"的,在其他连接的页面中将其连接id设为grey
;当我们点击该按钮时,该选项卡在其他页面中变得活跃并黑色
,并重新开始3小时的倒计时.
Now, I want to set one timeout event for any socket: If the button Keep alive!
of a tab has not been clicked for 3 hours (that's maybe because the tab has not been re-visited for 3 hours), we consider the connection "semi-dead", and make its connection id grey
in the page of other connections; when we click the button, the tab becomes alive and black
in other pages, and restarts the countdown of 3 hours.
有人知道如何实现吗?
另外,我有两个关于空闲/非活动连接、断开连接和tab-revisiting之间关系的可选问题:
Additionally, I have two optional questions about the relationships between idle/inactive connections, disconnect and tab-revisiting:
如果我们长时间不重新访问打开的标签页,是否会被视为断开连接?
If we don't re-visit an open tab for long time, will it be considered as a disconnect?
是否可以在浏览器选项卡上设置重新访问事件?
Is it possible to set a re-visiting event on a browser tab?
我目前的后端:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var ids = [];
server.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket) {
ids.push(socket.id);
io.sockets.emit('ids', ids);
socket.on('disconnect', function () {
var index = ids.indexOf(socket.id);
ids.splice(index, 1);
io.sockets.emit('ids', ids);
})
});
前端:
<button type="button" onclick="alert('Keep alive!')">Keep alive!</button>
<div id="ids"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
jQuery(function($) {
var socket = io.connect();
var $ids = $('#ids');
socket.on('ids', function (data) {
var html ="";
for (i = 0; i < data.length; i++) {
html += data[i] + "<br/>";
};
$ids.html(html);
})
});
</script>
推荐答案
Socket.io 使用心跳(0.9.x 版本)或使用 ping(1.x 版本以上)管理超时.一旦您在服务器上设置了这些超时,并且如果客户端符合 socket.io,则套接字将永远不会自动断开连接.当然,如果它丢失网络或网络超时或有人断开它,它会断开连接.
Socket.io manages timeout using heartbeat (0.9.x versions) or using ping (version 1.x onwards). Once you set these timeouts on the server and if the client is socket.io compliant, the socket will never disconnect automatically. Of course it will disconnect if it loses network or a network timeout or someone disconnects it.
请参阅当前 socketio 规范中的以下文本:
See the below text from the current socketio spec:
在这些选项中:
pingTimeout
(Number):要考虑多少ms没有pong包连接关闭 (60000)pingInterval
(Number):多少毫秒在发送新的 ping 数据包 (25000) 之前.
pingTimeout
(Number): how many ms without a pong packet to consider the connection closed (60000) pingInterval
(Number): how many ms before sending a new ping packet (25000).
socket.io 客户端将不断发送 ping 或心跳来使确保连接有效
The socket.io client will keep sending a ping or a heartbeat to make sure the connection is alive
.
因此,如果您的选项卡已打开并且没有活动,则您必须在应用程序级别而不是在 socket.io 级别管理它.
So if your tab is open and if there is no activity, you will have to manage this at your application level and not at the socket.io level.
要做到这一点,您可以使用计时器并检查何时从某个套接字接收到数据.定期计时器可以跟踪活动并在您的 3 小时期限到期后更改状态
To do the same, you can use a timer and check when you received the data from a certain socket. The periodic timer can keep track of activity and change the status once your 3 hour period expires
这篇关于激活空闲/非活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!