-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (50 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var Events = require('events');
var net = require('net');
var _ = require('underscore');
var parse = require('./lib/parse').parse;
var opt = {};
var defaultConfig = {
port: 8080,
host: '0.0.0.0'
};
var plugins = [];
/**
*
* @param {object} [options] Options for server
* @param {number} [options.port=8080] Listening port for the server
* @param {string} [options.host=0.0.0.0] Listening host for the server
* @constructor
*/
var Osp = function (options) {
opt = _.extend(defaultConfig, options);
Events.EventEmitter.call(this);
var self = this;
net.createServer(function (socket) {
self.emit('tcpConnect', socket);
socket.on('data', function(data) {
var parsedData = parse(data);
if (plugins[parsedData.type]) {
parsedData = plugins[parsedData.type](parsedData, socket);
}
self.emit('message', parsedData, socket);
});
socket.on('close', function() {
self.emit('tcpDisConnect');
});
}).listen(opt.port, opt.host);
};
Osp.prototype = Object.create(Events.EventEmitter.prototype);
/**
* Set a handler function to specified data type and returns it
* If handler is not specified returns with registered one
* @param {number} dataType The data type that the handler can parse
* @param {function} [handler] The handler function to data type
* @returns {*} The processed object
*/
Osp.prototype.plugin = function (dataType, handler) {
if (handler) {
plugins[dataType] = handler;
}
return plugins[dataType];
};
module.exports = Osp;