NodeJS + socket.io:简单的客户端/服务器示例不起作用 [英] NodeJS + socket.io: simple Client/Server example not working
问题描述
我使用的是 NodeJS v0.4.8 和最新版本的 socket.io
I’m using NodeJS v0.4.8 and the latest Version of socket.io from
npm install socket.io
npm install socket.io
在 Ubuntu 上:
on Ubuntu:
Linux mars 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
Linux mars 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
不幸的是,以下代码不会产生任何输出,无论是在客户端还是在服务器端.
The following code unfortunately doesn't produce any output, wheter on client, nor on server side.
有人知道吗?
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("...") 调用)是:
The output from NodeJS (NOT the sys.puts("...") calls) is:
信息 - socket.io 开始调试 -提供静态/socket.io.js 调试 -客户端授权信息 - 握手授权信息 - 握手b61a5c2751c1c8c8493db4b79d19e779
info - socket.io started debug - served static /socket.io.js debug - client authorized info - handshake authorized info - handshaken b61a5c2751c1c8c8493db4b79d19e779
推荐答案
我也(像 Derrish)喜欢用 express 简化我的工作的框架(真棒:)).您可以从 http://dl.dropbox.com/u/下载并提取此示例314941/socketio.zip.我相信您甚至不必安装这些模块,因为我已经将它们捆绑在本地(只需运行),这要感谢 npm :).
I also(like Derrish) like to use express framework to simplify my work(AWESOME :)). You can download and extract this sample from http://dl.dropbox.com/u/314941/socketio.zip. I believe you don't even have to install these modules because I have bundled them locally(just run) thanks to npm :).
alfred@alfred-laptop:~/tmp/socketio$ uname -a
Linux alfred-laptop 2.6.35-28-generic #50-Ubuntu SMP Fri Mar 18 19:00:26 UTC 2011 i686 GNU/Linux
alfred@alfred-laptop:~/tmp$ wget http://dl.dropbox.com/u/314941/socketio.zip
alfred@alfred-laptop:~/tmp$ unzip socketio.zip
alfred@alfred-laptop:~/tmp$ cd socketio/
alfred@alfred-laptop:~/tmp/socketio$ node -v
v0.4.7
alfred@alfred-laptop:~/tmp/socketio$ npm -v
1.0.6
alfred@alfred-laptop:~/tmp/socketio$ node app.js
代码:
app.js:
// npm install express
// npm install socket.io
var sys = require('sys'),
express = require('express'),
app = express.createServer('127.0.0.1'),
io = require('socket.io');
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.send('Hello World');
});
app.listen(3000);
var socket = io.listen(app);
socket.on('connection', function (client){
// new client is here!
setTimeout(function () {
client.send('Waited two seconds!');
}, 2000);
client.on('message', function () {
}) ;
client.on('disconnect', function () {
});
});
public/index.html:
<html>
<p id="text">socket.io</p>
<script src="socket.io/socket.io.js"></script>
<script src="jquery-1.6.1.min.js"></script><!-- Downloaded Jquery -->
<script>
$(document).ready(function(){
var socket = new io.Socket(),
text = $('#text');
socket.connect();
socket.on('connect', function () {
text.html('connected');
});
socket.on('message', function (msg) {
text.html(msg);
});
socket.on('disconnect', function () {
text.html('disconnected');
});
});
</script>
我的模块列表:
alfred@alfred-laptop:~/tmp/socketio$ npm ls
/home/alfred/tmp/socketio
├─┬ express@2.3.11
│ ├── connect@1.4.6
│ ├── mime@1.2.2
│ └── qs@0.1.0
└── socket.io@0.6.18
已安装的模块(非必需):
npm install express
npm install socket.io
浏览器将显示:
socket.io
在启动时,但您可能看不到它,因为它将被替换为connected
.connected
当用户连接到 socket.io 时.- 2 秒后会显示
Waited 2 seconds!
socket.io
on start, but probably you can't see this because it will be replaced withconnected
.connected
when the user connects to socket.io.- After 2 seconds it will display
Waited two seconds!
这篇关于NodeJS + socket.io:简单的客户端/服务器示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!