我无法获得用户名和& userPic所有连接的房间用户。
请建议我如何解决?请帮忙〜
<MY_PATH>\chatapp\socket\socket.js:34
user:getUsers[i].username,
^
TypeError: Cannot read property 'username' of undefined
at updateUserList (<MY_PATH>\chatapp\socket\socket.js:34:29)
at Socket.<anonymous> (<MY_PATH>\chatapp\socket\socket.js:22:7)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at <MY_PATH>\chatapp\node_modules\socket.io\lib\socket.js:514:12
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
这是代码。
function updateUserList(room, updateALL){
var getUsers = io.of('/messages').in(room).clients();
var userlist = [];
for(var i in getUsers){
userlist.push({
user:getUsers[i].username,
userPic:getUsers[i].userPic
});
}
socket.to(room).emit('updateUsersList', JSON.stringify(userlist));
if(updateALL) {
socket.broadcast.to(room).emit('updateUsersList')
}
}
答案 0 :(得分:0)
我在getUsers中获取数据如下:
Namespace {
name: '/messages',
server:
Server {
nsps: { '/': [Object], '/roomlist': [Object], '/messages': [Object] },
_path: '/socket.io',
_serveClient: true,
parser:
{ protocol: 4,
types: [Array],
CONNECT: 0,
DISCONNECT: 1,
EVENT: 2,
ACK: 3,
ERROR: 4,
BINARY_EVENT: 5,
BINARY_ACK: 6,
Encoder: [Function: Encoder],
Decoder: [Function: Decoder] },
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
Namespace {
name: '/',
server: [Object],
sockets: [Object],
connected: [Object],
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: [Object] },
eio:
Server {
clients: [Object],
clientsCount: 1,
wsEngine: 'ws',
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [Object],
_events: [Object],
_eventsCount: 1 },
httpServer:
Server {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
_connections: 3,
_handle: [Object],
_usingSlaves: false,
_slaves: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
_pendingResponseData: 0,
maxHeadersCount: null,
_connectionKey: '6::::3000',
[Symbol(asyncId)]: 13 },
engine:
Server {
clients: [Object],
clientsCount: 1,
wsEngine: 'ws',
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [Object],
_events: [Object],
_eventsCount: 1 } },
sockets:
{ '/messages#g3ylC0osj4SFJlkSAAAC':
Socket {
nsp: [Object],
server: [Object],
adapter: [Object],
id: '/messages#g3ylC0osj4SFJlkSAAAC',
client: [Object],
conn: [Object],
rooms: [Object],
acks: {},
connected: true,
disconnected: false,
handshake: [Object],
fns: [],
flags: {},
_rooms: [],
_events: [Object],
_eventsCount: 3,
username: 'Virendra Kumar',
userPic: 'https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/21078271_1452824561450709_6824442799108958435_n.jpg?oh=38e9461d2dde855c01e085b5ae2f9681&oe=5A7C1B0F' } },
connected:
{ '/messages#g3ylC0osj4SFJlkSAAAC':
Socket {
nsp: [Object],
server: [Object],
adapter: [Object],
id: '/messages#g3ylC0osj4SFJlkSAAAC',
client: [Object],
conn: [Object],
rooms: [Object],
acks: {},
connected: true,
disconnected: false,
handshake: [Object],
fns: [],
flags: {},
_rooms: [],
_events: [Object],
_eventsCount: 3,
username: 'Virendra Kumar',
userPic: 'https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/2799108958435_n.jpg?oh=38e9461d2dde855c01e085b5ae2f9681&oe=5A7C1B0F' } },
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter:
Adapter {
nsp: [Circular],
rooms: { '8229': [Object], '/messages#g3ylC0osj4SFJlkSAAAC': [Object] },
sids: { '/messages#g3ylC0osj4SFJlkSAAAC': [Object] },
encoder: Encoder {} },
_events: { connection: [Function] },
_eventsCount: 1 }
答案 1 :(得分:0)
这是server.js代码:
module.exports = function(io, rooms){
// Establish the connection
var chatrooms = io.of('/roomlist').on('connection', function(socket){
console.log('Connection Establish on the server!');
socket.emit('roomupdate', JSON.stringify(rooms)); // sends it back to the user that created it in the first place.
// Receive the new room event.
socket.on('newroom', function(data){
rooms.push(data); // append the data into the array.
socket.broadcast.emit('roomupdate', JSON.stringify(rooms)); // doesn't broadcast to the person that created the room.
socket.emit('roomupdate', JSON.stringify(rooms)); // sends it back to the user that created it in the first place.
})
});
var messages = io.of('/messages').on('connection', function(socket){
console.log('Connected to the Chatroom!');
socket.on('joinroom', function(data){
socket.username = data.user;
socket.userPic = data.userPic;
socket.join(data.room); //push user into partitioned room.
updateUserList(data.room, true);
});
socket.on('newMessage', function(data){
socket.broadcast.to(data.room_number).emit('messagefeed', JSON.stringify(data));
});
function updateUserList(room, updateALL){
var getUsers = io.of('/messages').in(room).clients();
//console.log(getUsers);return false;
var userlist = [];
for(var i in getUsers){
userlist.push({
user:getUsers[i].username,
userPic:getUsers[i].userPic
});
}
socket.to(room).emit('updateUsersList', JSON.stringify(userlist));
if(updateALL) {
socket.broadcast.to(room).emit('updateUsersList')
}
}
socket.on('updateList', function(data){
updateUserList(data.room);
})
});
}