简体   繁体   English

NodeJS和Socket.IO如何为socket.io.js提供服务

[英]How NodeJS and Socket.IO serve socket.io.js

I was learning NodeJS and Socket.IO via following code from a book(Learning Node). 我通过以下代码来学习NodeJS和Socket.IO(学习节点)。 The example worked. 这个例子很有效。 But I want to know how the node is serving the socket.io.js file in the statment <script src="/socket.io/socket.io.js"></script> Because there is no folder named socket.io in the project root and I don't have any code written to server static file. 但我想知道节点如何在statment <script src="/socket.io/socket.io.js"></script>提供socket.io.js文件,因为没有名为socket.io的文件夹在项目根目录中,我没有任何代码写入服务器静态文件。 Is this done via Socket.IO module? 这是通过Socket.IO模块完成的吗? Will it conflict if I use express to serve static files? 如果我使用express来提供静态文件会有冲突吗?

Client Side code 客户端代码

<html lang="en">
<head>
<meta charset="utf-8">
<title>bi-directional communication</title>
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost:8124');
    socket.on('news', function (data) {
        var html = '<p>' + data.news + '</p>';
        document.getElementById("output").innerHTML=html;
        socket.emit('echo', { back: data.news });
    });
</script>
</head>
<body>
    <div id="output"></div>
</body>
</html>

server side code 服务器端代码

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')

var counter;
app.listen(8124);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
    function (err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }
        counter = 1;
        res.writeHead(200);
        res.end(data);
    });
}

io.sockets.on('connection', function (socket) {
    socket.emit('news', { news: 'world' });
    socket.on('echo', function (data) {
        if (counter <= 50) {
            counter++;
            console.log(data.back);
            socket.emit('news', {news: data.back});
        }
    });
});

After some reading I got the explanation 经过一番阅读后,我得到了解释

In the server application, when the HTTP web server was created, it was passed to the Socket.IO's listen event: 在服务器应用程序中,创建HTTP Web服务器时,它被传递给Socket.IO的listen事件:

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)

What happens is that Socket.IO intercepts requests sent to the web server and listens for requests for: 会发生什么事情是Socket.IO拦截发送到Web服务器的请求并侦听以下请求:

/socket.io/socket.io.js

Socket.IO does a clever bit of behind-the-scenes finagling that determines what's re- turned in the response. Socket.IO做了一个巧妙的幕后故事,确定了响应中的内容。 If the client supports WebSockets, the JavaScript file returned is one that uses WebSockets to implement the client connection. 如果客户端支持WebSockets,则返回的JavaScript文件是使用WebSockets实现客户端连接的JavaScript文件。 If the client doesn't support WebSockets, but does support Forever iFrame (IE9), it returns that particular JavaScript client code, and so on. 如果客户端不支持WebSockets,但支持Forever iFrame(IE9),则会返回该特定的JavaScript客户端代码,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM