mirror of
https://github.com/pion/webrtc.git
synced 2025-10-13 02:43:48 +08:00
example: data-channels-create
This commit is contained in:
@@ -5,28 +5,39 @@ let log = msg => {
|
||||
document.getElementById('logs').innerHTML += msg + '<br>'
|
||||
}
|
||||
|
||||
let sendChannel = pc.createDataChannel()
|
||||
console.log(sendChannel.id)
|
||||
sendChannel.onclose = () => console.log('sendChannel has closed')
|
||||
sendChannel.onopen = () => console.log('sendChannel has opened')
|
||||
sendChannel.onmessage = e => log(`sendChannel got '${e.data}'`)
|
||||
// let sendChannel = pc.createDataChannel('foo')
|
||||
// sendChannel.onclose = () => console.log('sendChannel has closed')
|
||||
// sendChannel.onopen = () => console.log('sendChannel has opened')
|
||||
// sendChannel.onmessage = e => log(`sendChannel got '${e.data}'`)
|
||||
|
||||
pc.onsignalingstatechange = e => log(pc.signalingState)
|
||||
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
||||
pc.onicecandidate = event => {
|
||||
if (event.candidate === null) {
|
||||
document.getElementById('localSessionDescription').value = btoa(pc.localDescription.sdp)
|
||||
}
|
||||
}
|
||||
|
||||
pc.onnegotiationneeded = e =>
|
||||
pc.createOffer({ }).then(d => {
|
||||
document.getElementById('localSessionDescription').value = btoa(d.sdp)
|
||||
return pc.setLocalDescription(d)
|
||||
}).catch(log)
|
||||
|
||||
pc.ondatachannel = e => {
|
||||
log('got dc')
|
||||
dc = e.channel
|
||||
dc.onclose = () => console.log('dc has closed')
|
||||
dc.onopen = () => console.log('dc has opened')
|
||||
dc.onmessage = e => log(`dc got '${e.data}'`)
|
||||
window.sendMessage = () => {
|
||||
let message = document.getElementById('message').value
|
||||
if (message === '') {
|
||||
return alert('Message must not be empty')
|
||||
}
|
||||
|
||||
sendChannel.send(message)
|
||||
dc.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
// pc.onnegotiationneeded = e =>
|
||||
// pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
|
||||
|
||||
|
||||
|
||||
window.startSession = () => {
|
||||
let sd = document.getElementById('remoteSessionDescription').value
|
||||
@@ -34,9 +45,10 @@ window.startSession = () => {
|
||||
return alert('Session Description must not be empty')
|
||||
}
|
||||
|
||||
try {
|
||||
pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: atob(sd)}))
|
||||
} catch (e) {
|
||||
alert(e)
|
||||
}
|
||||
log(atob(sd))
|
||||
pc.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: atob(sd)})).catch(log)
|
||||
|
||||
log("ss", pc.signalingState)
|
||||
pc.createAnswer().then(d => pc.setLocalDescription(d)).catch(log)
|
||||
|
||||
}
|
||||
|
@@ -25,20 +25,6 @@ func randSeq(n int) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
rawSd, err := reader.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
sd, err := base64.StdEncoding.DecodeString(rawSd)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
/* Everything below is the pion-WebRTC API, thanks for using it! */
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{
|
||||
ICEServers: []webrtc.RTCICEServer{
|
||||
@@ -57,8 +43,6 @@ func main() {
|
||||
fmt.Printf("Connection State has changed %s \n", connectionState.String())
|
||||
}
|
||||
|
||||
// TODO: We have to send the offer otherwise it's empty.
|
||||
|
||||
d, err := peerConnection.CreateDataChannel("data", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -79,23 +63,28 @@ func main() {
|
||||
}
|
||||
d.Unlock()
|
||||
|
||||
// Set the remote SessionDescription
|
||||
offer := webrtc.RTCSessionDescription{
|
||||
Type: webrtc.RTCSdpTypeOffer,
|
||||
Sdp: string(sd),
|
||||
}
|
||||
if err := peerConnection.SetRemoteDescription(offer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
answer, err := peerConnection.CreateAnswer(nil)
|
||||
offer, err := peerConnection.CreateOffer(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
/* Signaling via STDIN */
|
||||
|
||||
// Get the LocalDescription and take it to base64 so we can paste in browser
|
||||
fmt.Println(base64.StdEncoding.EncodeToString([]byte(answer.Sdp)))
|
||||
fmt.Println(base64.StdEncoding.EncodeToString([]byte(offer.Sdp)))
|
||||
sd := mustReadStdin()
|
||||
|
||||
/* --- */
|
||||
|
||||
// Set the remote SessionDescription
|
||||
answer := webrtc.RTCSessionDescription{
|
||||
Type: webrtc.RTCSdpTypeAnswer,
|
||||
Sdp: sd,
|
||||
}
|
||||
if err := peerConnection.SetRemoteDescription(answer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Random messages will now be sent to any connected DataChannels every 5 seconds")
|
||||
for {
|
||||
time.Sleep(5 * time.Second)
|
||||
@@ -108,3 +97,18 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustReadStdin() string {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
rawSd, err := reader.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
sd, err := base64.StdEncoding.DecodeString(rawSd)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(sd)
|
||||
}
|
||||
|
@@ -254,7 +254,6 @@ func localDirection(weSend bool, peerDirection RTCRtpTransceiverDirection) RTCRt
|
||||
}
|
||||
|
||||
func (r *RTCPeerConnection) addRTPMediaSection(d *sdp.SessionDescription, codecType RTCRtpCodecType, midValue string, peerDirection RTCRtpTransceiverDirection, candidates []string) {
|
||||
|
||||
media := sdp.NewJSEPMediaDescription(codecType.String(), []string{}).
|
||||
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
|
||||
WithValueAttribute(sdp.AttrKeyMID, midValue).
|
||||
|
Reference in New Issue
Block a user