]> 127.0.0.1 Git - part/.git/commitdiff
websocket support proxy v0.5.10
authorqydysky <qydysky@foxmail.com>
Sat, 24 Apr 2021 13:57:15 +0000 (21:57 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 24 Apr 2021 13:57:15 +0000 (21:57 +0800)
websocket/Client.go

index 5d58982f6d763f0dca0bbe90030e6eabdb488ef5..e94a1e64ea304d81aa49bd08e2bf7f379355a566 100644 (file)
@@ -4,6 +4,7 @@ import (
        "time"
        "errors"
        "net/http"
+       "net/url"
        "reflect"
 
        "github.com/gorilla/websocket"
@@ -18,6 +19,7 @@ type Client struct {
 
        TO int
        Header map[string]string
+       Proxy string
        
        Ping Ping
 
@@ -54,6 +56,7 @@ func New_client(config Client) (o *Client) {
        tmp.Header = config.Header
        if v := config.Func_normal_close;v != nil {tmp.Func_normal_close = v}
        if v := config.Func_abort_close;v != nil {tmp.Func_abort_close = v}
+       if v := config.Proxy;v != "" {tmp.Proxy = v}
        if config.Ping.Period != 0 {tmp.Ping = config.Ping}
        return &tmp
 }
@@ -80,7 +83,16 @@ func (i *Client) Handle() (o *Client) {
                for k,v := range o.Header {
                        tmp_Header.Set(k, v)
                }
-               c, _, err := websocket.DefaultDialer.Dial(o.Url, tmp_Header)
+
+               dial := websocket.DefaultDialer
+               if o.Proxy != "" {
+                       proxy := func(_ *http.Request) (*url.URL, error) {
+                               return url.Parse(o.Proxy)
+                       }
+                       dial.Proxy = proxy
+               }
+               c, _, err := dial.Dial(o.Url, tmp_Header)
+
                if err != nil {return}
                defer c.Close()