使用apache中的htaccess文件隐藏Socket IO app的端口号

时间:2015-11-17 22:05:39

标签: node.js apache .htaccess socket.io

我在端口1234上运行了一个节点js socket io聊天应用程序,我希望url www.example.com:1234/chat使用htaccess文件命中socket.io/?EIO=3&transport=polling&t=1447797482690。 我设置聊天应用程序显示的位置,但套接字IO实际上不起作用,我得到RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ http://www.example.com:1234/ [P,L] 目前我正在使用Pm2来部署node.js应用程序。我不确定是否有socketio有一个错误或者我只是在我的代码中遗漏了一些东西。任何帮助表示赞赏。

继承人:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/chat', function(req, res){
  res.sendFile(__dirname + '/chat.html');
});
io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
      console.log('user disconnected');
    });
    socket.on('chat message', function(msg){
      console.log('message: ' + msg);
    });
    socket.on('chat message', function(msg){
      io.emit('chat message', msg);
    });
});
http.listen(1234, function(){
  console.log('listening on *:1234');
 });

和index.js:

NETCDF

1 个答案:

答案 0 :(得分:0)

我做的比这个代码:

在.htaccess中:

 RewriteEngine on 
 RewriteRule ^/?socket.io http://localhost:58000/socket.io/socket.io.js

和我的file.js:

var http = require('http'),
fs = require('fs');

var app = http.createServer(function (request, response) 
{
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Notif.js RESPONSE');
}).listen(XXXXX);

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

io.sockets.on('connection', function(socket) 
{ 
    console.log('New Connection from '+remoteAddr+':'+remotePort);

    socket.on('message_to_client', function(data)  //message_to_server_connection
    { 
        var message2 = "Mensaje from server: "+data["message"];  //sanitize(data["message"]).escape();  //
        io.sockets.emit("message_to_client",
        { 
            message: message2 
        });              
    });
    socket.on('message_to_server_connection', function(data)  //message_to_server_connection
    { 
        io.sockets.emit("message_to_client",
        { 
            message: "Connected...... ";
        }); 
    });
});

console.log('WebSocket Server listening on localhost:XXXXX');

当我在浏览器地址栏http://..../socket.io中写字时,nodejs响应是正确的:

  Notif.js RESPONSE

我的问题是如何编写de javascript代码以连接到nodeJS,避免写入真正的目标端口:

        var socketio= io.connect("localhost:XXXXX");            

        socketio.on("connect", function(data) 
        {
            socketio.emit("message_to_server_connection", 
            { 
                message : "Connected"
            });
        });

所以我想通过端口80路由此流量