我想要一个跨网络工作的事件发射器 [英] I want an eventemitter that works across a network
问题描述
我真的很喜欢 EventEmitter 范式,我想用它来通过网络在两个程序之间进行通信.
I really like the EventEmitter paradigm, and I'd like to use it to communicate between two programs across a network.
我想出了自己的SockEmitter
,但我想知道:我做错了吗(tm)"?是否有一些软件包已经可以做到这一点?是否有更适合跨网络通信的不同范式?
I came up with my own SockEmitter
, but I want to know: am I "doing it wrong(tm)"? Is there some package that will already do this? Is there a different paradigm that works better for communicating across a network?
这是我所拥有的:
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)
})
}
}
推荐答案
Socket.io 经常被提出来做这样的事情.它以事件的方式提供 2 路通信.但是,人们经常查看服务器发送的事件这是使用标准 http 消息的单向事件的 W3C 标准(而不是像 socket.io 那样对自定义协议执行 UPGRADE
).
Socket.io is often brought up for something like this. It provides 2 way communication in an evented way. However, people often look over Server Sent Events which is a W3C standard for 1-way eventing that uses standard http messages (rather than doing an UPGRADE
to a custom protocol like socket.io does).
使用SSE,您可能会看到许多专注于服务器到浏览器通信的文章,但该规范定义了一个简单的基于 http 的消息结构,可用于您想要的任何内容(例如:服务器到服务器).
With SSE, you will probably see many articles focused on server-to-browser communication, but the the spec defines a simple http based message structure which can be used for whatever you want (eg: server-to-server).
我一直在使用它并取得了巨大的成功.我可以构建事件服务器到服务器的东西,然后也可以轻松地与浏览器连接,这真是太棒了.正如预期的那样,IE 尚未支持,但大多数浏览器都支持.
I've been using it with great success. It is fantastic that I can build evented server-to-server things- and then easily connect with a browser too. As expected, IE isn't onboard yet but most browsers are.
有很多 SSE npm 模块.我已经将 eventsource-node
用于聆听"结束并取得了巨大成功.当我开始使用 SSE 时,没有用于发送事件的模块——只有用于监听的模块;所以我写了我自己的.不过这没什么大不了的——协议非常简单,只需要大约 100 行代码.我相信其中一个已发布的模块会很好用.
There are many SSE npm modules. I have used eventsource-node
for the "listening" end with great success. When I got started with SSE, there were no modules for sending events- just modules for listening; so I wrote my own. It was no a big deal though- the protocol is so dead simple that it only took about 100 lines of code. I'm sure one of the published modules will work great.
这篇关于我想要一个跨网络工作的事件发射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!