简单的nodeJS示例不适用于socket.io

发布于 2024-11-05 01:03:57 字数 1585 浏览 1 评论 0原文

一整天都在努力尝试使用 socket.io 使这个简单的示例正常工作。我最初在 Windows 7 上尝试使用 Cygwin。此后也在 OS X 上尝试过,结果相同。

运行脚本时,它显示了这一点...

2 May 20:57:47 - socket.io ready - accepting connections

但是访问index.html页面并没有显示客户端已连接。

index.html

<html>
<head>
<script type="text/javascript" src="socket.io.js"></script> 
<script type="text/javascript"> 
    var socket = new io.Socket('localhost',{'port':8090});

    socket.connect();

    socket.on('connect', function(){
        console.log('connected');
        socket.send('hi!'); 
    });

    socket.on('message', function(data){ 
        console.log('message recived: ' + data);
    });

    socket.on('disconnect', function(){
        console.log('disconected');
    });
</script> 
</head>
<body></body>
</html>

server.js

var http = require('http'), io = require('socket.io'),

server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

var socket = io.listen(server); 
socket.on('connection', function(client){ 
    console.log('client connected');

    client.on('message', function(){ 
        console.log('message arrive');
        client.send('some message');
    });

    client.on('disconnect', function(){
        console.log('connection closed');
    });
});

关于我可能做错了什么有什么想法吗?没有显示任何控制台消息。值得注意的是,当我使用 Firebug 查看 index.html 页面时,没有嵌入任何脚本,这很奇怪......不知道是什么原因造成的。

Have been struggling all day trying to make this simple example work using socket.io. I've tried initially on Windows 7 with Cygwin. Have since also tried on OS X, with the same result.

When running the script, it shows this...

2 May 20:57:47 - socket.io ready - accepting connections

But visiting the index.html page doesnt show a client has even connected.

index.html

<html>
<head>
<script type="text/javascript" src="socket.io.js"></script> 
<script type="text/javascript"> 
    var socket = new io.Socket('localhost',{'port':8090});

    socket.connect();

    socket.on('connect', function(){
        console.log('connected');
        socket.send('hi!'); 
    });

    socket.on('message', function(data){ 
        console.log('message recived: ' + data);
    });

    socket.on('disconnect', function(){
        console.log('disconected');
    });
</script> 
</head>
<body></body>
</html>

server.js

var http = require('http'), io = require('socket.io'),

server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

var socket = io.listen(server); 
socket.on('connection', function(client){ 
    console.log('client connected');

    client.on('message', function(){ 
        console.log('message arrive');
        client.send('some message');
    });

    client.on('disconnect', function(){
        console.log('connection closed');
    });
});

Any ideas on what I could be doing wrong? No console messages whatsoever are being displayed. Notably, when i use Firebug to look at the index.html page, no scripts are being embedded, which is odd..Not sure what could be causing that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独行侠 2024-11-12 01:03:57

您没有在index.html 文件中正确加载socket.io 库。试试这个:

<script type="text/javascript" src="http://localhost:8090/socket.io/socket.io.js"></script> 

You're not loading the socket.io library properly in your index.html file. Try this:

<script type="text/javascript" src="http://localhost:8090/socket.io/socket.io.js"></script> 
浪菊怪哟 2024-11-12 01:03:57

您没有提供 socket.io.js (或 flash 文件)。

我建议使用 CDN:

或使用 < a href="http://expressjs.com/" rel="noreferrer">express 来提供 socket.io.js 文件。

编辑:

错误实际上仔细观察你也没有提供index.html
再次表达可以工作但对于简单的例子:

var fs = require('fs');
var index = fs.readFileSync('index.html');
//note the readFileSync is done only in the first tic
.
.
.
res.writeHead(200, {'Content-Type': 'text/html'}); 
res.end(index); 

You're not serving up socket.io.js (or the flash file).

I'd recomend using the CDN:

<script src="http://cdn.socket.io/stable/socket.io.js"></script>

or alternatively use express to serve the socket.io.js file.

edit:

err actually looking closer you're also not serving up index.html
again express could work but for the simple example:

var fs = require('fs');
var index = fs.readFileSync('index.html');
//note the readFileSync is done only in the first tic
.
.
.
res.writeHead(200, {'Content-Type': 'text/html'}); 
res.end(index); 
蓝海似她心 2024-11-12 01:03:57

在客户端使用它作为路径!

<script type="text/javascript" src="/socket.io/socket.io.js"></script> 

Use this on the client side as the path !

<script type="text/javascript" src="/socket.io/socket.io.js"></script> 
゛清羽墨安 2024-11-12 01:03:57

是的,并评论以下行:

// server.listen(8090);

yes, and comment the following line:

// server.listen(8090);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文