node.js + socket.io example: client connects but socket.send() from client to server fails
server code
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'); })
});
client code
<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>
looks like node.js HEAD was broken.
git pull to 0474ce67908c9afddab69d3f0eb53564b10e2ad1 fixed the problem.
精彩评论