Skip to content

Commit b2d7f69

Browse files
committed
FIX: respond appropriately to trickleSDP
1 parent ecd8594 commit b2d7f69

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

server/services/attachWS/index.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,36 @@ function setupWebSocketServer (server) {
120120
debug('got %s offers from %s', params.offers.length, params.peer_id)
121121
debug('got %s peers from swarm %s', peers.length, params.info_hash)
122122

123-
peers.forEach((peer, i) => {
124-
const { sdp } = (params.offers && params.offers[i] && params.offers[i].offer) || {}
125-
const isTrickleSdp = SDP_TRICKLE_REGEX.test(sdp)
126-
if (isTrickleSdp) {
127-
swarm.offers.set(params.offers[i].offer_id, peer.peerId)
128-
}
129-
130-
peer.socket.send(
131-
JSON.stringify({
132-
action: 'announce',
133-
offer: params.offers[i].offer,
134-
offer_id: params.offers[i].offer_id,
135-
peer_id: common.hexToBinary(params.peer_id),
136-
info_hash: common.hexToBinary(params.info_hash)
137-
}),
138-
peer.socket.onSend
123+
server.getSwarm(params.info_hash)
124+
.then(
125+
(swarm) => {
126+
if (this.destroyed) return
127+
if (!swarm) {
128+
return this.emit('warning', new Error('no swarm with that `info_hash`'))
129+
}
130+
131+
peers.forEach((peer, i) => {
132+
const { sdp } = (params.offers && params.offers[i] && params.offers[i].offer) || {}
133+
const isTrickleSdp = SDP_TRICKLE_REGEX.test(sdp)
134+
if (isTrickleSdp) {
135+
swarm.offers.set(params.offers[i].offer_id, peer.peerId)
136+
}
137+
138+
peer.socket.send(
139+
JSON.stringify({
140+
action: 'announce',
141+
offer: params.offers[i].offer,
142+
offer_id: params.offers[i].offer_id,
143+
peer_id: common.hexToBinary(params.peer_id),
144+
info_hash: common.hexToBinary(params.info_hash)
145+
}),
146+
peer.socket.onSend
147+
)
148+
debug('sent offer to %s from %s', peer.peerId, params.peer_id)
149+
})
150+
}
139151
)
140-
debug('sent offer to %s from %s', peer.peerId, params.peer_id)
141-
})
152+
.catch(err => server.emit('warning', err))
142153
}
143154

144155
const done = () => {
@@ -155,10 +166,6 @@ function setupWebSocketServer (server) {
155166
params.peer_id
156167
)
157168

158-
server.getSwarm(params.info_hash)
159-
.then(gotSwarm)
160-
.catch(err => server.emit('warning', err))
161-
162169
const gotSwarm = swarm => {
163170
if (server.destroyed) return
164171
if (!swarm) {
@@ -167,6 +174,7 @@ function setupWebSocketServer (server) {
167174
new Error('no swarm with that `info_hash`')
168175
)
169176
}
177+
170178
// Mark the destination peer as recently used in cache
171179
const toPeer = swarm.peers.get(params.to_peer_id)
172180
if (!toPeer) {
@@ -190,6 +198,10 @@ function setupWebSocketServer (server) {
190198

191199
done()
192200
}
201+
202+
server.getSwarm(params.info_hash)
203+
.then(gotSwarm)
204+
.catch(err => server.emit('warning', err))
193205
} else {
194206
done()
195207
}

0 commit comments

Comments
 (0)