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,38 +5,50 @@ let log = msg => {
|
|||||||
document.getElementById('logs').innerHTML += msg + '<br>'
|
document.getElementById('logs').innerHTML += msg + '<br>'
|
||||||
}
|
}
|
||||||
|
|
||||||
let sendChannel = pc.createDataChannel()
|
// let sendChannel = pc.createDataChannel('foo')
|
||||||
console.log(sendChannel.id)
|
// sendChannel.onclose = () => console.log('sendChannel has closed')
|
||||||
sendChannel.onclose = () => console.log('sendChannel has closed')
|
// sendChannel.onopen = () => console.log('sendChannel has opened')
|
||||||
sendChannel.onopen = () => console.log('sendChannel has opened')
|
// sendChannel.onmessage = e => log(`sendChannel got '${e.data}'`)
|
||||||
sendChannel.onmessage = e => log(`sendChannel got '${e.data}'`)
|
|
||||||
|
|
||||||
|
pc.onsignalingstatechange = e => log(pc.signalingState)
|
||||||
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
|
||||||
|
pc.onicecandidate = event => {
|
||||||
pc.onnegotiationneeded = e =>
|
if (event.candidate === null) {
|
||||||
pc.createOffer({ }).then(d => {
|
document.getElementById('localSessionDescription').value = btoa(pc.localDescription.sdp)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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')
|
||||||
|
}
|
||||||
|
|
||||||
|
dc.send(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pc.onnegotiationneeded = e =>
|
||||||
|
// pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
window.startSession = () => {
|
window.startSession = () => {
|
||||||
let sd = document.getElementById('remoteSessionDescription').value
|
let sd = document.getElementById('remoteSessionDescription').value
|
||||||
if (sd === '') {
|
if (sd === '') {
|
||||||
return alert('Session Description must not be empty')
|
return alert('Session Description must not be empty')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
log(atob(sd))
|
||||||
pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: atob(sd)}))
|
pc.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: atob(sd)})).catch(log)
|
||||||
} catch (e) {
|
|
||||||
alert(e)
|
log("ss", pc.signalingState)
|
||||||
}
|
pc.createAnswer().then(d => pc.setLocalDescription(d)).catch(log)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -25,20 +25,6 @@ func randSeq(n int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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
|
// Create a new RTCPeerConnection
|
||||||
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{
|
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{
|
||||||
ICEServers: []webrtc.RTCICEServer{
|
ICEServers: []webrtc.RTCICEServer{
|
||||||
@@ -57,8 +43,6 @@ func main() {
|
|||||||
fmt.Printf("Connection State has changed %s \n", connectionState.String())
|
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)
|
d, err := peerConnection.CreateDataChannel("data", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -79,23 +63,28 @@ func main() {
|
|||||||
}
|
}
|
||||||
d.Unlock()
|
d.Unlock()
|
||||||
|
|
||||||
// Set the remote SessionDescription
|
offer, err := peerConnection.CreateOffer(nil)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Signaling via STDIN */
|
||||||
|
|
||||||
// Get the LocalDescription and take it to base64 so we can paste in browser
|
// 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")
|
fmt.Println("Random messages will now be sent to any connected DataChannels every 5 seconds")
|
||||||
for {
|
for {
|
||||||
time.Sleep(5 * time.Second)
|
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) {
|
func (r *RTCPeerConnection) addRTPMediaSection(d *sdp.SessionDescription, codecType RTCRtpCodecType, midValue string, peerDirection RTCRtpTransceiverDirection, candidates []string) {
|
||||||
|
|
||||||
media := sdp.NewJSEPMediaDescription(codecType.String(), []string{}).
|
media := sdp.NewJSEPMediaDescription(codecType.String(), []string{}).
|
||||||
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
|
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
|
||||||
WithValueAttribute(sdp.AttrKeyMID, midValue).
|
WithValueAttribute(sdp.AttrKeyMID, midValue).
|
||||||
|
Reference in New Issue
Block a user