kudos/koa-websocket

It's really nice lib , but 1 question

Closed this issue · 7 comments

I don't know how to manage my clients

any example for manage client ? like I wanna notice 1 client some message , I wanna server push to client , and I wirte some code , it's little stupid , want better solution

this.websocket.send('waiting for verify data');

     //message format : cmd|arg


        //need save this.websocket here

       var socket = this.websocket

     this.websocket.on('message', function(message) {


          var vec_str = message.split('|');
          var cmd = vec_str[0];
          var arg = vec_str[1];

          console.log(cmd,arg);
          var argobj = JSON.parse(arg);
          console.log(argobj);

          switch(cmd){
            case 'verify':{
                var username = argobj.user;
                var password = argobj.password;

                //test code

                var exampleShop = shopData[0];
                var exampleShopObj = exampleShop.toObject();
                exampleShopObj.websocket = socket ;

                onlineStoreManager.addStore(exampleShop._id , exampleShopObj);
                onlineStoreManager.sendMessage(exampleShop._id,"login sucess");
            }
            break;
          }


     });

first time use websocket , I don't know how to manage client

kudos commented

Your question is a little unclear, does the code above work? If not, what error do you see? If it does, are you just looking for advice on better usage?

Hm..your code is ok , no error , but I have little question about usage , I wanna push message to a client which is already connected, but I do now know how to find the client

kudos commented

I see what you mean. Each open websocket stays active within the code block you pasted above, so you don't need to wrap it for user management.

Here's a quick rewrite with some changes I would make.

function socket() {
  this.websocket.send('waiting for verify data');

  // use JSON as the encapsulating message format
  // message format : {cmd: cmd, arg: arg}
  this.websocket.on('message', function(message) {

    console.log(message);
    var parsedMessage = JSON.parse(message);
    var cmd = parsedMessage.cmd;
    var arg = parsedMessage.arg;

    switch (cmd) {
      case 'verify':
        {
          var username = argobj.user;
          var password = argobj.password;

          //test code

          var exampleShop = shopData[0];
          var exampleShopObj = exampleShop.toObject();

          // I'm not sure what this does, but maybe you don't need it now
          onlineStoreManager.addStore(exampleShop._id, exampleShopObj);
          // `this.websocket` still refers to the connection just opened
          // new connections get their own context
          this.websocket.send("login sucess");
        }
        break;
    }
  }.bind(this)); // bind `this` to new scope so we don't need to reassign
}

Can I push [this.websocket] to some array to manage? Because I need broadcast sometime and send single message without [on message], like one client talk to other client situation

And I am not really understood what's meaning "bind(this)"

I see bind meaning , cause it in on message scope , bind is tell that scope belong father scope :)

Thanks , I use

bind(this) 

can make I want !
I just want keep this.websockt to somewhere and when I want to call it