简体   繁体   English

使用socket.io(NodeJs)检测客户端是否与网络断开连接

[英]Detect if client side is disconnected from network with socket.io (NodeJs)

I am creating an app with node.js and socket.io. 我正在使用node.js和socket.io创建一个应用程序。 I need to detect if client side is disconnected from internet and fire socket.disconnect() manually/automatically. 我需要检测客户端是否已从互联网断开连接,并手动/自动断开了fire socket.disconnect()。

I have searched but cant find any solution. 我已经搜索过,但是找不到任何解决方案。 Here is my client and server side code. 这是我的客户端和服务器端代码。

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const fs = require('fs');

server.listen(8005, () => console.log('app is running at 8005 port'));

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
})

const sockets = {};
io.sockets.on('connection', (socket) => {
    socket.once('disconnect', function () {
        console.log('-------disconencted', socket.userId); 
        delete sockets[socket.userId];
    });
    socket.on('init', (data) => {
        socket.userId = data.userId;
        sockets[data.userId] = socket;
        sockets[data.userId].emit('userOnline', { id : data.userId});
        console.log(socket.userId,'--------useronline-----------',data)
    });


    socket.on('test', (data) => {
        socket.emit('testResp', { id : 'sdfd'});
        console.log('--------useronline-----------',socket.userId);
    });

    socket.on('total', data => {
        socket.emit('totalResp', { users: Object.keys(sockets)})
        console.log('--------total-----------',socket.userId);

    })





})

Client side code: 客户端代码:

var socket = io('http://localhost:8005');
    socket.on('userOnline', function (data) {
        console.log(socket.userId, '---userOnline--------', data);
    });

    socket.on('testResp', function (data) {
        console.log(socket.userId, '----testResp-------', data);
    });

    socket.on('totalResp', function (data) {
        document.querySelector('#total_users_length').innerHTML = data.users.length;

        console.log(socket.userId, '----totalResp-------', data);
    });
    document.querySelector('#init').addEventListener('click', function(){

        socket.emit('init', {userId: document.querySelector('input').value});
    });

    document.querySelector('#test').addEventListener('click', function(){

      socket.emit('test', {test: 'tst'});
    });

    document.querySelector('#total').addEventListener('click', function(){

    socket.emit('total', {test: 'tst'});
    });

I am storing the userId of user in socket so that when a user disconnected with server, i can find its id and can send notifications. 我将用户的userId存储在套接字中,以便当用户与服务器断开连接时,我可以找到其ID并可以发送通知。 Thanks in advance. 提前致谢。

I think you are looking for the disconnect event, You can try with this. 我认为您正在寻找断开连接事件,可以尝试一下。

socket.on('disconnect', function () {
    io.emit('user disconnected');
  });
});

Since you have already added this in your code. 由于您已经在代码中添加了此代码。 socket fires the disconnect event whenever the client is not responding to the heartbeat that socket implements internally. 每当客户端不响应套接字在内部实现的心跳时,套接字就会触发断开事件。 You can check the docs for more info Socket.io check the "Disconnection detection" section. 您可以检查文档以获取更多信息。Socket.io请检查“断开连接检测”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM