NodeJS + socket.io:简单的客户端/服务器示例不起作用
我正在使用 NodeJS v0.4.8 和最新版本的 socket.io
npm 安装 socket.io
:
Linux mars 2.6.38-8-generic #42-Ubuntu SMP 周一 4 月 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
I’m using NodeJS v0.4.8 and the latest Version of socket.io from
npm install socket.io
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
The following code unfortunately doesn't produce any output, wheter on client, nor on server side.
Does anybody have a clue?
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"); }) ;
});
CLIENT-SIDE
<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>
The output from NodeJS (NOT the sys.puts("...") calls) is:
info - socket.io started debug -
served static /socket.io.js debug -
client authorized info - handshake
authorized info - handshaken
b61a5c2751c1c8c8493db4b79d19e779
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Express 3.0 + Socket.io 工作示例
服务器 (app.js)
客户端 (index.html)
您可以使用下面的链接 fork 代码
https://github.com/sreekumar-kr/Expree3.0-- -Socket.IO
Express 3.0 + Socket.io working example
server ( app.js )
client ( index.html )
you can fork the code using the link below
https://github.com/sreekumar-kr/Expree3.0---Socket.IO
我也(像 Derrish 一样)喜欢使用 express 框架来简化我的工作(太棒了:) )。您可以从 http://dl.dropbox.com/u/ 下载并提取此示例314941/socketio.zip。我相信您甚至不必安装这些模块,因为我已经在本地捆绑了它们(只需运行),这要感谢 npm :)。
如何安装:
代码:
app.js:
public/index.html:
我的模块列表:
已安装的模块(不是必需的):
浏览器将显示:
socket.io
在启动时,但您可能看不到它,因为它将被connected
替换。connected
当用户连接到 socket.io 时。等待两秒!
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 :).
How to install:
The code:
app.js:
public/index.html:
Listing of my modules:
Installed modules(NOT necessary):
Browser will display:
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.Waited two seconds!
我采用了您的示例并将其放入使用 Express 的节点应用程序中。您的 HTML 代码已放置在 public 下的静态 HTML 文件中。你的例子效果很好。代码如下所示。我想确保 socket.io 脚本文件和 HTML 文件都能正确提供。
I took your example and dropped it in an a node app using express. Your HTML code was placed in a static HTML file under public. Your example worked fine. The code is shown below. I wanted to make sure both the socket.io script file and the HTML file were being served up properly.