2015-12-16 57 views
1

我在跟着聊天example這有點舊,但我確實從這開始。socket.io無法調用未定義的方法'emit'(使用node.js和express,jade)

然而,之後把那個頁面描述所有的工作我得到以下錯誤:

TypeError: Cannot call method 'emit' of undefined 
    at /Users/juneyoungoh/Documents/Nodejs/ChatSample/app.js:109:29 
    at Array.forEach (native) 
    at userJoined (/Users/juneyoungoh/Documents/Nodejs/ChatSample/app.js:108:33) 
    at Socket.<anonymous> (/Users/juneyoungoh/Documents/Nodejs/ChatSample/app.js:62:7) 
    at Socket.EventEmitter.emit (events.js:98:17) 
    at Socket.onevent (/Users/juneyoungoh/Documents/Nodejs/ChatSample/node_modules/socket.io/lib/socket.js:330:8) 
    at Socket.onpacket (/Users/juneyoungoh/Documents/Nodejs/ChatSample/node_modules/socket.io/lib/socket.js:290:12) 
    at Client.ondecoded (/Users/juneyoungoh/Documents/Nodejs/ChatSample/node_modules/socket.io/lib/client.js:193:14) 
    at Decoder.Emitter.emit (/Users/juneyoungoh/Documents/Nodejs/ChatSample/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20) 
    at Decoder.add (/Users/juneyoungoh/Documents/Nodejs/ChatSample/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12) 

的源代碼的問題部分是

function userJoined(uName) { 
    Object.keys(socketsOfClients).forEach(function(sId){ 
    io.sockets.sockets[sId].emit('userJoined ', {'userName' : uName}); 
    }); 
} 

所以我追蹤的被攝體io.sockects和我得到了什麼在屏幕下面是

{ name: '/', 
    server: 
    { nsps: { '/': [Circular] }, 
    _path: '/socket.io', 
    _serveClient: true, 
    _adapter: [Function: Adapter], 
    _origins: '*:*', 
    sockets: [Circular], 
    eio: 
     { clients: [Object], 
     clientsCount: 1, 
     pingTimeout: 60000, 
     pingInterval: 25000, 
     upgradeTimeout: 10000, 
     maxHttpBufferSize: 100000000, 
     transports: [Object], 
     allowUpgrades: true, 
     allowRequest: [Function], 
     cookie: 'io', 
     ws: [Object], 
     _events: [Object] }, 
    httpServer: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 7, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 120000, 
     _connectionKey: '4:0.0.0.0:3000' }, 
    engine: 
     { clients: [Object], 
     clientsCount: 1, 
     pingTimeout: 60000, 
     pingInterval: 25000, 
     upgradeTimeout: 10000, 
     maxHttpBufferSize: 100000000, 
     transports: [Object], 
     allowUpgrades: true, 
     allowRequest: [Function], 
     cookie: 'io', 
     ws: [Object], 
     _events: [Object] } }, 
    sockets: 
    [ { nsp: [Circular], 
     server: [Object], 
     adapter: [Object], 
     id: 'AmJ6dbF_Y_Kus1tcAAAB', 
     client: [Object], 
     conn: [Object], 
     rooms: [Object], 
     acks: {}, 
     connected: true, 
     disconnected: false, 
     handshake: [Object], 
     _events: [Object] } ], 
    connected: 
    { AmJ6dbF_Y_Kus1tcAAAB: 
     { nsp: [Circular], 
     server: [Object], 
     adapter: [Object], 
     id: 'AmJ6dbF_Y_Kus1tcAAAB', 
     client: [Object], 
     conn: [Object], 
     rooms: [Object], 
     acks: {}, 
     connected: true, 
     disconnected: false, 
     handshake: [Object], 
     _events: [Object] } }, 
    fns: [], 
    ids: 0, 
    acks: {}, 
    adapter: 
    { nsp: [Circular], 
    rooms: { AmJ6dbF_Y_Kus1tcAAAB: [Object] }, 
    sids: { AmJ6dbF_Y_Kus1tcAAAB: [Object] }, 
    encoder: {} }, 
    _events: { connection: [Function] } } 

我完全理解爲什麼我不能得到io.sockets.sockets[sId],因爲io.sockets.sockets沒有密鑰。

要修復這種結構,我應該在connection事件上做額外的工作嗎? 我現在的連接處理程序是

io.sockets.on('connection', function(socket){ 
    socket.on('set username', function(userName){ 

    if (clients[userName] === undefined) { 
     clients[userName] = socket.id; 
     socketsOfClients[socket.id] = userName; 
     userNameAvailable(socket.id, userName); 
     userJoined(userName); 

    } else { 
     if (clients[userName] == socket.id) { 

     } else { 
     userNameAlreadyInUse(socket.id, userName); 
     } 
    } 
    }); 
} 

謝謝!

回答

2

這是socket.io io.to('socket id').emit('some event')

相關問題