0.0.1 • Published 10 years ago
spocket v0.0.1
Spocket
Socket with easy message encoding and decoding.
Spocket, a drop-in replacement for net's Socket class, provides a simplistic and modular way to encode and decode messages through plain JavaScript functions (or npm-hosted algorithms).
const example = new Spocket({ ...opts });
// ...
example.on('message', function(data) {
console.log(data.foo.bar);
});
example.send({
foo: 123,
bar: {
baz: 'Qux!'
}
});You can also create a Spocket from a Socket:
var example = Spocket.from(socket, { ...opts });Installation
$ npm install --save spocketUsage
import Spocket from 'spocket';Spocket([opts]) (extends net.Socket)
The Spocket class is drop-in compatible with net.Socket, because it actually is a net.Socket class with a few additions. Events, methods, and properties from net.Socket all work.
opts(Object) Any ofnet.Socket's options orSpocket's extras.opts.encode(Function) A function used to encode messages.opts.decode(Function) A function used to decode messages.opts.algo(Object) An alternative algorithm object for encoding and decoding.
Example:
new Spocket({
// An option passed to net.Socket
writable: false,
// Encoding and decoding
encode: function(object) { ... },
decode: function(buffer) { ... },
// Algorithm object, alternative to the above.
algo: websocket,
});Event: 'message'
This event is emitted after 'data' with the decoded message.
data(Anything) Decoded message.
Example:
foo.on('message', function(data) {
console.log(data.foo.bar);
});Spocket#send(message, [encoding, callback])
The same as net.Socket#write except that message gets encoded.
Example:
foo.send({
foo: 123,
bar: {
baz: 'Qux!',
},
})Credits
| Jamen Marzonie |
License
MIT © Jamen Marzonie
0.0.1
10 years ago