-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
63 lines (47 loc) · 1.3 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict'
const EventEmitter = require('events')
const net = require('net')
const jscribe = require('.')
const main = async () => {
const server = net.createServer()
server.listen(12345)
const serverPromise = EventEmitter
.once(server, 'connection')
.then(([conn]) => conn)
const clientConn = net.connect(12345)
const clientPromise = EventEmitter.once(clientConn, 'connect')
const [serverConn] = await Promise.all([serverPromise, clientPromise])
const promise = jscribe.receive(clientConn, {
maxBufferSize: 4096,
timeout: 3e3
})
const opts = {
maxBufferSize: 4096,
schema: {
type: 'object',
properties: {
foo: { type: 'integer' },
bar: { type: 'string' }
},
required: ['foo'],
additionalProperties: false
}
}
jscribe(serverConn, opts, (err, msg) => {
if (err) {
console.error('[SERVER]', err)
return
}
jscribe.send(serverConn, { baz: true })
console.log('[SERVER]', 'Message from client:', JSON.stringify(msg))
})
jscribe.send(clientConn, { bar: 'baz' })
jscribe.send(clientConn, { foo: 1, bar: 'baz' })
const msg = await promise
console.log('[CLIENT]', 'Message from server:', JSON.stringify(msg))
process.exit()
}
main().catch(err => {
console.error(err)
process.exit(1)
})