mafintosh / webrtc-swarm

Create a swarm of p2p connections using webrtc and a signalhub
MIT License
380 stars 60 forks source link

2 peers as initiator ? #37

Open G-Ray opened 5 years ago

G-Ray commented 5 years ago

Disclamer: We use simple-peer@9.2.1. The following issue is not visible with simple-peer <= 9.0.x, only from simple-peer >= 9.1.x

Sometimes, we get 2 peers trying to connect as initiator at same time. Here are the logs:

First peer, in one tab:

my uuid: cjrrm879l0000305zxsk3el9n index.js:118 /all {type: "connect", from: "cjrrm879l0000305zxsk3el9n"} index.js:121 skipping self cjrrm879l0000305zxsk3el9n index.js:118 /all {type: "connect", from: "cjrrm8b3a000030608j7zc0u4"} index.js:136 connecting to new peer (as initiator) cjrrm8b3a000030608j7zc0u4 index.js:1062 [3f9c98c] new peer {initiator: true, channelConfig: undefined, config: {…}, stream: undefined, offerConstraints: {…}} index.js:1062 [3f9c98c] _needsNegotiation index.js:1062 [3f9c98c] starting batched negotiation index.js:1062 [3f9c98c] start negotiation index.js:118 /all {type: "connect", from: "cjrrm879l0000305zxsk3el9n"} index.js:121 skipping self cjrrm879l0000305zxsk3el9n index.js:1062 [3f9c98c] signalingStateChange have-local-offer index.js:1062 [3f9c98c] createOffer success index.js:1062 [3f9c98c] signal index.js:1062 [3f9c98c] iceStateChange (connection: new) (gathering: gathering) 3index.js:620 started iceComplete timeout index.js:1062 [3f9c98c] iceStateChange (connection: new) (gathering: complete) index.js:118 /all {type: "connect", from: "cjrrm879l0000305zxsk3el9n"} index.js:121 skipping self cjrrm879l0000305zxsk3el9n index.js:171 signalling cjrrm8b3a000030608j7zc0u4 {type: "offer", sdp: "v=0 ↵o=- 2895647361888715537 2 IN IP4 127.0.0.1 ↵s…a=mid:0 ↵a=sctpmap:5000 webrtc-datachannel 1024 ↵"} index.js:1062 [3f9c98c] signal() index.js:1062 [3f9c98c] destroy (error: InvalidStateError: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer) index.js:83 disconnected from peer cjrrm8b3a000030608j7zc0u4 Error: InvalidStateError: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer at makeError (index.js:1153) at index.js:229

Second peer, in other tab:

my uuid: cjrrm8b3a000030608j7zc0u4 index.js:118 /all Object index.js:121 skipping self cjrrm8b3a000030608j7zc0u4 index.js:118 /all Object index.js:136 connecting to new peer (as initiator) cjrrm879l0000305zxsk3el9n index.js:1062 [98300c4] new peer Object index.js:1062 [98300c4] _needsNegotiation index.js:1062 [98300c4] starting batched negotiation index.js:1062 [98300c4] start negotiation index.js:171 signalling cjrrm879l0000305zxsk3el9n Object index.js:1062 [98300c4] signal() index.js:1062 [98300c4] destroy (error: InvalidStateError: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer) index.js:83 disconnected from peer cjrrm879l0000305zxsk3el9n Error: InvalidStateError: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer at makeError (index.js:1153) at index.js:229 index.js:157 skipping non-offer Object index.js:157 skipping non-offer Object index.js:157 skipping non-offer Object index.js:118 /all {type: "connect", from: "cjrrm879l0000305zxsk3el9n"} index.js:136 connecting to new peer (as initiator) cjrrm879l0000305zxsk3el9n index.js:1062 [8d5419d] new peer {initiator: true, channelConfig: undefined, config: undefined, stream: undefined, offerConstraints: {…}} index.js:1062 [8d5419d] _needsNegotiation index.js:1062 [8d5419d] starting batched negotiation index.js:1062 [8d5419d] start negotiation index.js:1062 [8d5419d] signalingStateChange have-local-offer index.js:1062 [8d5419d] createOffer success index.js:1062 [8d5419d] signal index.js:1062 [8d5419d] iceStateChange (connection: new) (gathering: gathering) 3index.js:620 started iceComplete timeout index.js:1062 [8d5419d] iceStateChange (connection: new) (gathering: complete)

We are unable to reproduce this issue in tests, but would be great if you have an idea why 2 peers could try to join as iniator at same time ?

corwin-of-amber commented 4 years ago

I managed to get around this by using https://github.com/QuantResearch/webrtc-swarm (thanks @QuantResearch). It uses a Math.random value to break the symmetry and I found that it works well. I did not like the floating point numbers in the messages so I made a variation that uses cuid instead (https://github.com/corwin-of-amber/webrtc-swarm).