コード例 #1
0
dnode.prototype.connect = function () {
    var self = this;
    var params = protocol.parseArgs(arguments);
    var client = self.proto.create();
    
    if (!params.port) params.port = parseInt(window.location.port, 10);
    if (isNaN(params.port)) delete params.port;
    var sock = client.socketio = new io.Socket(
        params.host || window.location.hostname,
        params
    );
    
    client.end = function () {
        sock.disconnect();
    };
    
    sock.on('connect', function () {
        client.start();
        self.emit('connect');
    });
    
    sock.on('disconnect', function () {
        client.emit('end');
        self.emit('end');
    });
    
    sock.on('message', client.parse);
    
    client.on('request', function (req) {
        sock.send(JSON.stringify(req) + '\n');
    });
    
    if (params.block) {
        client.on('remote', function () {
            params.block.call(client.instance, client.remote, client);
        });
    }
    
    this.stack.forEach(function (middleware) {
        middleware.call(client.instance, client.remote, client);
    });
    
    sock.connect();
};
コード例 #2
0
ファイル: index.js プロジェクト: BrazilianJoe/dnode
dnode.prototype.connect = function () {
    var params = protocol.parseArgs(arguments);
    var stream = params.stream;
    var client = null;
    var self   = this;  
    
    if (params.port) {
        params.host = params.host || '127.0.0.1';
        if (params.key) {
            var options = { key: params.key, cert: params.cert };
            stream = tls.connect(params.port, params.host, options, function() {
                attachDnode();
            });
        }
        else {
            stream = net.createConnection(params.port, params.host);
            stream.on('connect', function() {
                attachDnode();              
            });
        }
    }
    else {
      attachDnode();
    } 
    
    stream.remoteAddress = params.host;
    stream.remotePort = params.port;
    
    var args = arguments; 
    
    if (params.reconnect) {
        stream.on('error', (function (err) {
            if (err.code === 'ECONNREFUSED') {
                if (client) client.emit('refused');
                
                setTimeout((function () {
                    if (client) client.emit('reconnect');
                    dnode.prototype.connect.apply(this, args);
                }).bind(this), params.reconnect);
            }
            else if (client) client.emit('error', err)
            else this.emit('error', err)
        }).bind(this));
        
        stream.once('end', (function () {
            if (!params.reconnect) return;
            client.emit('drop');
            
            setTimeout((function () {
                if (!params.reconnect) return;
                client.emit('reconnect');
                dnode.prototype.connect.apply(this, args);
            }).bind(this), params.reconnect);
        }).bind(this));
    }
    else {
        stream.on('error', (function (err) {
            if (client) client.emit('error', err)
            else this.emit('error', err)
        }).bind(this));
    }
    
    function attachDnode() {
      
        client = createClient(self.proto, stream);
        
        client.end = function () {
            if (params.reconnect) params.reconnect = 0;
            stream.end();
        };
        
        self.stack.forEach(function (middleware) {
            middleware.call(client.instance, client.remote, client);
        });
        
        if (params.block) {
            client.on('remote', function () {
                params.block.call(client.instance, client.remote, client);
            });
        }
        
        process.nextTick(function () {
            if (client.listeners('error').length === 0) {
                // default error handler to keep everything from crashing
                client.on('error', function (err) {
                    console.error(err && err.stack || err);
                })
            }
        });
        
        client.start();
    };
    
    return this;
};
コード例 #3
0
ファイル: index.js プロジェクト: BrazilianJoe/dnode
dnode.prototype.listen = function () {
    var params = protocol.parseArgs(arguments);
    var server = params.server;
    
    if (params.port) {
        params.host = params.host || '127.0.0.1';
        if (params.key) {
            var options = {
                key: params.key,
                cert: params.cert,
                ca: params.ca,
                requestCert: params.requestCert,
                rejectUnauthorized: params.rejectUnauthorized
            };
            server = tls.createServer(options);
            server.listen(
                params.port, params.host,
                this.emit.bind(this, 'ready')
            );
        }
        else {
            server = net.createServer();
            server.listen(
                params.port, params.host,
                this.emit.bind(this, 'ready')
            );
        }
    }
    else if (server && server instanceof http.Server
    || 'httpAllowHalfOpen' in server || params.webserver) {
        // a webserver, use socket.io
        if (!SocketIO) SocketIO = require('./lib/stream_socketio');
        server = SocketIO(
            server || params.webserver,
            params.mount || '/dnode.js',
            params.io || {}
        );
    }
    
    if (!server) {
        this.emit('error', new Error('Not sure how to fire up this listener'));
    }
    
    var clients = {};
    var listenFor = server instanceof tls.Server 
                    ? 'secureConnection' 
                    : 'connection' ;
    server.on(listenFor, (function (stream) {
        var client = createClient(this.proto, stream);
        clients[client.id] = client;
        
        this.stack.forEach(function (middleware) {
            middleware.call(client.instance, client.remote, client);
        });
        
        if (params.block) {
            client.on('remote', function () {
                params.block.call(client.instance, client.remote, client);
            });
        }
        
        client.start();
    }).bind(this));
    
    server.on('error', this.emit.bind(this, 'error'));
    
    this.server = server;
    server.on('close', this.emit.bind(this, 'close'));
    
    return this;
};