我有个问题。
客户端代码
<html>
<body onload="fun()">
<script src="C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js"></script>
<script>
function fun()
{
alert("hello")
var socket = new io.Socket('localhost',{'port':8090});
socket.on('connect', function(){
socket.send('hi!');
})
socket.on('connect', function(){ console.log('connected to server');socket.send('hi there, this is a test message'); })
socket.on('message', function(data){
alert(data);
})
socket.on('disconnect', function(){})
}
</script>
</body>
</html>
服务器端代码:
var http = require('http'),
io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')
server = http.createServer(function(req, res){
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(8090);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ console.log('message arrive'); })
client.on('disconnect', function(){ console.log('connection closed');})
});
从 socket.io 找到这个例子。当我运行服务器时,它给了我 Socket io 已准备就绪。当我运行浏览器时接受连接它没有显示任何内容,并且在 firefox firebug 控制台上请帮助我解决这个问题。