Skip to content

Commit 342a6cc

Browse files
committed
working loopback comms
1 parent be4e5bd commit 342a6cc

5 files changed

Lines changed: 16 additions & 9 deletions

File tree

examples/test-loopback-party.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function main(){
3333
})
3434

3535

36-
let comms2 = new Dataparty.Comms.LoopbackComms({ channel: loopback.peer2 })
36+
let comms2 = new Dataparty.Comms.LoopbackComms({ channel: loopback.peer2, session: 'foobar' })
3737

3838
let peer2 = new Dataparty.PeerParty({
3939
comms: comms2,

src/comms/loopback-channel.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
const debug = require('debug')('dataparty.comms.loopback-channel')
12
const EventEmitter = require("eventemitter3")
23

34
class PeerNode {
4-
constructor(peer){
5+
constructor(peer, name){
6+
this.name = name
57
this.events = new EventEmitter
68
this.peer = peer
79
}
810

911
post(ns, msg){
12+
debug('post('+this.name+')', ns, msg)
1013
this.events.emit(ns, msg)
1114
}
1215

1316
on(ns, func){
17+
debug('on('+this.name+')', ns)
1418
this.peer.events.on(ns, func)
1519
}
1620
}
@@ -19,8 +23,8 @@ module.exports = class LoopbackChannel {
1923
constructor(){
2024
//
2125

22-
this.peer1 = new PeerNode()
23-
this.peer2 = new PeerNode(this.peer1)
26+
this.peer1 = new PeerNode(undefined, '1')
27+
this.peer2 = new PeerNode(this.peer1, '2')
2428

2529
this.peer1.peer = this.peer2
2630
}

src/comms/loopback-comms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const LoopbackSocket = require('./loopback-socket')
99
const AUTH_TIMEOUT_MS = 3000
1010

1111
class LoopbackComms extends PeerComms {
12-
constructor({remoteIdentity, host, party, channel}){
13-
super({remoteIdentity, host, party})
12+
constructor({remoteIdentity, host, party, channel, ...options}){
13+
super({remoteIdentity, host, party, ...options})
1414

1515
this.channel = channel
1616
}

src/comms/loopback-socket.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = class LoopbackSocket extends EventEmitter {
1919

2020
start(){
2121
debug('start')
22-
this.channel.post('connected', true)
22+
//this.channel.post('connected', true)
2323
this.ready_local = true
2424
this.checkConnected()
2525
}
@@ -30,15 +30,18 @@ module.exports = class LoopbackSocket extends EventEmitter {
3030
this.closed = false
3131
this.emit('connect', true)
3232
debug('checkConnected - connected')
33+
this.channel.post('connected', true)
3334
}
3435
else if(!this.ready){
36+
debug('checkConnected - not ready')
3537
this.channel.post('connected', true)
3638
}
3739
}
3840

3941
onconnected(){
4042
debug('onconnected')
4143
if(!this.ready_remote){
44+
debug('make ready')
4245
this.ready_remote = true
4346
this.checkConnected()
4447
}

src/comms/peer-comms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ class PeerComms extends SocketComms {
214214
if(this.host){
215215
debug('host mode comms')
216216

217-
this.socket.on('connect', this.handleClientConnection.bind(this))
217+
this.socket.once('connect', this.handleClientConnection.bind(this))
218218
this.socket.on('data', this.handleClientCall.bind(this))
219219
}
220220
else{
221221
debug('client mode comms')
222-
this.socket.on('connect', this.onopen.bind(this))
222+
this.socket.once('connect', this.onopen.bind(this))
223223
this.socket.on('data', this.handleMessage.bind(this))
224224
}
225225

0 commit comments

Comments
 (0)