我正在使用NodeJS v0.4.8和最新版本的socket.io
npm install 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
遗憾的是,以下代码不会在客户端或服务器端产生任何输出.
有人有线索吗?
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"); }) ;
});
Run Code Online (Sandbox Code Playgroud)
<html> …Run Code Online (Sandbox Code Playgroud)