Skip to content

Commit 0603fb2

Browse files
committed
add ws
1 parent 9981d19 commit 0603fb2

File tree

3 files changed

+147
-9
lines changed

3 files changed

+147
-9
lines changed

example.js

+42-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const leveldown = require('leveldown')
55
const async = require('async')
66
const utils = require('./utils')
77
const debug = require('./debug')
8+
const setupWS = require('./ws-stack')
89
const TLS_ENABLED = true
910

1011
const aliceOpts = {
@@ -41,7 +42,16 @@ async.map([
4142
meet([alice, bob], err => {
4243
if (err) throw err
4344

44-
haveFun(alice, bob)
45+
alice.on('message', function (message) {
46+
console.log('alice received a message from bob', message)
47+
})
48+
49+
bob.on('message', function (message) {
50+
console.log('bob received a message from alice', message)
51+
})
52+
53+
haveTCPFun(alice, bob)
54+
// haveWebsocketsFun(alice, bob)
4555
})
4656
})
4757

@@ -54,7 +64,7 @@ function meet (nodes, cb) {
5464
}, cb)
5565
}
5666

57-
function haveFun (alice, bob) {
67+
function connectTCP (alice, bob) {
5868
const aliceServer = tcp.createServer({
5969
port: 12345,
6070
// TLS using axolotl
@@ -72,10 +82,6 @@ function haveFun (alice, bob) {
7282

7383
aliceServer.on('error', console.error)
7484

75-
alice.on('message', function (message) {
76-
console.log('alice received a message from bob', message)
77-
})
78-
7985
const bobClient = tcp.createClient({
8086
port: 12345,
8187
// TLS using axolotl
@@ -92,11 +98,35 @@ function haveFun (alice, bob) {
9298
bobClient.on('message', data => {
9399
bob.receive(data, alice._recipientOpts, logErr)
94100
})
101+
}
95102

96-
bob.on('message', function (message) {
97-
console.log('bob received a message from alice', message)
98-
})
103+
function connectWebSockets (alice, bob) {
104+
const url = 'ws://localhost:42824'
105+
connect(url, alice)
106+
connect(url, bob)
99107

108+
function connect (url, node) {
109+
const setup = setupWS(node)
110+
const stack = setup.networkingStack({
111+
url: url,
112+
tls: TLS_ENABLED
113+
})
114+
115+
// stack.on('error', logErr)
116+
}
117+
}
118+
119+
function haveWebsocketsFun (alice, bob) {
120+
connectWebSockets(alice, bob)
121+
chat(alice, bob)
122+
}
123+
124+
function haveTCPFun (alice, bob) {
125+
connectTCP(alice, bob)
126+
chat(alice, bob)
127+
}
128+
129+
function chat (alice, bob) {
100130
alice.signAndSend({
101131
to: bob._recipientOpts,
102132
object: {
@@ -123,5 +153,8 @@ function logErr (err) {
123153
if (err) {
124154
console.error(err)
125155
console.error(err.stack)
156+
if (err.tfError) {
157+
console.error(err.tfError.stack)
158+
}
126159
}
127160
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"mkdirp": "^0.5.1",
2626
"nkey-bitcoin": "^1.1.1",
2727
"nkey-ec": "^2.0.1",
28+
"sendy": "^2.2.0",
29+
"sendy-axolotl": "^1.0.1",
30+
"sendy-ws": "^1.4.0",
2831
"through2": "^2.0.1",
2932
"write-file-atomic": "^1.1.4",
3033
"xtend": "^4.0.1"

ws-stack.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const TLSClient = require('sendy-axolotl')
2+
const Sendy = require('sendy')
3+
const SendyWS = require('sendy-ws')
4+
const newSwitchboard = SendyWS.Switchboard
5+
const WebSocketClient = SendyWS.Client
6+
const tradle = require('@tradle/engine')
7+
// const utils = require('./utils')
8+
9+
module.exports = function createConnector (node) {
10+
const connector = {
11+
myIdentifier: function (tls) {
12+
return connector.getIdentifier(node.identity, tls)
13+
},
14+
15+
getIdentifier: function (identity, tls) {
16+
const pk = tls ? tradle.utils.find(identity.pubkeys, k => {
17+
return k.type === 'ec' && k.purpose === 'tls'
18+
}) : tradle.utils.find(identity.pubkeys, k => {
19+
return k.type === 'ec' && k.purpose === 'sign'
20+
})
21+
22+
return tradle.utils.serializePubKey(pk).toString('hex')
23+
},
24+
25+
parseIdentifier: function (identifier) {
26+
const pubKey = tradle.utils.unserializePubKey(new Buffer(identifier, 'hex'))
27+
pubKey.pub = new Buffer(pubKey.pub, 'hex')
28+
return pubKey
29+
},
30+
31+
networkingStack: function (opts) {
32+
const url = opts.url
33+
const tlsEnabled = opts.tls
34+
const webSocketClient = new WebSocketClient({
35+
url: url,
36+
autoConnect: true,
37+
// for now, till we figure out why binary
38+
// doesn't work (socket.io parser errors on decode)
39+
// forceBase64: true
40+
})
41+
42+
webSocketClient.on('disconnect', function () {
43+
switchboard.clients().forEach(function (c) {
44+
// reset OTR session, restart on connect
45+
debug('aborting pending sends due to disconnect')
46+
c.destroy()
47+
})
48+
49+
// pause all channels
50+
node.sender.pause()
51+
})
52+
53+
webSocketClient.on('connect', function (recipient) {
54+
// resume all paused channels
55+
node.sender.resume()
56+
})
57+
58+
const tlsKey = tlsEnabled && tradle.utils.find(node.keys, key => {
59+
return key.get('purpose') === 'tls'
60+
})
61+
62+
const switchboard = newSwitchboard({
63+
identifier: connector.myIdentifier(tlsEnabled),
64+
unreliable: webSocketClient,
65+
clientForRecipient: function (recipient) {
66+
const sendy = new Sendy(opts)
67+
if (!tlsKey) return sendy
68+
69+
return new TLSClient({
70+
key: {
71+
secretKey: tlsKey.priv,
72+
publicKey: tlsKey.pub
73+
},
74+
client: sendy,
75+
theirPubKey: new Buffer(connector.parseIdentifier(recipient).pub, 'hex')
76+
})
77+
}
78+
})
79+
80+
switchboard.on('timeout', function (identifier) {
81+
switchboard.cancelPending(identifier)
82+
})
83+
84+
node._send = function (msg, recipientInfo, cb) {
85+
const identifier = connector.getIdentifier(recipientInfo.object, tlsEnabled)
86+
switchboard.send(identifier, msg, cb)
87+
}
88+
89+
switchboard.on('message', function (msg, sender) {
90+
const pubKey = connector.parseIdentifier(sender)
91+
node.receive(msg, { pubKey })
92+
})
93+
94+
return {
95+
switchboard,
96+
webSocketClient
97+
}
98+
}
99+
}
100+
101+
return connector
102+
}

0 commit comments

Comments
 (0)