Node.js + Socket.io + Apache [英] Node.js + Socket.io + Apache

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

问题描述

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

I'm looking for a way to integrate Node.js + Socket.io + Apache in the following way: I want apache to continue serving HTML / JS files. I want node.js to listen for connection on port 8080. Something like this:

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' });
  });

});

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

if I access a HTML that connect to this server, it works, but I need to go to mydomian.com:8080/index.html. What I want is to be able to go to mydomian.com/index.html. and be able to open a socket connection:

<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>

在这个例子中,当我去8080端口时,我不得不使用fs.readFile。网址。

In this example I had to use fs.readFile of wont work when I go to the port 8080 in the URL.

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

Any suggestions? Tks.

推荐答案

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

Serve your static content from Apache port 80 and serve your dynamic/data content over a Socket.IO server on port 8080. You don't need the app = require('http').createServer(handler) in your Socket.IO app

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

Apache port 80 |-------------| clients |------------| Socket.IO port 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');
  });
});

这篇关于Node.js + Socket.io + Apache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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