socketvat
install
- install node
npm install socketvat
npm test socketvat
DEBUG=socketvat,test npm test socketvat
api
opts
)
sv = socketvat(instanceof sv === require('eventvat')
opts.namespace
- default:socketvat
- will be used to prefix every event which will be sent to remote socket, also it will only listen for remote events prefixed by that namespace
<socket>
[,<cb>
])
sv.initSocket(<socket>
has to be an instance of require('nssocket').NsSocket
var nss = require('nssocket')
var sv = require('socketvat')
var serverVat = sv()
var clientVat = sv()
var s = nss.createServer(function(s){serverVat.initSocket(s)})
s.listen(4545)
var c = new nss.NsSocket()
clientVat.initSocket(c)
c.connect(4545)
<arg1>
[,<arg2>
[,<arg3>
]],[<listener>
])
sv.listen(this will start a net
or tls
-Server
-
<arg1>
,<arg2>
,<arg3>
Int
→port
String
→host
(orpath
if no port is defined, start a unix-socket-server)Object
port
→port
host
→host
key
→ starttls
-Server with that keycert
→ starttls
-Server with that certca
→ starttls
-Server with that ca
-
<listener>
Function
→ connection-listener, will be called with 2 arguments:- a object with helper-functions for communication with remote socketvat
- an instance of
require('nssocket').NsSocket
<arg1>
[,<arg2>
[,<arg3>
]],[<listener>
])
sv.connect(this will create a net
or tls
-Connection
-
<arg1>
,<arg2>
,<arg3>
Int
→port
String
→host
(orpath
if no port is defined, start a unix-socket-connection)Object
port
→port
host
→host
key
→ starttls
-Connection with that keycert
→ starttls
-Connection with that certreconnect
→ upon connection -drop or -error, connect again inopts.reconnect
milliseconds
-
<listener>
Function
→ connection-listener, will be called with 2 arguments:- a object with helper-functions for communication with remote socketvat
- an instance of
require('nssocket').NsSocket
examples
simple
var sv = require('../socketvat')
var assert = require('assert')
// server
var s = sv()
s.set('foo','server')
s.listen(3000,function(remote,socket){
remote.once('get',function(k,v){
assert.equal(k,'foo')
assert.equal(v,'client')
})
remote.get('foo')
})
// client
var c = sv()
c.set('foo','client')
c.connect(3000,function(remote,socket){
remote.once('get',function(k,v){
assert.equal(k,'foo')
assert.equal(v,'server')
})
remote.get('foo')
})
verbose
var sv =var serverVat =var server = serverVatvar clientVat =var client = clientVatserverVatserverVatclientVatclientVatserverVatclientVat{return {name = name || '•'name = name+Array13var event = Array ? thisevent : thiseventevent = event+Array32console}}
output:
serverVat : set foo → [ 'bar' ]
serverVat : set → [ 'foo', 'bar' ]
---- clientVat once "set *": set foo → [ 'bar' ]
clientVat : set foo → [ 'bar' ]
clientVat : set → [ 'foo', 'bar' ]
clientSocket : start → []
---- sent the message
serverSocket : start → []
serverSocket : data socketvat method onAny → [ { args: [] } ]
serverSocket : data socketvat method set → [ { args: [ 'client', 'y' ] } ]
---- serverVat once "set client": set client → [ 'y' ]
serverVat : set client → [ 'y' ]
serverVat : set → [ 'client', 'y' ]
serverSocket : data socketvat method get → [ { args: [ 'client' ] } ]
serverVat : get client → [ 'y' ]
serverVat : get → [ 'client', 'y' ]
serverSocket : data socketvat method keys → [ { args: [ 'client' ] } ]
serverVat : keys → [ /client/, [ 'client' ] ]
clientSocket : data socketvat method onAny → [ { args: [] } ]
clientSocket : data socketvat method set → [ { args: [ 'server', 'x' ] } ]
clientVat : set server → [ 'x' ]
clientVat : set → [ 'server', 'x' ]
clientSocket : data socketvat event set client → [ { args: [ 'y' ] } ]
clientRemote : set client → [ 'y' ]
clientSocket : data socketvat event set → [ { args: [ 'client', 'y' ] } ]
clientRemote : set → [ 'client', 'y' ]
clientSocket : data socketvat event get client → [ { args: [ 'y' ] } ]
clientRemote : get client → [ 'y' ]
clientSocket : data socketvat event get → [ { args: [ 'client', 'y' ] } ]
clientRemote : get → [ 'client', 'y' ]
clientSocket : data socketvat event keys → [ { args: [ 'client', [Object] ] } ]
clientRemote : keys → [ 'client', [ 'client' ] ]
serverSocket : data socketvat event set server → [ { args: [ 'x' ] } ]
serverRemote : set server → [ 'x' ]
serverSocket : data socketvat event set → [ { args: [ 'server', 'x' ] } ]
serverRemote : set → [ 'server', 'x' ]
clientSocket : close → [ undefined ]
serverSocket : close → []
---- client disconnected