我真的很喜欢 EventEmitter 范例,我想用它在网络上的两个程序之间进行通信。
我想出了自己的SockEmitter
,但我想知道:我是不是“做错了(tm)”?是否有一些软件包已经可以做到这一点?是否有不同的范式更适合通过网络进行通信?
这是我所拥有的:
var JsonSocket = require('json-socket')
// An event emitter that uses a JsonSocket.
// emit passes things over the wire, and data received
// over the wire calls the listeners.
//
// As a result, se.on('foo', console.log); se.emit('foo', 5)
// won't do what you might normally expect from an emitter.
function SockEmitter(socket) {
this._listeners = {}
this.sock = new JsonSocket(socket)
this.sock.on('message', this._message.bind(this))
}
SockEmitter.prototype = {
on: function (type, handler) {
if (!this._listeners[type]) {
this._listeners[type] = [];
}
this._listeners[type].push(handler)
},
off: function (type, handler) {
if (!this._listeners[type]) {
return false
}
var idx = this._listeners[type].indexOf(handler)
if (idx === -1) return false
this._listeners[type].splice(idx, 1)
},
emit: function (type) {
var args = [].slice.call(arguments, 1)
this.sock.sendMessage({type: type, args: args})
},
_message: function (message) {
if (!message || !message.type || !Array.isArray(message.args)) {
return console.error('Invalid message received: %s', message)
}
if (!this._listeners[message.type]) return
this._listeners[message.type].forEach(function (handler) {
handler.apply(null, message.args)
})
}
}