我正在使用NodeJS v0.4.8和来自npm install socket.io的最新版本的socket.io。
在Ubuntu上:
Linux mars 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
不幸的是,以下代码无法在客户端或服务器端产生任何输出。
有人有线索吗?
服务器端:
在Ubuntu上:
Linux mars 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
不幸的是,以下代码无法在客户端或服务器端产生任何输出。
有人有线索吗?
服务器端:
var http = require('http'),
io = require('socket.io'),
fs = require('fs'),
sys = require('sys');
respcont = fs.readFileSync('testclient.js');
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(respcont);
});
server.listen(8082);
var socket = io.listen(server);
socket.on('connection', function(client){
sys.puts("New client is here!");
client.send("hello world");
client.on('message', function(msg) { sys.puts("client has sent:"+msg); }) ;
client.on('disconnect', function() { sys.puts("Client has disconnected"); }) ;
});
客户端
<html>
<body>
<script type="text/javascript" src="http://localhost:8082/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8082,rememberTransport:true,timeout:1500});
socket.connect();
socket.on('connect', function() {
console.log('connected to server');
socket.send('Hi Server...');
});
socket.on('message', function() {
console.log('received a message!');
});
socket.on('disconnect', function() {
console.log('disconnected from server');
});
</script>
</body>
</html>
从NodeJS输出的内容(不是sys.puts("...")调用)是:
信息 - socket.io已启动 调试 - 提供静态文件/socket.io.js 调试 - 客户端已授权 信息 - 握手 已授权 信息 - 已握手 b61a5c2751c1c8c8493db4b79d19e779