A
A
Alex Pts2014-10-06 21:21:13
Node.js
Alex Pts, 2014-10-06 21:21:13

Does socket.io have a client layer?

Each tab in the browser on the server is represented by a unique socket object. I would like not to work with such a connection, but to work with a set of sockets as with 1 client of the subject area.
While solved it by means of rooms (room). All sockets of one client get to 1 room and the message for this client goes to this room. But for some reason it seems that this is a crutch option and it can be done easier.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bumbay, 2014-10-16
@bumbay

Do it with an array. Let's say you get a user id when connecting to socket.io.
Write it to an object.

var clients = {}; // global variable node

... соединение {
('u' + user_id in clients ? client['u' + user_id] : clients['u' + user_id] = []).push(socket.id);
... }

As a result, when you open two tabs, you will have an array:
{
  'u1': [socket_id_hash_one, socket_id_hash_two]
}

And already when performing any emit, you will have a list of socket_id of a specific user for which to emit.
var clients = {}; // global variable node

... соединение {
('u' + user_id in clients ? client['u' + user_id] : clients['u' + user_id] = []).push(socket.id);
... }
...

setInterval(function () {
  // io.eio.clientsCount — количество подключенных клиентов
  // io.eio.clients — список socket_id подключенных клиентов
  // на основании пишем цикл, который изменяет объект clients в зависимости от io.eio.clients
  // на этом основании можно сделать user online list
}, 500);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question