mappum / electron-webrtc

⚛ Use WebRTC in Node.js via a hidden Electron process
317 stars 47 forks source link

Data Channel Between Node Client and Browser Client? #64

Closed RaggarDK closed 7 years ago

RaggarDK commented 8 years ago

How would one go about creating a connection and exchanging data between a NodeJS client using this module and a browser client using vanilla Simple-Peer? I'm mainly interested in a data channel, but have a hard time wrapping my head around this.

I tested the simple-peer.js example/test with success, and I tested the main example of the Simple-Peer readme with success. Now, the offer and answer are both different in these 2 examples, and using the offer from the electron-webrtc test fails when used in the Simple-Peer example to create on answer.

I don't believe I've come across an implementation to use as reference.

RaggarDK commented 8 years ago

Okay, here are a couple of examples of offers and answers from both the node.js implementation of electron-webrtc and the simple-peer implementation using iceweasel.

Node: { type: 'offer', sdp: 'v=0\r\no=- 450038689866581906 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=msid-semantic: WMS\r\nm=application 9 DTLS/SCTP 5000\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:cL07LeL2HVCoFqUk\r\na=ice-pwd:xyyCX6sze+RGONb8JQJEw4EK\r\na=fingerprint:sha-256 5D:C8:13:E0:91:0A:18:F2:27:04:CF:FA:84:F8:D3:38:D6:1C:42:AC:DA:D3:3E:A3:CF:F6:F3:EA:93:82:14:66\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n' } { candidate: { candidate: 'candidate:1505098533 1 udp 2122260223 192.168.216.131 49490 typ host generation 0 ufrag cL07LeL2HVCoFqUk network-id 1', sdpMLineIndex: 0, sdpMid: 'data' } } { candidate: { candidate: 'candidate:813675748 1 udp 1686052607 80.161.159.177 55383 typ srflx raddr 192.168.216.131 rport 49490 generation 0 ufrag cL07LeL2HVCoFqUk network-id 1', sdpMLineIndex: 0, sdpMid: 'data' } }

{ type: 'answer', sdp: 'v=0\r\no=- 6153013603825704171 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=msid-semantic: WMS\r\nm=application 9 DTLS/SCTP 5000\r\nc=IN IP4 0.0.0.0\r\nb=AS:30\r\na=ice-ufrag:d9JsS8v0af+S2emM\r\na=ice-pwd:I0L7799q72LaYvIYo+xmF5/u\r\na=fingerprint:sha-256 52:BE:9E:B1:F7:ED:A3:CF:4D:39:16:AC:33:09:58:0B:78:E3:59:14:75:66:30:42:91:76:8A:E8:8D:60:63:45\r\na=setup:active\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n' } { candidate: { candidate: 'candidate:389416917 1 tcp 1518280447 192.168.216.131 9 typ host tcptype active generation 0 ufrag cL07LeL2HVCoFqUk network-id 1', sdpMLineIndex: 0, sdpMid: 'data' } } { candidate: { candidate: 'candidate:1505098533 1 udp 2122260223 192.168.216.131 55843 typ host generation 0 ufrag d9JsS8v0af+S2emM network-id 1', sdpMLineIndex: 0, sdpMid: 'data' } }

IceWeasel: {"type":"offer","sdp":"v=0\r\no=mozilla...THIS_IS_SDPARTA-43.0.4 1629133132086014286 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 A8:70:4C:EB:7A:6D:9D:E7:7D:14:7C:00:38:8E:CB:96:25:22:C6:8F:26:2C:D7:B7:57:F0:5D:2A:51:99:8F:51\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=application 55382 DTLS/SCTP 5000\r\nc=IN IP4 80.161.159.177\r\na=candidate:0 1 UDP 2122252543 192.168.216.131 46239 typ host\r\na=candidate:1 1 UDP 1686052863 80.161.159.177 55382 typ srflx raddr 192.168.216.131 rport 46239\r\na=sendrecv\r\na=end-of-candidates\r\na=ice-pwd:916adfb15d457f10b714031f69ef44f4\r\na=ice-ufrag:85cbee68\r\na=mid:sdparta_0\r\na=sctpmap:5000 webrtc-datachannel 256\r\na=setup:actpass\r\na=ssrc:1280419403 cname:{0f2c6cb0-8f88-48cc-945a-415f427a86d5}\r\n"}

{"type":"answer","sdp":"v=0\r\no=mozilla...THIS_IS_SDPARTA-43.0.4 1069964022327374592 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 21:ED:FA:F3:B2:69:97:AF:3A:DA:67:AF:3F:97:47:FF:6F:C3:D3:87:C4:9E:98:FF:71:B0:66:5E:8D:D4:A2:AB\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=application 61667 DTLS/SCTP 5000\r\nc=IN IP4 80.161.159.177\r\na=candidate:0 1 UDP 2122252543 192.168.216.131 32902 typ host\r\na=candidate:1 1 UDP 1686052863 80.161.159.177 61667 typ srflx raddr 192.168.216.131 rport 32902\r\na=sendrecv\r\na=end-of-candidates\r\na=ice-pwd:4b23487fb9e7dd9049fe451c25245db4\r\na=ice-ufrag:54656cc0\r\na=mid:sdparta_0\r\na=sctpmap:5000 webrtc-datachannel 256\r\na=setup:active\r\na=ssrc:2243935156 cname:{a29c3f8e-3052-43d6-b135-79f4cffd1893}\r\n"}

Even if I JSON encode the offer from within node, as such: {"type":"offer","sdp":"v=0\r\no=- 463783147652736254 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=msid-semantic: WMS\r\nm=application 9 DTLS/SCTP 5000\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:yxbqmGZt0e47Mywe\r\na=ice-pwd:aXkJleZMIRwt0Al9RwBKR6Ko\r\na=fingerprint:sha-256 B3:00:A1:5D:00:93:76:99:36:7B:16:7A:57:9C:01:A6:6A:BF:92:8C:80:A8:0E:D2:14:35:C7:9A:7C:28:4B:0D\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n"} {"candidate":{"candidate":"candidate:1505098533 1 udp 2122260223 192.168.216.131 45583 typ host generation 0 ufrag yxbqmGZt0e47Mywe network-id 1","sdpMLineIndex":0,"sdpMid":"data"}}

I get the following error in the console of Icewesel: error Error: signal() called with invalid signal data Stack trace: [2]</</Peer.prototype.signal@http://localhost/bundle.js:240:19 [1]</<@http://localhost/bundle.js:14:3

I do see the output of both are very different, but I have no idea as to how I make either fit, so the browser can connect to the node peer (Initiater).

mappum commented 8 years ago

@RaggarDK What version of electron-webrtc are you using? I recently made a change that should fix the signaling data not containing any real addresses (like in your example), which was published in version 0.2.10.

RaggarDK commented 8 years ago

I'm using a fresh install. And I can confirm it is version 0.2.10. I think the issue is the way the offer is created using electron-webrtc, as the simple-peer library might be unable to understand this? In the example offers I posted above, you see some differences between the two.

mappum commented 8 years ago

There shouldn't be any difference since simple-peer doesn't generate any of the signaling data itself, it just delegates to the underlying WebRTC implementation. To answer your original question, you should be using simple-peer in Node as well by passing in your electron-webrtc instance as the wrtc option.

var wrtc = require('electron-webrtc')
var Peer = require('simple-peer')
var p = new Peer({ initiator: true, wrtc: wrtc })
p.on('signal', ...)