【问题标题】:cannot call method 'on' of undefined at object无法在对象处调用未定义的方法“on”
【发布时间】:2015-03-26 13:40:52
【问题描述】:

当我尝试在 node.js 命令提示符中运行 node app.js 时,我收到一条错误消息

cannot call method 'on' of undefined at object

这是我的代码:

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

    server.listen(3000);

app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});

io.socket.on('connection', function(socket) {
socket.on('send message', function(data){
    io.socket.emit('new message', data);
});
});

出了什么问题?

【问题讨论】:

  • io.socketsocket,其中一个或两个未定义,请检查它们。

标签: javascript node.js socket.io chat


【解决方案1】:
  1. io.socket.on 应该是 io.on
  2. io.socket.emit 应该是 socket.emit

如:

io.on('connection', function(socket) {
  socket.on('send message', function(data){
    socket.emit('new message', data);
  );
});

【讨论】:

    猜你喜欢
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2011-06-04
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多