带socket.io的Websocket服务器不起作用

时间:2014-06-15 19:27:34

标签: websocket socket.io

我想创建一个socket.io websocket服务器,但它不起作用。我的剧本出了什么问题?

脚本:

http://pastebin.com/eJ4Eny0w

1 个答案:

答案 0 :(得分:0)

server.listen(...)放在最后。此外,您的socket=require(...))声明似乎已关闭。将其更改为var io = require('socket.io').listen(server);

代码是:

var server = require( 'http' ).createServer();
server.listen( 4444 );
var socket = require( 'socket.io' );
var io = socket.listen( server );   
console.log('Started!');
io.on('connection', function(socket){
    console.log('Connected');
    socket.on('message', function(data) {
        console.log('Debug!');
    });
});

应该是:

var server = require( 'http' ).createServer();
var io = require('socket.io').listen(server);  
console.log('Started!');
io.on('connection', function(socket){
    console.log('Connected');
    socket.on('message', function(data) {
        console.log('Debug!');
    });
});
server.listen( 4444 );