NodeJS + socket.io:简单的客户端/服务器示例无法正常工作

hel*_*lix 7 javascript comet node.js socket.io

我正在使用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>
<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>
Run Code Online (Sandbox Code Playgroud)

NodeJS的输出(不是sys.puts("...")调用)是:

info - socket.io启动调试 - 服务静态/socket.io.js调试 - 客户端授权信息 - 握手授权信息 - handshaken b61a5c2751c1c8c8493db4b79d19e779

Alf*_*red 3

我也(像 Derrish 一样)喜欢使用Express框架来简化我的工作(太棒了:))。您可以从http://dl.dropbox.com/u/314941/socketio.zip下载并提取此示例。我相信你甚至不必安装这些模块,因为我已经在本地捆绑了它们(只需运行),这要归功于 npm :)。

\n\n

如何安装:

\n\n
alfred@alfred-laptop:~/tmp/socketio$ uname -a\nLinux alfred-laptop 2.6.35-28-generic #50-Ubuntu SMP Fri Mar 18 19:00:26 UTC 2011 i686 GNU/Linux\nalfred@alfred-laptop:~/tmp$ wget http://dl.dropbox.com/u/314941/socketio.zip\nalfred@alfred-laptop:~/tmp$ unzip socketio.zip\nalfred@alfred-laptop:~/tmp$ cd socketio/\nalfred@alfred-laptop:~/tmp/socketio$ node -v\nv0.4.7\nalfred@alfred-laptop:~/tmp/socketio$ npm -v\n1.0.6\nalfred@alfred-laptop:~/tmp/socketio$ node app.js\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码:

\n\n

应用程序.js:

\n\n
// npm install express\n// npm install socket.io\n\nvar sys         = require(\'sys\'),\n        express = require(\'express\'),\n        app         = express.createServer(\'127.0.0.1\'),\n        io          = require(\'socket.io\'); \n\napp.use(express.static(__dirname + \'/public\'));\n\napp.get(\'/\', function (req, res) {\n    res.send(\'Hello World\');\n});\n\napp.listen(3000);\n\nvar socket = io.listen(app); \n\nsocket.on(\'connection\', function (client){ \n  // new client is here!\n  setTimeout(function () {\n        client.send(\'Waited two seconds!\');\n    }, 2000);\n\n  client.on(\'message\', function () {\n  }) ;\n\n  client.on(\'disconnect\', function () {\n  });\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

公共/index.html:

\n\n
<html>\n<p id="text">socket.io</p>\n\n<script src="socket.io/socket.io.js"></script> \n<script src="jquery-1.6.1.min.js"></script><!-- Downloaded Jquery -->\n\n<script> \n    $(document).ready(function(){\n\n        var socket  = new io.Socket(),\n                text        = $(\'#text\');\n\n        socket.connect();\n\n        socket.on(\'connect\', function () {\n            text.html(\'connected\');\n        });\n\n        socket.on(\'message\', function (msg) {\n            text.html(msg);\n        });\n\n        socket.on(\'disconnect\', function () {\n            text.html(\'disconnected\');\n        });\n\n    });\n</script> \n
Run Code Online (Sandbox Code Playgroud)\n\n

我的模块列表:

\n\n
alfred@alfred-laptop:~/tmp/socketio$ npm ls\n/home/alfred/tmp/socketio\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac express@2.3.11 \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 connect@1.4.6 \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mime@1.2.2 \n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 qs@0.1.0 \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 socket.io@0.6.18\n
Run Code Online (Sandbox Code Playgroud)\n\n

安装的模块(非必需):

\n\n
npm install express\nnpm install socket.io\n
Run Code Online (Sandbox Code Playgroud)\n\n

浏览器会显示:

\n\n
    \n
  1. socket.io开始时,但您可能看不到它,因为它将被替换为connected.
  2. \n
  3. connected当用户连接到 socket.io 时。
  4. \n
  5. 2秒后显示Waited two seconds!图像
  6. \n
\n