From 177f71f0bcc49de6943cbc568af178e75beffa12 Mon Sep 17 00:00:00 2001 From: Tristan Matthews Date: Wed, 12 Apr 2023 18:25:53 -0400 Subject: [PATCH 1/3] logger: do not write to stdout --- janus.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/janus.go b/janus.go index 09d4cc0..42f255b 100644 --- a/janus.go +++ b/janus.go @@ -92,7 +92,7 @@ func (gateway *Gateway) send(msg map[string]interface{}, transaction chan interf data, err := json.Marshal(msg) if err != nil { - fmt.Printf("json.Marshal: %s\n", err) + log.Printf("json.Marshal: %s\n", err) return } @@ -112,7 +112,7 @@ func (gateway *Gateway) send(msg map[string]interface{}, transaction chan interf select { case gateway.errors <- err: default: - fmt.Printf("conn.Write: %s\n", err) + log.Printf("conn.Write: %s\n", err) } return @@ -160,14 +160,14 @@ func (gateway *Gateway) recv() { select { case gateway.errors <- err: default: - fmt.Printf("conn.Read: %s\n", err) + log.Printf("conn.Read: %s\n", err) } return } if err := json.Unmarshal(data, &base); err != nil { - fmt.Printf("json.Unmarshal: %s\n", err) + log.Printf("json.Unmarshal: %s\n", err) continue } @@ -181,13 +181,13 @@ func (gateway *Gateway) recv() { typeFunc, ok := msgtypes[base.Type] if !ok { - fmt.Printf("Unknown message type received!\n") + log.Printf("Unknown message type received!\n") continue } msg := typeFunc() if err := json.Unmarshal(data, &msg); err != nil { - fmt.Printf("json.Unmarshal: %s\n", err) + log.Printf("json.Unmarshal: %s\n", err) continue // Decode error } @@ -211,7 +211,7 @@ func (gateway *Gateway) recv() { session := gateway.Sessions[base.Session] gateway.Unlock() if session == nil { - fmt.Printf("Unable to deliver message. Session gone?\n") + log.Printf("Unable to deliver message. Session gone?\n") continue } @@ -220,7 +220,7 @@ func (gateway *Gateway) recv() { handle := session.Handles[base.Handle] session.Unlock() if handle == nil { - fmt.Printf("Unable to deliver message. Handle gone?\n") + log.Printf("Unable to deliver message. Handle gone?\n") continue } From 31876832abb2edad71d28eeb10ca625fa2955f21 Mon Sep 17 00:00:00 2001 From: Tristan Matthews Date: Tue, 26 Jul 2022 09:56:27 -0400 Subject: [PATCH 2/3] transport: allow setting a request header on the WS This is an API breaking change but the old behaviour can be used if `nil` is given as the new argument to Connect. --- janus.go | 5 +++-- janus_test.go | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/janus.go b/janus.go index 42f255b..30ea321 100644 --- a/janus.go +++ b/janus.go @@ -6,6 +6,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/http" "log" "os" "sync" @@ -49,10 +50,10 @@ func generateTransactionId() xid.ID { } // Connect initiates a webscoket connection with the Janus Gateway -func Connect(wsURL string) (*Gateway, error) { +func Connect(wsURL string, requestHeader http.Header) (*Gateway, error) { websocket.DefaultDialer.Subprotocols = []string{"janus-protocol"} - conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) + conn, _, err := websocket.DefaultDialer.Dial(wsURL, requestHeader) if err != nil { return nil, err diff --git a/janus_test.go b/janus_test.go index cfd12f4..02ecd5f 100644 --- a/janus_test.go +++ b/janus_test.go @@ -1,12 +1,14 @@ package janus import ( + "net/http" "testing" ) func Test_Connect(t *testing.T) { - client, err := Connect("ws://39.106.248.166:8188/") + reqHeader := http.Header{"User-Agent": []string{"janus-test"}} + client, err := Connect("ws://39.106.248.166:8188/", reqHeader) if err != nil { t.Fail() return From ee1f957d6b04b3dd70023ce2d731c3f30299729a Mon Sep 17 00:00:00 2001 From: Tristan Matthews Date: Tue, 26 Jul 2022 09:58:18 -0400 Subject: [PATCH 3/3] janus: fix typos --- janus.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/janus.go b/janus.go index 30ea321..0eb4b06 100644 --- a/janus.go +++ b/janus.go @@ -34,7 +34,7 @@ type Gateway struct { Sessions map[uint64]*Session // Access to the Sessions map should be synchronized with the Gateway.Lock() - // and Gateway.Unlock() methods provided by the embeded sync.Mutex. + // and Gateway.Unlock() methods provided by the embedded sync.Mutex. sync.Mutex conn *websocket.Conn @@ -49,7 +49,7 @@ func generateTransactionId() xid.ID { return xid.New() } -// Connect initiates a webscoket connection with the Janus Gateway +// Connect initiates a websocket connection with the Janus Gateway func Connect(wsURL string, requestHeader http.Header) (*Gateway, error) { websocket.DefaultDialer.Subprotocols = []string{"janus-protocol"}