如何使用 Socket.io 设置使 Apache 和 Node.js 工作? [英] How to make Apache and Node.js with Socket.io setup work?

查看:29
本文介绍了如何使用 Socket.io 设置使 Apache 和 Node.js 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚从 Node.js/Socket.io 开始.最终,我想提供来自 Apache 的页面,同时使用 Node.js 服务器推送和扩充一些内容.这只是一个小测试,看看它是如何工作的.这是我的设置:

Just starting with Node.js/Socket.io. Ultimately I want to serve the pages from Apache while pushing and augmenting some of the content with Node.js server. This is just a little test to see how it all works. Here is my setup:

  • Apache 在端口 8080
  • 上运行
  • Node.js 在端口 8000
  • 上运行
  • Apache running on port 8080
  • Node.js running on port 8000

复制 index.html 页面(见下文)到它们各自的根目录.

I copied the index.html page (see below) to their respective root directories.

  1. 当我打开 http://localhost:8000 时,一切正常,我可以看到完整的消息交换.

  1. When I open http://localhost:8000 everything works as expected and I can see the complete message exchange.

当我打开 http://localhost:8080 时,一切似乎都正常(基于请求和响应标头,以及与 1 相比的 Node.js 日志.)但我可以看到 <强>无消息交换在 Node.js 或 Firebug 日志中.

When I open http://localhost:8080 everything appears to work (based on request and response headers, and Node.js log compared to 1.) but I can see no message exchange in either Node.js or Firebug log.

当我第一次打开 Node.js 提供的页面时,关闭它,然后打开 Apache 提供的页面,它可以工作.我可以在 Node.js 终端中看到CONNECTED"、DISCONNECTED"和CONNECTED"消息.

When I first open the page served by Node.js, close it, then open the page served by Apache it works. I can see "CONNECTED", "DISCONNECTED", and then "CONNECTED" messages in the Node.js terminal.

问题是:如何使案例2.起作用?

The question is: how to make case 2. work?

页面:

<script src="http://localhost:8000/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost:8000');
    socket.on('server calling', function(data) {
        console.log(data);
        socket.emit('client response', {my : 'data'});
    }); 
    socket.emit('client calling', {foo : 'bar'});
</script>
Hello!

服务器:

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

app.listen(8000);

function handler(req,res) {  

    path = req.url == "/" ? "./index.html" : "." + req.url; 
    fs.readFile(path,
        function(err, data) {
            if(err) {
                res.writeHead(500);
                return res.end('Error loading page.');
            }   
            res.writeHead(200);
            res.end(data);
        }   
    );

    io.sockets.on('connection', function (socket) {

        console.log('CONNECTED');

        setInterval(function() {            
            socket.emit('server calling', { hello: 'world' }); 
        }, 2000);

        socket.on('client response', function(data) {
            console.log(data);
        });
        socket.on('disconnect', function() {
            console.log('DISCONNECTED');
        }); 
        socket.on('client calling', function(data) {
            console.log(data);
        }); 
    });
};

我知道以下关于 SO 的问题:

I'm aware of the following questions on SO:

但我仍然无法让它工作......

but I still cannot make it work...

我试过了:

  • 将路径更改为/socket.io-client/dist/socket.io.js"

在浏览器中:ReferenceError: io is not defined.当我访问 http://localhost:8000/socket.io-client/dist/socket.io.js 时,我得到 Welcome to socket.io. 响应.

In the browser: ReferenceError: io is not defined. When I go to http://localhost:8000/socket.io-client/dist/socket.io.js I get Welcome to socket.io. response.

在服务器日志中:info - unhandled socket.io url

  • 从 Apache 静态服务器提供 socket.io.js"

这将如何运作?Apache 不知道如何处理 /socket.io/socket.io.jsnode_modules automagic?

How would that work? Apache does not know how to handle the /socket.io/socket.io.js and node_modules automagic?

  • dark_ruby 建议设置代理

我将Apache根目录下index.html页面中的脚本源改为:

I changed the script source in the index.html page in Apache root directory to:

<script src="http://localhost:8080/socket.io/"></script>

在 Apache 配置中,我添加了:

In Apache config, I added:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
ProxyPass /socket.io/ http://localhost:8000/socket.io/socket.io.js

使用这样的配置打开http://localhost:8080 与上面2.的结果相同:

With such configuration opening http://localhost:8080 had the same result as in 2. above:

$ node node-server.js
   info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized U9xPRw_0rRpsv-K9qVkS
   debug - setting request GET /socket.io/1/websocket/U9xPRw_0rRpsv-K9qVkS
   debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   debug - client authorized for 
   debug - websocket writing 1:: 
   debug - emitting heartbeat for client U9xPRw_0rRpsv-K9qVkS
   debug - websocket writing 2:: 
   debug - set heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   ... 
   info  - transport end (undefined)
   debug - set close timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - cleared close timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - cleared heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   debug - discarding transport

即没有消息.

推荐答案

您的 io.sockets 位于处理程序函数中.我假设它会在您向节点 JS 服务器发出请求时被调用.到那时,Node JS 服务器永远不会执行您的 IO 代码.

Your io.sockets is located inside handler function. I'm assuming it will be called when you make a request to node JS server. Till then, node JS server never executes your IO code.

尝试将其移出处理程序并将其添加到您的 app.js 中.

Try moving it out of the handler and add it in your app.js.

并且无需更改路径或代理.您最初发布的代码很好.问题不在于加载 socket.io.js 文件.但是节点服务器检测io事件.

And no need to change the path or the proxy. The code you posted originally is just fine. The problem is not with loading socket.io.js file. But node server detecting io events.

这篇关于如何使用 Socket.io 设置使 Apache 和 Node.js 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆