Skip to content

Commit

Permalink
Merge pull request #31 from tcriess/change-remove-json-double-encoding
Browse files Browse the repository at this point in the history
Remove json double encodings in websocket communication
  • Loading branch information
GRVYDEV committed Feb 21, 2021
2 parents ec2e136 + 8a86fc8 commit f2aa04b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
hub.RLock()
if _, ok := hub.Clients[c]; ok {
Expand Down Expand Up @@ -264,7 +264,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 {
hub.RLock()
if _, ok := hub.Clients[c]; ok {
Expand Down
4 changes: 2 additions & 2 deletions ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ws/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 {
Expand Down
6 changes: 4 additions & 2 deletions ws/message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ws

import "encoding/json"

const (
MessageTypeAnswer = "answer"
MessageTypeCandidate = "candidate"
Expand All @@ -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"`
}

0 comments on commit f2aa04b

Please sign in to comment.