javascript - How to get response from other user after joining room using socket.io -


i have 1 doubt regarding socket.io.i have 2 type of user i.e-admin,client .first admin create userid , join room.i need when user join room admin should response , need regarding this.

my working codes given below.

server.js:

var port=8888; var express=require('express'); var morgan = require('morgan'); var http=require('http'); var bodyparser= require('body-parser'); var methodoverride = require('method-override'); var mongo = require('mongojs'); var database='oditek'; var collections=['video']; var app= express(); var server=http.server(app); var io=require('socket.io')(server); var db = mongo.connect("127.0.0.1:27017/"+database, collections); app.use(express.static(__dirname + '/public'));     // set static files location /public/img /img users app.use(morgan('dev'));                     // log every request console app.use(bodyparser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded app.use(bodyparser.json())    // parse application/json app.use(methodoverride());                  // simulate delete , put db.on('ready', function () {     console.log('database connected') }); app.get('/',function(req,res){     res.sendfile('view/login.html'); }); app.post('/login',function(req,res){     var username=req.body.username;     var password=req.body.userpassword;     if(username && password){         db.video.findone({             username:username,             password:password         },function(err,doc){             if(doc){                 console.log('login',doc);                 res.send(doc);             }             if(err){                 console.log('login12',err);                 res.send("could not login");             }         });     } }); app.get('/video',function(req,res){     res.sendfile('view/video.html'); });  //socket----programming// var roomid; io.on('connection',function(socket){     //console.log(socket);     roomid=socket.handshake.query.roomid;     var usertype=socket.handshake.query.usertype;     socket.join(roomid);  }); server.listen(port); console.log('server listening on port'+port); 

my client side code given below.

function videobroadcasting(utype){     var messagegateway;     if(utype=='admin'){         var userid = getrandomstring();         $('#styled').val('http://localhost:8888/video?usertype=client & id='+userid);         messagegateway=io('http://localhost:8888/?roomid='+userid+'usertype='+utype);     }     if(utype=='user'){         messagegateway=io('http://localhost:8888/?usertype='+utype);     }     messagegateway.on('connect',function(){         console.log('socket connected');      }); } function getrandomstring() {    return (math.random() * new date().gettime()).tostring(36).replace(/\./g, ''); } function getquery(key){         var temp = location.search.match(new regexp(key + "=(.*?)($|\&)", "i"));         if(!temp) return;         return temp[1]; } 

after client joining room admin should 1 notification.please me this.

one simple solution save socket object admin in server when admin joins room. then, when other user join room, emit message socket admin/admins. keep array update admins logged in room.

something like:

var socketadmin = {}; io.on('adminjoins',function(socket){   socketadmin = socket;   roomid=socket.handshake.query.roomid;   var usertype=socket.handshake.query.usertype;   socket.join(roomid); }); io.on('clientjoins',function(socket){   roomid=socket.handshake.query.roomid;   socketadmin.emit('newclient', {socketclient: socket, roomid: roomid};   var usertype=socket.handshake.query.usertype;   socket.join(roomid); }); 

in example, client sends message 'adminjoins' if admin, or 'clientjoins' ir client, can send checking utype var have. in case new client joins room, admin recieve 'newclient' message socket of client , roomid (just example).


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -