繁体   English   中英

如何使客户端与同一节点进程中的服务器对话?

[英]How to make client talk to server in same node proccess?

我有一个用SSJS(节点)编写的应用程序...该应用程序需要将数据提供给fsockopen将请求的php脚本。.好的这里...该服务器需要通过持久连接从第二台服务器收集数据。 这个怎么做? 使同一过程协调这些连接? 这可能吗?

var net         = require('net');
/*  #############################################
    #  "CLIENT" Used to connect to data server
    #   --------------------------------- 
    #############################################
*/

var clientConnect   = net.createConnection(port, host);
clientConnect.setEncoding('utf8');

clientConnect.on('connect', function () {
    console.log('Client','Connected to CAGEAPI');
    clientConnect.write('user@pass');
});

clientConnectt.on('data', function (data) {
    console.log('Client','Data received: ' + data);
});

clientConnect.on('close', function(code) {
    console.log('Client','Connection closed');
});

clientConnect.on('error', function (err) {
    console.log(err);
});


/*  ################################################
    #                                
    #  "SERVER" Used to serv data to PHPScripts 
    #   --------------------------------        
    ################################################
*/
var handleServer = net.createServer(function(server) {
    console.log('Server','CONNECTED: ' + server.remoteAddress +':'+ server.remotePort);
    server.on('data', function(data) {

        console.log('Server','DATA ' + server.remoteAddress + ': ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        server.write('You said "' + data + '"');

    });

    // Add a 'close' event handler to this instance of socket
    server.on('close', function(data) {
        console.log('Server','CLOSED: ' + server.remoteAddress +' '+ server.remotePort);
    });

}).listen(port2, host2);

(客户端和服务器)都工作正常...但是如何使他们彼此交谈呢?

我认为您可能正在追求像这样的事情:

/*jslint node: true, white: true */

// Declare constructors
var DataSource, PHPClientServer;

// The DataSource class
// Handles connecting/reconnecting to the data source, and piping endpoints together
(function() {
    "use strict";

    DataSource = function(net)
    {
        this.net = net;
    };

    DataSource.prototype.net = null;

    DataSource.prototype.host = 'localhost';
    DataSource.prototype.port = 0;
    DataSource.prototype.user = '';
    DataSource.prototype.pass = '';

    DataSource.prototype.socket = null;
    DataSource.prototype.currentClient = null;

    DataSource.prototype.start = function(host, port, user, pass)
    {
        if (host !== undefined) {
            this.host = host;
        }
        if (port !== undefined) {
            this.port = port;
        }
        if (user !== undefined) {
            this.user = user;
        }
        if (pass !== undefined) {
            this.pass = pass;
        }

        this.socket = this.net.createConnection(this.port, this.host);

        this.socket.on('connect', function () {
            console.log('Data source connected');

            this.socket.write(this.user + '@' + this.pass);
        }.bind(this));

        this.socket.on('error', function() {
            console.error('Error on data source connection');

            this.stop();
            this.start();
        }.bind(this));

        this.socket.on('end', function() {
            console.error('Data source connection terminated');

            this.stop();
            this.start();
        }.bind(this));
    };

    DataSource.prototype.stop = function()
    {
        this.socket.end();
        this.socket = null;
    };

    DataSource.prototype.attachClient = function(client)
    {
        console.log('Attaching client to data source');

        this.currentClient = client;

        this.socket.pipe(this.currentClient);
        this.currentClient.pipe(this.socket, {end: false});
    };

    DataSource.prototype.detachCurrentClient = function()
    {
        console.log('Detaching client from data source');

        this.socket.unpipe(this.currentClient);
        this.currentClient.unpipe(this.socket);

        this.currentClient = null;
    };

    DataSource.prototype.hasClient = function()
    {
        return this.currentClient !== null;
    };
}());

// The PHPClientServer class
// Handles the server operations for PHP clients
(function() {
    "use strict";

    PHPClientServer = function(net, dataSource)
    {
        this.net = net;
        this.dataSource = dataSource;

        this.pendingClientStack = [];
    };

    PHPClientServer.prototype.net = null;
    PHPClientServer.prototype.dataSource = null;

    PHPClientServer.prototype.host = null;
    PHPClientServer.prototype.port = null;

    PHPClientServer.prototype.server = null;
    PHPClientServer.prototype.pendingClientStack = null;

    PHPClientServer.prototype.start = function(host, port)
    {
        var clientTerminateHandler = function() {
            console.log('Client disconnected');
            this.dataSource.detachCurrentClient();

            if (this.pendingClientStack.length) {
                console.log('Attaching next client in queue');
                this.dataSource.attachClient(this.pendingClientStack.shift());
            }
        }.bind(this);

        if (host !== undefined) {
            this.host = host;
        }
        if (port !== undefined) {
            this.port = port;
        }

        this.server = this.net.createServer(function(client) {
            console.log('Client connected');

            client.on('end', clientTerminateHandler);
            client.on('error', clientTerminateHandler);

            if (this.dataSource.hasClient()) {
                console.log('Client added to queue');
                this.pendingClientStack.push(client);
            } else {
                this.dataSource.attachClient(client);
            }
        }.bind(this));

        this.server.listen(this.port, this.host);
    };

    PHPClientServer.prototype.stop = function()
    {
        this.server.close();
        this.server = null;
    };
}());

// Bootstrap
var net, dataSource, server;

net = require('net');

dataSource = new DataSource(net);
dataSource.start('192.168.0.1', 23);

server = new PHPClientServer(net, dataSource);
server.start('0.0.0.0', 12345);

我意识到那是一堵带有最少说明的代码墙,因此请询问是否有您不了解的内容。

另外,在有人说之前,是的,我完全意识到,我正在将一种典型的OOP语言当作一种经典的Java语言来对待,即Javascript!= Java。 我不在乎,我喜欢以这种方式使用Javascript。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM