From d1a79f49acfb30c48824acc4cf0f4aa34b053a49 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 Apr 2020 17:57:32 +0200 Subject: [PATCH] WIP --- relay.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/relay.go b/relay.go index a8b3c88..cfdbbf3 100644 --- a/relay.go +++ b/relay.go @@ -21,6 +21,9 @@ const ( // CANDIDATE This is a candidate CANDIDATE = iota + + // CloseConnection Requests the connection to be closed + CloseConnection = iota ) type receivedSignal struct { @@ -58,8 +61,20 @@ func onSignal(callHash, peerID string, data map[string]interface{}) { if data["type"] == "SDP" { newSignal.sigType = SDP - newSignal.offer.Type = webrtc.SDPTypeOffer - newSignal.offer.SDP = data["data"].(map[string]interface{})["sdp"].(string) + + // I have to re-encode data to initialize SDP + var enc []byte + enc, err := json.Marshal(data["data"]) + if err != nil { + log.Printf("Could not re-encode candidate ! %s", err) + return + } + + err = json.Unmarshal(enc, &newSignal.offer) + if err != nil { + log.Printf("Discarding invalid candidate: %s", err) + return + } } else if data["type"] == "CANDIDATE" { newSignal.sigType = CANDIDATE @@ -78,6 +93,11 @@ func onSignal(callHash, peerID string, data map[string]interface{}) { return } + } else if data["type"] == "CLOSE_CONN" { + + // Close connection + newSignal.sigType = CloseConnection + } else { log.Fatalf("Invalid signal type: %s !", data["type"]) } @@ -112,6 +132,13 @@ func onSignal(callHash, peerID string, data map[string]interface{}) { } } +/// Request connections to be closed +func onCloseConnection(callHash, peerID string) { + onSignal(callHash, peerID, map[string]interface{}{ + "type": "CLOSE_CONN", + }) +} + /// Close a connection func closeConnection(r activeRelay) { log.Printf("Closing call %s / id: %d", r.callHash, r.id)