From 33db6dfaac71d2742af3d8ff2a38ca4073aa5966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Wed, 17 Nov 2021 05:19:31 -0300 Subject: [PATCH] improvements at NewConn - now w opts, more flexible --- smqcli/lib.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/smqcli/lib.go b/smqcli/lib.go index 7541f1a..76db6e6 100644 --- a/smqcli/lib.go +++ b/smqcli/lib.go @@ -1,6 +1,7 @@ package smqcli import ( + "crypto/tls" "encoding/json" "errors" "fmt" @@ -8,6 +9,7 @@ import ( "go.digitalcircle.com.br/open/simplemq/lib/random" "go.digitalcircle.com.br/open/simplemq/lib/types" "log" + "net/http" "regexp" "sync" "time" @@ -18,7 +20,7 @@ func dbg(s string, p ...interface{}) { } type MQConn struct { - url string + opts *MQConnOpts retry bool chRetry chan *struct{} //chReady chan *struct{} @@ -104,7 +106,7 @@ func (c *MQConn) Rpc(s string, pli []byte) ([]byte, error) { err := c.Write(m) select { case <-lchan: - case <-time.After(time.Second * 15): + case <-time.After(time.Second * time.Duration(c.opts.Timeout)): } close(lchan) lchan = nil @@ -190,7 +192,28 @@ func (c *MQConn) Init() error { c.retry = true doConn := func() error { - con, _, err := websocket.DefaultDialer.Dial(c.url, nil) + d := websocket.Dialer{ + NetDial: nil, + NetDialContext: nil, + Proxy: nil, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + HandshakeTimeout: 0, + ReadBufferSize: 0, + WriteBufferSize: 0, + WriteBufferPool: nil, + Subprotocols: nil, + EnableCompression: false, + Jar: nil, + } + h := http.Header{} + if c.opts.Headers != nil { + for k, v := range c.opts.Headers { + h.Set(k, v) + } + } + con, _, err := d.Dial(c.opts.Url, h) if err != nil { return err } @@ -225,9 +248,15 @@ func (c *MQConn) Init() error { return nil } -func New(u string) (*MQConn, error) { +type MQConnOpts struct { + Url string + Headers map[string]string + Timeout int +} + +func New(o *MQConnOpts) (*MQConn, error) { ret := &MQConn{ - url: u, + opts: o, } err := ret.Init() go func() {