websocket.io正在提出未定义错误的方法

时间:2014-05-06 08:58:22

标签: node.js websocket stack-smash

代码

var express = require('express')
    , wsio = require('websocket.io');

/**
 * Create express app.
 */
var app = express.createServer();

/**
 * Listen
 */
app.listen(3000);


/**
 * Attach websocket server.
 */

var ws = wsio.attach(app);

/**
 * Serve your code
 */
app.use(express.static('public'));

/**
 * Listening on connections
 */
ws.on('connection', function (socket) {
    //listen on the message event and send it back
    // socket.on('message', function (msg) {
    //  console.log('   \033[96mgot:\033[39m    ' + msg);
    //  socket.send('pong');
    // });  

});

我收到此错误

.io\lib\protocols\hybi-16.js:39
    .on('text', function (packet) {
     ^
TypeError: Object #<Receiver> has no method 'on'
    at new WebSocket (H:\wamp\www\practices\nodejs\smashing\chapter10-websocket\
node_modules\websocket.io\lib\protocols\hybi-16.js:39:6)
    at Server.createClient (H:\wamp\www\practices\nodejs\smashing\chapter10-webs
ocket\node_modules\websocket.io\lib\server.js:88:10)
    at Server.handleUpgrade (H:\wamp\www\practices\nodejs\smashing\chapter10-web
socket\node_modules\websocket.io\lib\server.js:60:21)
    at HTTPServer.<anonymous> (H:\wamp\www\practices\nodejs\smashing\chapter10-w
ebsocket\node_modules\websocket.io\lib\websocket.io.js:92:8)
    at HTTPServer.EventEmitter.emit (events.js:106:17)
    at Socket.socket.ondata (http.js:1986:14)
    at TCP.onread (net.js:525:27)

1 个答案:

答案 0 :(得分:0)

您可能正在使用旧版快递。不推荐使用createServer。并且表达&gt; = 3.x不会返回http服务器。所以可能没有连接http服务器,这会引发你的错误。

表示自己需要附加到http服务器。因此,请将库和代码更新为最新标准。见this