Hi,
I am attempting to send out message utilizing webrtc datachannel in go lang. However the all set state of information network is constantly linking
So, I am incapable to send out message. Can any individual aid me to locate the concern?
plan primary.
import (.
" fmt".
" log".
"<< http://github.com/pion/webrtc/v3|github.com/pion/webrtc/v3>".
).
func primary() {
// Establish a WebRTC link.
peerConnection, err:= webrtc.NewPeerConnection( webrtc.Configuration {
ICEServers: [] webrtc.ICEServer {
{
Links: [] string stun.l.google.com:19302"> >,.
},.
},.
} ).
if err!= nil {
log.Fatal( err).
}
// Establish an information network.
dataChannel, err:= peerConnection.CreateDataChannel(" myDataChannel", nil).
if err!= nil {
log.Fatal( err).
}
// Establish network message trainers.
dataChannel.OnOpen( func() {
fmt.Println(" Information transport opened up").
} ).
dataChannel.OnMessage( func( msg webrtc.DataChannelMessage) {
fmt.Printf(" Message from information network: %sn", string( msg.Data)).
} ).
fmt.Println( dataChannel.ReadyState()).
// Send out a message over the information network.
message:= [] byte(" Hello there, globe!").
if err:= dataChannel.Send( message); err!= nil {
log.Fatal( err).
}
// Shut the link when the program is ended up.
postpone peerConnection.Close().
// Wait forever.
pick {}
}
This does not appear like a legitimate link. Does it in fact appear like this in your code, or did somethingbhappen in the copy-paste right into this online forum?
This instance helps me, just to see this instance: webrtc/examples/data-channels at master · pion/webrtc · GitHub
plan primary.
import (.
" bufio".
" bytes".
" compress/gzip".
" encoding/base64".
" encoding/json".
" fmt".
" io".
" io/ioutil".
" os".
" strings".
" time".
" github.com/pion/randutil".
" github.com/pion/webrtc/v3".
).
const compress = incorrect.
func primary() {
// Whatever listed below is the Pion WebRTC API! Many thanks for utilizing it ❤.
// Prepare the setup.
config:= webrtc.Configuration {
ICEServers: [] webrtc.ICEServer {
{
Links: [] string {"stun: stun.l.google.com:19302"},.
},.
},.
}
// Produce a brand-new RTCPeerConnection.
peerConnection, err:= webrtc.NewPeerConnection( config).
if err!= nil {
panic( err).
}
postpone func() {
if cErr:= peerConnection.Close(); cErr!= nil {
fmt.Printf(" can not shut peerConnection: %vn", cErr).
}
}()
// Establish the trainer for Peer link state.
// This will certainly alert you when the peer has actually connected/disconnected.
peerConnection.OnConnectionStateChange( func( s webrtc.PeerConnectionState) {
fmt.Printf(" Peer Link State has actually transformed: %sn", s.String()).
if s == webrtc.PeerConnectionStateFailed {
// Wait till PeerConnection has actually had no network task for 30 secs or one more failing. It might be reconnected utilizing an ICE Restart.
// Usage webrtc.PeerConnectionStateDisconnected if you have an interest in identifying much faster timeout.
// Keep in mind that the PeerConnection might return from PeerConnectionStateDisconnected.
fmt.Println(" Peer Link has actually mosted likely to fallen short leaving").
os.Exit( 0 ).
}
} ).
// Register information network development handling.
peerConnection.OnDataChannel( func( d * webrtc.DataChannel) {
fmt.Printf(" New DataChannel %s %dn", d.Label(), d.ID()).
// Register network opening up handling.
d.OnOpen( func() {
fmt.Printf(" Information transport '% s'-'% d' open. Random messages will certainly currently be sent out to any type of linked DataChannels every 5 secondsn", d.Label(), d.ID()).
for array time.NewTicker( 5 * time.Second). C {
message:= RandSeq( 15 ).
fmt.Printf(" Sending out '% s' n", message).
// Send out the message as message.
sendErr:= d.SendText( message).
if sendErr!= nil {
panic( sendErr).
}
}
} ).
// Register sms message handling.
d.OnMessage( func( msg webrtc.DataChannelMessage) {
fmt.Printf(" Message from DataChannel '% s': '% s' n", d.Label(), string( msg.Data)).
} ).
} ).
// Wait on the deal to be pasted.
deal:= webrtc.SessionDescription {}
Decipher( MustReadStdin(), && deal)
.// Establish the remote SessionDescription.
err = peerConnection.SetRemoteDescription( deal).
if err!= nil {
panic( err).
}
// Produce a response.
solution, err:= peerConnection.CreateAnswer( nil).
if err!= nil {
panic( err).
}
// Produce network that is obstructed till ICE Celebration is total.
gatherComplete:= webrtc.GatheringCompletePromise( peerConnection).
// Establishes the LocalDescription, as well as begins our UDP audiences.
err = peerConnection.SetLocalDescription( solution).
if err!= nil {
panic( err).
}
// Block till ICE Celebration is total, disabling flow ICE.
// we do this since we just can trade one signaling message.
// in a manufacturing application you ought to trade ICE Prospects through OnICECandidate.
< 0 {
break.
}
}
fmt.Println("").
return in.
}
func Encode( obj user interface {}) string {
b, err:= json.Marshal( obj).
if err!= nil {
panic( err).
}
if compress {
b = zip( b).
}
return base64.StdEncoding.EncodeToString( b).
}
func zip( in [] byte) [] byte {
var b bytes.Buffer.
gz:= gzip.NewWriter(&& b)
. _, err:= gz.Write (in ).
if err!=
nil {panic( err ).
}
err = gz.Flush().
if err!= nil {
panic( err).
}
err= gz.Close().
if err!= nil
{panic (err).
} return b.Bytes()}
func unzip( in[] byte) (* )byte {
var b bytes.Buffer.
_, err:= b.Write(&in).
if err!= nil {
panic( err).
}
r, err:= gzip.NewReader( & b).
if err!= nil {
panic( err).
}
res, err: = ioutil.ReadAll( r).
if err! = nil {panic (err).} return res.} [].