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

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

问题描述

刚开始使用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://本地主机:8000 一切正常,我可以看到完整的信息交换

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

当我打开的http://本地主机:8080 一切似乎工作(根据请求和响应头,和Node.js的日志相比,1。 ),但我可以看到否消息交换在任的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的终端则已连接的消息。

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:

  • Node.js + Socket.io + Apache
  • node.js and apache on diffrent servers

但我仍然不能使它工作...

but I still cannot make it work...

我试过:


  • 改变路径/socket.io-client/dist/socket.io.js

在浏览器中:的ReferenceError:没有定义IO 的。当我去的http://本地主机:8000 / socket.io客户端/距离/ socket.io.js 我得到的欢迎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.

在服务器日志:信息 - 未处理socket.io网址


  • 服务与Apache静态服务器socket.io.js

如何将这项工作? Apache不知道如何处理 /socket.io/socket.io.js node_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建议成立一个代理

我改变了脚本源在index.html页面在Apache的根目录下:

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://本地主机: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服务器发出请求,将被调用。至此,节点JS服务器永远不会执行你的IO code。

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.

和无需更改路径或代理。你最初发布的code是就好了。问题是不与装载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.

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

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