From 8a86fc81357f11935448b4074b7aab76e549eb32 Mon Sep 17 00:00:00 2001
From: Thorsten Riess
Date: Sat, 20 Feb 2021 14:07:40 +0100
Subject: [PATCH] Remove json double encodings in websocket communication
---
main.go | 4 ++--
ws/client.go | 4 ++--
ws/hub.go | 2 +-
ws/message.go | 6 ++++--
4 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/main.go b/main.go
index a750ad9..a3b1278 100644
--- a/main.go
+++ b/main.go
@@ -222,7 +222,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
if msg, err := json.Marshal(ws.WebsocketMessage{
Event: ws.MessageTypeCandidate,
- Data: string(candidateString),
+ Data: candidateString,
}); err == nil {
c.Send <- msg
} else {
@@ -260,7 +260,7 @@ func websocketHandler(w http.ResponseWriter, r *http.Request) {
if msg, err := json.Marshal(ws.WebsocketMessage{
Event: ws.MessageTypeOffer,
- Data: string(offerString),
+ Data: offerString,
}); err == nil {
c.Send <- msg
} else {
diff --git a/ws/client.go b/ws/client.go
index daa86a9..c96d26e 100644
--- a/ws/client.go
+++ b/ws/client.go
@@ -66,7 +66,7 @@ func (c *Client) ReadLoop() {
switch message.Event {
case MessageTypeCandidate:
candidate := webrtc.ICECandidateInit{}
- if err := json.Unmarshal([]byte(message.Data), &candidate); err != nil {
+ if err := json.Unmarshal(message.Data, &candidate); err != nil {
log.Printf("could not unmarshal candidate msg: %s", err)
return
}
@@ -78,7 +78,7 @@ func (c *Client) ReadLoop() {
case MessageTypeAnswer:
answer := webrtc.SessionDescription{}
- if err := json.Unmarshal([]byte(message.Data), &answer); err != nil {
+ if err := json.Unmarshal(message.Data, &answer); err != nil {
log.Printf("could not unmarshal answer msg: %s", err)
return
}
diff --git a/ws/hub.go b/ws/hub.go
index a887580..31e6e01 100644
--- a/ws/hub.go
+++ b/ws/hub.go
@@ -79,7 +79,7 @@ func (h *Hub) SendInfo(info Info) {
}
if msg, err := json.Marshal(WebsocketMessage{
Event: MessageTypeInfo,
- Data: string(i),
+ Data: i,
}); err == nil {
h.Broadcast <- msg
} else {
diff --git a/ws/message.go b/ws/message.go
index 6c2d721..ede43ea 100644
--- a/ws/message.go
+++ b/ws/message.go
@@ -1,5 +1,7 @@
package ws
+import "encoding/json"
+
const (
MessageTypeAnswer = "answer"
MessageTypeCandidate = "candidate"
@@ -8,6 +10,6 @@ const (
)
type WebsocketMessage struct {
- Event string `json:"event"`
- Data string `json:"data"`
+ Event string `json:"event"`
+ Data json.RawMessage `json:"data"`
}