init.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package hook
  2. import (
  3. "github.com/gorilla/websocket"
  4. "github.com/imroc/req/v3"
  5. "net/url"
  6. "time"
  7. )
  8. type Hook struct {
  9. ServerIp string
  10. AdminPort string
  11. WxPort string
  12. Client *req.Client
  13. WorkPhoneUrl string
  14. }
  15. func NewHook(serverIp string, adminPort string, WxPort string) *Hook {
  16. client := req.C().DevMode()
  17. client.SetCommonRetryCount(2).
  18. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  19. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  20. workPhoneUrl := url.URL{Scheme: "ws", Host: "chat.gkscrm.com:13088"}
  21. return &Hook{
  22. ServerIp: serverIp,
  23. AdminPort: adminPort,
  24. WxPort: WxPort,
  25. Client: req.C().DevMode(),
  26. WorkPhoneUrl: workPhoneUrl.String(),
  27. }
  28. }
  29. func (h *Hook) setServerIp(ip string) *Hook {
  30. h.ServerIp = ip
  31. return h
  32. }
  33. func (h *Hook) setAdminPort(port string) *Hook {
  34. h.AdminPort = port
  35. return h
  36. }
  37. func (h *Hook) setWxPort(port string) *Hook {
  38. h.WxPort = port
  39. return h
  40. }
  41. func (h *Hook) connWorkPhone() (*websocket.Conn, error) {
  42. dialer := websocket.DefaultDialer
  43. conn, _, err := dialer.Dial(h.WorkPhoneUrl, nil)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return conn, nil
  48. }