node.js + socket.io示例:客户端连接但socket.send()从客户端到服务器失败

时间:2011-02-18 19:48:05

标签: node.js comet socket.io

服务器代码

var http = require('http'),  
io = require('socket.io'),
fs = require('fs');

respcont = fs.readFileSync('socketio.client.js');


server = http.createServer(function(req, res){ 
 // your normal server code 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end(respcont);
});
server.listen(8080);

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
  // new client is here! 
    client.on('message', function(){ console.log('message arrived'); }) 
    client.on('disconnect', function(){ console.log('disconnected'); }) 
}); 

客户代码

<html>
<body>
<script src="/socket.io/socket.io.js"></script> 
<script> 
    var socket = new io.Socket(null,{port:8080,rememberTransport:true,timeout:1500});
 socket.connect();
socket.on('connect', function(){ console.log('connected to server'); socket.send('hi there, this is a test message'); }) 
socket.on('message', function(){ console.log('recieved a message!'); }) 
socket.on('disconnect', function(){ console.log('disconnected from server'); }) 
</script> 
</body>
</html>

1 个答案:

答案 0 :(得分:2)

看起来像node.js HEAD坏了。

git pull to 0474ce67908c9afddab69d3f0eb53564b10e2ad1解决了这个问题。