整天都在努力尝试使用 socket.io 使这个简单的示例工作。我最初在 Windows 7 上使用 Cygwin 进行了尝试。此后也尝试过 OS X,结果相同。
运行脚本时,它显示...
2 May 20:57:47 - socket.io ready - accepting connections
但是访问 index.html 页面并没有显示客户端甚至已连接。
索引.html
<html>
<head>
<script type="text/javascript" src="socket.io.js"></script>
<script type="text/javascript">
var socket = new io.Socket('localhost',{'port':8090});
socket.connect();
socket.on('connect', function(){
console.log('connected');
socket.send('hi!');
});
socket.on('message', function(data){
console.log('message recived: ' + data);
});
socket.on('disconnect', function(){
console.log('disconected');
});
</script>
</head>
<body></body>
</html>
服务器.js
var http = require('http'), io = require('socket.io'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(8090);
var socket = io.listen(server);
socket.on('connection', function(client){
console.log('client connected');
client.on('message', function(){
console.log('message arrive');
client.send('some message');
});
client.on('disconnect', function(){
console.log('connection closed');
});
});
关于我可能做错了什么的任何想法?没有显示任何控制台消息。值得注意的是,当我使用 Firebug 查看 index.html 页面时,没有嵌入任何脚本,这很奇怪..不确定是什么原因造成的。