5

我在端口 8484 上运行闪存套接字策略服务器。在同一个端口上,我需要接收 http 请求。我正在考虑检查是否请求了策略文件(在下面的 if 语句中),如果没有 - 将 http 请求转发到正在运行 express 的另一个端口(比如说 localhost:3000)。我怎样才能得到它?

// flash socket policy server
var file = '/etc/flashpolicy.xml',
    host = 'localhost',
    port =  8484,
    poli = 'something';

var fsps = require('net').createServer(function (stream) {
    stream.setEncoding('utf8');
    stream.setTimeout(10000);
    stream.on('connect', function () {
        console.log('Got connection from ' + stream.remoteAddress + '.');
    });
    stream.on('data', function (data) {
        console.log(data);
        var test = /^<policy-file-request\/>/;
        if (test.test(data)) {
            console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
            stream.end(poli + '\0');
        } else {
            console.log('Not a policy file request ' + stream.remoteAddress + '.');
            stream.end('HTTP\0');

            // FORWARD REQUEST TO localhost:3000 for example //

        }
    });
    stream.on('end', function () {
        stream.end();
    });
    stream.on('timeout', function () {
        console.log('Request from ' + stream.remoteAddress + ' timed out.');
        stream.end();
    });
});

require('fs').readFile(file, 'utf8', function (err, poli) {
    if (err) throw err;
    fsps.listen(port, host);
    console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});
4

1 回答 1

3

我不久前解决了这个问题,但忘记了这个问题:) 解决方案是创建一个套接字,它可以在 http express 服务器和 tcp flash 策略服务器之间发送和检索数据。

闪存策略服务器:

var file = process.argv[2] || '/etc/flashpolicy.xml',
    host = process.argv[3] || 'localhost',
    port = process.argv[4] || 8484,
    poli = 'flash policy data\n',
    net  = require('net'),
    http = require('http');

var fsps = net.createServer(function (stream) {
    stream.setEncoding('utf8');
    stream.on('connect', function () {
        console.log('Got connection from ' + stream.remoteAddress + '.');
    });
    stream.on('data', function (data) {
        var test = /^<policy-file-request\/>/;
        if (test.test(data)) {
            console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
            stream.end(poli + '\0');
        } else {
            console.log('Not a policy file request ' + stream.remoteAddress + '.');

            var serviceSocket = new net.Socket();
            serviceSocket.connect(3000, 'localhost', function () {
                console.log('>>>> Data from 8484 to 3000 >>>>\n', data.toString());
                serviceSocket.write(data);
            });
            serviceSocket.on("data", function (received_data) {
                console.log('<<<< Data from 3000 to 8484 to client <<<<\n', received_data.toString());
                stream.write(received_data);
            });
        }
    });
    stream.on('end', function () {
        console.log('tcp server disconnected');
    });
    stream.on('timeout', function () {
        console.log('Request from ' + stream.remoteAddress + ' timed out.');
    });
});

require('fs').readFile(file, 'utf8', function (err, poli) {
    if (err) throw err;
    fsps.listen(port, host);
    console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});

localhost:3000 上的示例快递服务器:

var express = require('express')
  , http = require('http')
  , path = require('path')
  , app = express();

// all environments
app.set('port', process.env.PORT || 3000);

app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', function(req, res){
  res.send('Proper HTTP response');
});

app.post('/', function(req, res){
  console.log(req.body);
  res.send('Proper HTTP response');
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express HTTP server listening on port ' + app.get('port'));
});
于 2014-05-10T11:46:29.507 回答