A network library for pull-streams to handle multiple protocols with the same interface.
var _ = require('icebreaker')
var network = require('icebreaker-network')
var listen = network.listen('tcp://127.0.0.1:8090')
_(listen, network.on({
ready: function (e) {
console.log('socket is ready on port: '+e.address)
//connect to localhost
network.connect(e.address, function (err,connection) {
// handle the client side pull-stream
if(err) throw err
_('hello', connection, _.drain(function (d) {
console.log(d.toString())
}, function (err) {
// close the socket
listen.end()
}))
})
},
connection: function (s) {
// handle the server side pull-stream
_(s, _.map(function (d) {
return d.toString() + ' world'
}), s)
},
end: function () {
console.log('socket closed')
}
}))
{protocol}://{ip}:{port}/
{protocol+unix}://{path}
shs+{protocol}://{Base64 and URL encoded public key}@{ip}:{port}/{app_key}
shs+{protocol}+unix://{Base64 and URL encoded public key}@{path}/{app_key}
- ready
- message
- connection
- end
_(
network.listen('tcp://127.0.0.1:8090'),
network.on({
ready:function(e){
console.log('tcp socket is ready on address: '+e.address)
},
end:function(){
}
})
)
Helper to handle events on a listener.
- Returns a sink if the end event is set, otherwise a through.
_(
listener,
network.on({
ready:function(e){
console.log(e.address)
},
connection:function(e){
// handle the connection
}
end:function(err){
if(err) console.log(err)
}
}))
Helper to map events on a listener.
- Returns a through.
_(
network.listen('tcp://127.0.0.1:8090'),
network.map({
connection:function(e){
e.isSuper = true
return e
}
}),
network.on({
connection:function(e){
console.log(e)
},
end:function(err){
console.log(err)
}
})
)
The async version of map.
- Returns a through.
_(
network.listen('tcp://127.0.0.1:8090'),
network.asyncMap({
connection:function(e,done){
e.isSuper = true
done(e)
}
}),
network.on({
connection:function(e){
console.log(e)
},
end:function(err){
console.log(err)
}
})
)
This connects to a specified url address.
- Url to connect.
- Parameters for the selected protocol is optional, when the selected protocols is not a shs protocol.
- callback(error,connection) returns error or a connection duplex stream.
- For shs protocols is params.keys.publicKey and params.keys.secretKey required.
var cl = require('chloride')
var keys = cl.crypto_sign_keypair()
This starts a server in the listening mode on the given url and parameters.
- Returns a source with events
- URL is required the format is protocol://host:port
- Parameters for the selected protocol is optional, when the selected protocols is not a shs protocol.
This closes the socket
This sends a network message on message-oriented protocols to a destination.
This combines multiple network.listen to one source.
var os = require('os')
var listener = network.combine(
listen('tcp://localhost:8089'),
listen('udp://0.0.0.0:8089',{reuseAddr:true,loopback:true,multicast:'239.5.5.5'}),
listen('tcp+unix://'+os.tmpdir()+'/test4.socket')
)
_(listener,network.on({
// all listener ready
ready:function(e){
// sends a string message over udp
listener.push('ready')
// connects to localhost
network.connect('tcp://localhost:8089',function(err,connection){
})
},
// incomming tcp connection
connection:function(connection){
_(['hello'],connection,_.drain(function(item){
console.log(item)
},function(err){
if(err) return console.log(err)
}))
},
// incomming udp message
message:function(e){
console.log(e)
},
end:function(err){
}
}))
- Returns a list of the supported protocols.
Register a custom protocol.
- Name of the protocol for example 'custom:'.
- For the custom connect and listen see in lib/tcp for example.
MIT