发射到包含特定 socket.name 的特定套接字(如耳语)
emit to specifics sockets (like a whisper) which contain specific socket.name
我的套接字像这样存储在一个对象中 "people"。但现在我想在 people.name 中提取与 ["4323","9","43535"] 这样的对象的巧合,例如 9。在这种情况下意味着提取 "OGyF_FMFbsr0ldcbAAAK" 套接字。
用几句话浏览 ["4323","9","43535"] 并查找他们是否在 people 中,然后向包含 people.name 的套接字发出通知 === 9.可能不止一个插座。
所以。
每个 "attending"
["4323","9","43535"]
在"people"
{
"ZA-CJOc1PtiwDVxkAAAD":
{"name":"4","owns":"2-0-62","inroom":"2-0-62","device":"desktop"},
"wKg2rcFSHgcl4m3WAAAG":
{"name":"3","owns":"2-0-110","inroom":"2-0-110","device":"desktop"},
"OGyF_FMFbsr0ldcbAAAK":
{"name":"9","owns":null,"inroom":null,"device":"desktop"}
}
然后发出
io.sockets.socket(id).emit("notification", result);
问题:
如何使 select 套接字发送通知的正确代码?
那么如何为每个人发出通知?
提前致谢
如果我正确理解您的要求,那么一种方法是遍历 people
对象的键,将每个键的 name
属性与attending
数组中的元素,并将任何匹配的键推送到新数组 found
中,以获取名字在 attending
列表中的人员列表。
然后您可以遍历 found
数组以向 people
对象中符合您的搜索条件的客户端发送消息。
var attending = ['4323', '9', '43535'],
found = [];
var people = {
'ZA-CJOc1PtiwDVxkAAAD': {
'name': '4', 'owns': '2-0-62', 'inroom': '2-0-62', 'device': 'desktop'
},
'wKg2rcFSHgcl4m3WAAAG': {
'name': '3', 'owns': '2-0-110', 'inroom': '2-0-110', 'device': 'desktop'
},
'OGyF_FMFbsr0ldcbAAAK': {
'name': '9', 'owns': null, 'inroom': null, 'device': 'desktop'
}
};
for (var person in people) {
for (var i = 0, numAttending = attending.length; i < numAttending; i++) {
if (people[person].name === attending[i]) {
found.push(person);
}
}
}
for (var i = 0, numFound = found.length; i < numFound; i++) {
io.sockets.socket(found[i]).emit('notification', result);
};
编辑
如果你想将整个对象推入你的 found
数组,你可以这样做。由于整个对象而不仅仅是客户端 ID 都存储在数组中,因此下面的发射循环需要进行一些微调才能继续工作。
for (var person in people) {
for (var i = 0, numAttending = attending.length; i < numAttending; i++) {
if (people[person].name === attending[i]) {
found.push(people[person]);
//this would give something like this, without the socket id
//[{"name":"3","owns":null,"inroom":null,"device":"desktop"}]
}
}
}
for (var person in found) {
io.sockets.socket(person).emit('notification', result);
};
我的套接字像这样存储在一个对象中 "people"。但现在我想在 people.name 中提取与 ["4323","9","43535"] 这样的对象的巧合,例如 9。在这种情况下意味着提取 "OGyF_FMFbsr0ldcbAAAK" 套接字。
用几句话浏览 ["4323","9","43535"] 并查找他们是否在 people 中,然后向包含 people.name 的套接字发出通知 === 9.可能不止一个插座。
所以。
每个 "attending"
["4323","9","43535"]
在"people"
{
"ZA-CJOc1PtiwDVxkAAAD":
{"name":"4","owns":"2-0-62","inroom":"2-0-62","device":"desktop"},
"wKg2rcFSHgcl4m3WAAAG":
{"name":"3","owns":"2-0-110","inroom":"2-0-110","device":"desktop"},
"OGyF_FMFbsr0ldcbAAAK":
{"name":"9","owns":null,"inroom":null,"device":"desktop"}
}
然后发出
io.sockets.socket(id).emit("notification", result);
问题:
如何使 select 套接字发送通知的正确代码?
那么如何为每个人发出通知?
提前致谢
如果我正确理解您的要求,那么一种方法是遍历 people
对象的键,将每个键的 name
属性与attending
数组中的元素,并将任何匹配的键推送到新数组 found
中,以获取名字在 attending
列表中的人员列表。
然后您可以遍历 found
数组以向 people
对象中符合您的搜索条件的客户端发送消息。
var attending = ['4323', '9', '43535'],
found = [];
var people = {
'ZA-CJOc1PtiwDVxkAAAD': {
'name': '4', 'owns': '2-0-62', 'inroom': '2-0-62', 'device': 'desktop'
},
'wKg2rcFSHgcl4m3WAAAG': {
'name': '3', 'owns': '2-0-110', 'inroom': '2-0-110', 'device': 'desktop'
},
'OGyF_FMFbsr0ldcbAAAK': {
'name': '9', 'owns': null, 'inroom': null, 'device': 'desktop'
}
};
for (var person in people) {
for (var i = 0, numAttending = attending.length; i < numAttending; i++) {
if (people[person].name === attending[i]) {
found.push(person);
}
}
}
for (var i = 0, numFound = found.length; i < numFound; i++) {
io.sockets.socket(found[i]).emit('notification', result);
};
编辑
如果你想将整个对象推入你的 found
数组,你可以这样做。由于整个对象而不仅仅是客户端 ID 都存储在数组中,因此下面的发射循环需要进行一些微调才能继续工作。
for (var person in people) {
for (var i = 0, numAttending = attending.length; i < numAttending; i++) {
if (people[person].name === attending[i]) {
found.push(people[person]);
//this would give something like this, without the socket id
//[{"name":"3","owns":null,"inroom":null,"device":"desktop"}]
}
}
}
for (var person in found) {
io.sockets.socket(person).emit('notification', result);
};