如何检查一堆用户是在线/离线,它是在线显示绿点或其他红点使用angular4中的angular4

时间:2017-11-15 05:47:23

标签: angular typescript

这是我的component.ts代码。实际上我不知道那个只是我正在尝试。无论是正确还是错误但是我想要显示用户是否在线,除了用户名显示绿色按钮或别的红点按钮

 onlineCheck()
      {
        let xhr = new XMLHttpRequest();
        return new Promise((resolve, reject)=>{
            xhr.onload = () => {
                // Set online status
                this.isOnline = true;
                resolve(true);
                console.log("user is in online "+this.isOnline);
            };
            xhr.onerror = () => {
                // Set online status
                this.isOnline = false;
                reject(false);
                console.log("user is in offline "+this.isOnline);

            };
            xhr.open('GET',this.onlineURL(this.appl_name,this.s_name), true);
            console.log("live Url "+this.onlineURL(this.appl_name,this.s_name));
            xhr.send();
        });

1 个答案:

答案 0 :(得分:0)

这是您可以根据您的要求简化或自定义的示例代码。 Socketio中提供了许多有用的功能,请参阅此处以进一步更改here

服务器代码:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
   fs.readFile(__dirname + '/index.html',
      function (err, data) {
         if (err) {
             res.writeHead(500);
             return res.end('Error loading index.html');
         }
   res.writeHead(200);
   res.end(data);
   });
 }

 io.on('connection', function (socket) {
   socket.on('myOnlineStatus', function (data) {
     console.log(data);
   });
 });

客户代码:

 var socket = io('http://localhost');
 socket.on('myOnlineStatus', function (data) {
   console.log(data);
   // Write logic to identify each user and update network status
   // You will get status of each user every 10 seconds
 });
 setInterval(() => { 
           socket.emit('my other event', { myId: 'Raja', status : navigator.onLine });
     }, 10000);