UNPKG

newbeely-nodejs

Version:

简单易用的轻量级nodejs服务框架. 框架简单到只有组件逻辑,不同的组件提供不同的服务,使用外部的配置文件(只有一个配置文件)方便的组合成一个完整的服务框架. 整个服务使用bearcat(网易开源的nodejs面向切面编程的轻量级框架(AOP))管理,极大的解耦组件间的耦合.(关于代码热更新后续开放).

58 lines (47 loc) 1.39 kB
var Net = require('net'); var Tls = require('tls'); var Logger = require('pomelo-logger').getLogger('newbeely', "tcpComponent"); var EventEmitter = require('events').EventEmitter; var Util = require('util'); var Bearcat = require('bearcat'); var REQ_ID = 0; function TcpClient(opts) { EventEmitter.call(this); this.opts = opts || {ssl: false, host: "localhost", port: 1990}; this.socket = null; this.responses = {}; }; Util.inherits(TcpClient, EventEmitter); /** * */ TcpClient.prototype.connect = function () { var _this = this; var socket = Net.connect(this.opts, function () { Logger.debug('connect to server by ' + _this.opts); _this.emit('connected'); }); this.socket = Bearcat.getBean('tcp-connection', socket, this.opts.protocol || ""); this.socket.on('close', function () { Logger.debug("socket closed!"); }); this.socket.on('message', function (data) { if (_this.responses[data.reqid]) { _this.responses[data.reqid](data); } }); }; TcpClient.prototype.send = function (route, msg, cb) { if (this.socket) { this.socket.send(0, REQ_ID++, route, msg, "utf8"); this.responses[REQ_ID - 1] = cb; } }; module.exports = { id: "tcp-client", func: TcpClient, scope: "prototype", args: [ {name: "opts", type: "Object"} ] }