简体   繁体   中英

how to check the a bunch of users is in online/offline and it is online show green dots or else red dot using typescript in angular4

this is my component.ts code.Really I don't know about that one just I am trying .whether it is correct or wrong however I want to show the if users are in online besides username shows green button or else red dot button

 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();
        });

This is sample code you can simpleify or customize based on your requirement. Many useful features are available in Socketio refer here for further change here .

Server code:

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);
   });
 });

Client code:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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