Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

兼容其他版本socket io #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

hnaoto
Copy link

@hnaoto hnaoto commented Jan 15, 2016

^ ^ 非常感谢提供源码和详细教程~

关于0.9以后的socket io没有clients()方法, 私聊会报错, 详见这里: #3
stackoverflow上有各种解决方法,http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients?answertab=active#tab-to 我个人采取了其中一种, 测试"socket.io": "^1.3.7"已经通过。

有空请看看吧~谢谢~

@phobal
Copy link

phobal commented Nov 7, 2016

我也遇到这个问题,我用的socket版本是1.5.1,按照stackoverflow上的做法还是不对,最后用下面的方式可以解决问题:

  var clients = Object.entries(io.sockets.sockets);
      //遍历找到该用户
      clients.forEach(function(client) {
        if (client[1].name == data.to) {
          //触发该用户客户端的 say 事件
          client[1].emit('say', data);
        }
      });

@johnsoncheg
Copy link

stackoverflow上取到的是个对象并不是数组- -

@lynndotconfig
Copy link

@phobal It works. While Getting Object.entries is not a function

var entries = require('object.entries')
var clients = entries(io.sockets.sockets);

@wuhao5436
Copy link

wuhao5436 commented Sep 13, 2017

my problem is that " clients.forEach is not a function " ,the version of socket.io is v2.0.3,
because var clients = io.sockets.sockets; got an Object but a Array , so using "for key in " search for the user is right way;

        var clients = io.sockets.sockets;
        //遍历找到该用户
        for(var k in clients){
          if(clients[k].name == data.to){
              clients[k].emit("say",data);
          }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants