socket.io无法调用未定义的方法'emit'(使用node.js和express,jade)

时间:2022-04-20 19:42:27

I am following the chatting example Which is little bit old but I did start with this.

我正在关注这个有点老的聊天示例,但我确实从这开始。

However, after did all jobs described in that page I got error below :

但是,在完成该页面中描述的所有工作后,我收到以下错误:

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)

The problem part of source code is

源代码的问题部分是

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

So I track the object io.sockects and what I got in screen is below

所以我跟踪对象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] } }

I fully understand why I can not get io.sockets.sockets[sId], since io.sockets.sockets has no key.

我完全理解为什么我不能得到io.sockets.sockets [sId],因为io.sockets.sockets没有密钥。

To fix this structure, should I do extra job on connection event? My connection handler for now is

要修复此结构,我应该在连接事件上做额外的工作吗?我现在的连接处理程序是

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);
      }
    }
  });
}

Thanks !

2 个解决方案

#1


2  

This is new version of socket.io io.to('socket id').emit('some event')

这是socket.io io.to的新版本('socket id')。emit('some event')

#2


0  

That blog post is many years too old..

这篇博文很多年了......

Take a look at the updated chat demo:

看一下更新的聊天演示:

http://socket.io/get-started/chat/

And, you can download the source here:

并且,您可以在此处下载源代码:

https://github.com/rauchg/chat-example

#1


2  

This is new version of socket.io io.to('socket id').emit('some event')

这是socket.io io.to的新版本('socket id')。emit('some event')

#2


0  

That blog post is many years too old..

这篇博文很多年了......

Take a look at the updated chat demo:

看一下更新的聊天演示:

http://socket.io/get-started/chat/

And, you can download the source here:

并且,您可以在此处下载源代码:

https://github.com/rauchg/chat-example