mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 15:16:52 +08:00
Add data-channels jsfiddle to git
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# data-channels
|
# data-channels
|
||||||
TODO
|
data-channels is a pion-WebRTC application that shows how you can send/recv DataChannel messages from a web browser
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
### Download data-channels
|
### Download data-channels
|
||||||
@@ -8,10 +8,10 @@ go get github.com/pions/webrtc/examples/data-channels
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Open data-channels example page
|
### Open data-channels example page
|
||||||
[jsfiddle.net](http://jsfiddle.net/5hqt08fe/9/)
|
[jsfiddle.net](https://jsfiddle.net/gh/get/library/pure/pions/webrtc/tree/master/examples/data-channels/jsfiddle)
|
||||||
|
|
||||||
### Run data-channels, with your browsers SessionDescription as stdin
|
### Run data-channels, with your browsers SessionDescription as stdin
|
||||||
In the jsfiddle the top textarea is your browser, copy that and:
|
In the jsfiddle the top textarea is your browser's session description, copy that and:
|
||||||
#### Linux/macOS
|
#### Linux/macOS
|
||||||
Run `echo $BROWSER_SDP | data-channels`
|
Run `echo $BROWSER_SDP | data-channels`
|
||||||
#### Windows
|
#### Windows
|
||||||
@@ -22,6 +22,10 @@ Run `echo $BROWSER_SDP | data-channels`
|
|||||||
Copy the text that `data-channels` just emitted and copy into second text area
|
Copy the text that `data-channels` just emitted and copy into second text area
|
||||||
|
|
||||||
### Hit 'Start Session' in jsfiddle
|
### Hit 'Start Session' in jsfiddle
|
||||||
TODO
|
Under Start Session you should see 'Checking' as it starts connecting. If everything worked you should see `New DataChannel foo 1`
|
||||||
|
|
||||||
|
Now you can put whatever you want in the `Message` textarea, and when you hit `Send Message` it should appear in your browser!
|
||||||
|
|
||||||
|
You can also type in your terminal, and when you hit enter it will appear in your web browser.
|
||||||
|
|
||||||
Congrats, you have used pion-WebRTC! Now start building something cool
|
Congrats, you have used pion-WebRTC! Now start building something cool
|
||||||
|
1
examples/data-channels/jsfiddle/demo.css
Normal file
1
examples/data-channels/jsfiddle/demo.css
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
5
examples/data-channels/jsfiddle/demo.details
Normal file
5
examples/data-channels/jsfiddle/demo.details
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
name: data-channels
|
||||||
|
description: Example of using pion-WebRTC to communicate with a web browser using bi-direction DataChannels
|
||||||
|
authors:
|
||||||
|
- Sean DuBois
|
9
examples/data-channels/jsfiddle/demo.html
Normal file
9
examples/data-channels/jsfiddle/demo.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
Browser base64 Session Description <textarea id="localSessionDescription" readonly="true"></textarea> <br />
|
||||||
|
Golang base64 Session Description: <textarea id="remoteSessionDescription"></textarea> <br/>
|
||||||
|
<button onclick="window.startSession()"> Start Session </button> <br />
|
||||||
|
<br />
|
||||||
|
|
||||||
|
Message: <textarea id="message">This is my DataChannel message!</textarea> <br/>
|
||||||
|
<button onclick="window.sendMessage()"> Send Message </button> <br />
|
||||||
|
|
||||||
|
<div id="logs"></div>
|
41
examples/data-channels/jsfiddle/demo.js
Normal file
41
examples/data-channels/jsfiddle/demo.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* eslint-env browser */
|
||||||
|
|
||||||
|
let pc = new RTCPeerConnection()
|
||||||
|
let log = msg => {
|
||||||
|
document.getElementById('logs').innerHTML += msg + '<br>'
|
||||||
|
}
|
||||||
|
|
||||||
|
let sendChannel = pc.createDataChannel('foo')
|
||||||
|
sendChannel.onclose = () => console.log('sendChannel has closed')
|
||||||
|
sendChannel.onopen = () => console.log('sendChannel has opened')
|
||||||
|
sendChannel.onmessage = e => console.log(`sendChannel got ${e.data}`)
|
||||||
|
|
||||||
|
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
||||||
|
|
||||||
|
pc.onnegotiationneeded = e =>
|
||||||
|
pc.createOffer({ }).then(d => {
|
||||||
|
document.getElementById('localSessionDescription').value = btoa(d.sdp)
|
||||||
|
return pc.setLocalDescription(d)
|
||||||
|
}).catch(log)
|
||||||
|
|
||||||
|
window.sendMessage = () => {
|
||||||
|
let message = document.getElementById('message').value
|
||||||
|
if (message === '') {
|
||||||
|
return alert('Message must not be empty')
|
||||||
|
}
|
||||||
|
|
||||||
|
sendChannel.send(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
window.startSession = () => {
|
||||||
|
let sd = document.getElementById('remoteSessionDescription').value
|
||||||
|
if (sd === '') {
|
||||||
|
return alert('Session Description must not be empty')
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: atob(sd)}))
|
||||||
|
} catch (e) {
|
||||||
|
alert(e)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user