开发者问题收集

Server.listeners 不是一个函数

2018-04-06
10499

我按照 此 websockets 教程 学习如何将 Socket.IO 集成到 Angular 项目中。我严格按照步骤操作,但当我尝试运行我的 websockets 服务器项目时,出现以下错误:

TypeError: server.listeners is not a function at Server.attach (\ws-server\node_modules\engine.io\lib\server.js:455:26)

在 server.js 的第 455 行,我们看到此代码

  // cache and clean up listeners
  var listeners = server.listeners('request').slice(0);

我很困惑为什么会看到此错误。我已更新 express 和 Socket.IO,但仍然看到相同的问题。如果需要,我可以提供 angular 代码,但我认为它不相关。

这是我的服务器代码

let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')('http');

io.on('connection', (socket) =>{
    //Log when a user connects
    console.log('user connected');

    //Log when a client disconnects
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });

    //When we receive a message event from our client, print out the 
    //contents of that message and echo it back to our clients using emit()

    socket.on('message', (message)=>{
        console.log('message received: ' + message);
        io.emit('message', {type:'new-message', text: message});
    });
});

//initialize server on port 5000
http.listen(5000, () =>{
    console.log('started on port 500');
});

这是完整的错误

TypeError: server.listeners is not a function at Server.attach (C:\Users***\Documents\poker game\ws-server\node_modules\engine.io\lib\server.js:455:26) at Function.attach (C:\Users***\Documents\poker game\ws-server\node_modules\engine.io\lib\engine.io.js:124:10) at Server.initEngine (C:\Users***\Documents\poker game\ws-server\node_modules\socket.io\lib\index.js:310:21) at C:\Users***\Documents\poker game\ws-server\node_modules\socket.io\lib\index.js:295:10 at Encoder.encode (C:\Users***\Documents\poker game\ws-server\node_modules\socket.io-parser\index.js:135:5) at Server.listen.Server.attach (C:\Users***\Documents\poker game\ws-server\node_modules\socket.io\lib\index.js:290:16) at new Server (C:\Users***\Documents\poker game\ws-server\node_modules\socket.io\lib\index.js:59:17) at Server (C:\Users***\Documents\poker game\ws-server\node_modules\socket.io\lib\index.js:44:41) at Object. (C:\Users***\Documents\poker game\ws-server\index.js:3:30) at Module._compile (module.js:652:30)

2个回答

您不小心传递了一个字符串文字 ('http') ,而不是 (http) 变量名。

将此更改为:

let http = require('http').Server(app);
let io = require('socket.io')('http');

更改为:

let http = require('http').Server(app);
let io = require('socket.io')(http);

我今天自己也犯了同样的错误。:)

AndrewJDawes
2020-05-05

将您的服务器更改为此,我尚未测试它,但它应该可以工作

 var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);

    app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
    });
   io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

    http.listen(5000, function(){
      console.log('listening on *:5000');
    });

我不熟悉 Angular,但是,这是您的代码在使用 JQuery 的前端应该是什么样子

<script>
  $(function () {
    var socket = io();
    $('form').submit(function(){
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
    socket.on('chat message', function(msg){
      $('#messages').append($('<li>').text(msg));
    });
  });
</script>
Abslen Char
2018-04-06