github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/network/web_dialer.go (about) 1 package network 2 3 import ( 4 "context" 5 "github.com/15mga/kiwi" 6 "net/http" 7 8 "github.com/15mga/kiwi/util" 9 "github.com/fasthttp/websocket" 10 ) 11 12 type websocketDialer struct { 13 name string 14 url string 15 header http.Header 16 agent *webAgent 17 receiver kiwi.FnAgentBytes 18 } 19 20 func NewWebDialer(url string, header http.Header, msgType int, receiver kiwi.FnAgentBytes, options ...kiwi.AgentOption) kiwi.IDialer { 21 d := &websocketDialer{ 22 url: url, 23 header: header, 24 receiver: receiver, 25 agent: NewWebAgent(url, msgType, receiver, options...), 26 } 27 return d 28 } 29 30 func (d *websocketDialer) Name() string { 31 return d.name 32 } 33 34 func (d *websocketDialer) Connect(ctx context.Context) *util.Err { 35 conn, _, err := websocket.DefaultDialer.Dial(d.url, d.header) 36 if err != nil { 37 return util.NewErr(util.EcConnectErr, util.M{ 38 "url": d.url, 39 "error": err.Error(), 40 }) 41 } 42 43 d.agent.Start(ctx, conn) 44 return nil 45 } 46 47 func (d *websocketDialer) Agent() kiwi.IAgent { 48 return d.agent 49 }