Node.js + Socket.io + Apache

osc*_*arm 6 javascript websocket node.js socket.io

我正在寻找一种方法来以下列方式集成Node.js + Socket.io + Apache:我希望apache继续提供HTML/JS文件.我希望node.js监听端口8080上的连接.这样的事情:

var util = require("util"),
    app = require('http').createServer(handler),
    io = require('/socket.io').listen(app),
    fs = require('fs'),
    os = require('os'),
    url = require('url');

app.listen(8080);

function handler (req, res) {

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

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });

  socket.on('my other event', function (data) {
    socket.emit('ok 1', { hello: 'world' });
  });

  socket.on('clientMSG', function (data) {
    socket.emit('ok 2', { hello: 'world' });
  });

});
Run Code Online (Sandbox Code Playgroud)

如果我访问连接到此服务器的HTML,它可以工作,但我需要访问mydomian.com:8080/index.html.我想要的是能够访问mydomian.com/index.html.并能够打开套接字连接:

<script>
        var socket = io.connect('http://mydomain.com', {port: 8080});
        socket.on('news', function (data) {
            console.log(data);
            socket.emit('my other event', { my: 'data from the client' });
        });

        socket.on('connect', function (data) {
            console.log("connect");
        });

        socket.on('disconnect', function (data) {
            console.log("disconnect");
        });


            //call this function when a button is clicked
        function sendMSG()
        {
            console.log("sendMSG"); 
            socket.emit('clientMSG', { msg: 'non-scheduled message from client' });
        }

    </script>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,当我转到URL中的端口8080时,我不得不使用fs.readFile.

有什么建议?韩国社交协会.

Ehe*_*Tov 13

从Apache端口80提供静态内容,并通过端口8080上的Socket.IO服务器提供动态/数据内容.您不需要app = require('http').createServer(handler)在Socket.IO应用程序中使用

Apache端口80 | ------------- | 客户| ------------ | Socket.IO端口8080

var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  io.sockets.emit('this', { will: 'be received by everyone'});

  socket.on('clientMSG', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    sockets.emit('user disconnected');
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 这不会触发"同源政策"违规吗?http://en.wikipedia.org/wiki/Same_origin_policy (2认同)

Phi*_*Enc 5

AWS + APACHE + NODEJS + SOCKET.IO + ANGULARJS

SERVER SIDE
这对我在端口80上运行apache和端口8000上的NodeJS的生产服务器上工作.通过所需选项更改NodeJS端口...

  1. 在/ var/www/html中为NodeJS服务器的文件创建名为"nodejs"的文件夹
  2. 在不同于80的端口上运行Nodej,例如端口8000
  3. 执行命令:a2enmod proxy_http
  4. 执行命令:a2enmod proxy_wstunnel
  5. 将接下来的两行放在以下文件的末尾:/etc/apache2/apache2.conf

    LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
    LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so 
    
    Run Code Online (Sandbox Code Playgroud)
  6. 将接下来的12行放在以下文件的末尾:/sites-available/000-default.conf
    (如果您创建了不同的站点,请将行放在那里)

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^socket.io          [NC]
    RewriteCond %{QUERY_STRING} transport=websocket [NC]
    RewriteRule /{.*}       ws://localhost:8000/$1  [P,L]
    
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteRule /(.*) ws://localhost:8000/$1 [P,L]
    
    ProxyPass /nodejs http://localhost:8000/
    ProxyPassReverse /nodejs http://localhost:8000/
    
    
    ProxyPass /socket.io http://localhost:8000/socket.io
    ProxyPassReverse /socket.io http://loacalhost:8000/socket.io
    
    ProxyPass /socket.io ws://localhost:8000/socket.io
    ProxyPassReverse /socket.io ws://localhost:8000/socket.io
    
    Run Code Online (Sandbox Code Playgroud)
  7. sudo service apache2 restart


客户端
我使用以下在AngularJS中实现Socket.io,但我认为本指南
对socket.io技术的基本Javascript实现也很有用.

要调用我的PHP服务器:example.com
要调用NodeJS服务器:example.com/nodejs
要调用NodeJS Socket:example.com <---此调用将默认由库完成

希望对你有所帮助!