acceptor.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "go.uber.org/zap"
  6. "net/http"
  7. "net/url"
  8. "time"
  9. "git.bvbej.com/bvbej/base-golang/pkg/websocket/peer"
  10. "github.com/gorilla/websocket"
  11. )
  12. var _ WsAcceptor = (*wsAcceptor)(nil)
  13. var upgrader = websocket.Upgrader{
  14. CheckOrigin: func(r *http.Request) bool {
  15. return true
  16. },
  17. }
  18. type WsAcceptor interface {
  19. Start(addr string, sessMgr *peer.SessionManager) error
  20. Stop(sessMgr *peer.SessionManager)
  21. }
  22. type wsAcceptor struct {
  23. server *http.Server
  24. logger *zap.Logger
  25. addr string
  26. }
  27. func NewWsAcceptor(loggers *zap.Logger) WsAcceptor {
  28. return &wsAcceptor{logger: loggers}
  29. }
  30. func (ws *wsAcceptor) Start(addr string, sessMgr *peer.SessionManager) error {
  31. urlObj, err := url.Parse(addr)
  32. if err != nil {
  33. return fmt.Errorf("websocket urlparse failed. url(%s) %v", addr, err)
  34. }
  35. if urlObj.Path == "" {
  36. return fmt.Errorf("websocket start failed. expect path in url to listen addr:%s", addr)
  37. }
  38. http.HandleFunc(urlObj.Path, func(w http.ResponseWriter, r *http.Request) {
  39. c, upgradeErr := upgrader.Upgrade(w, r, nil)
  40. if upgradeErr != nil {
  41. ws.logger.Sugar().Errorf("upgrade http failed: %s", upgradeErr)
  42. w.WriteHeader(http.StatusNotFound)
  43. _, _ = w.Write([]byte(http.StatusText(http.StatusNotFound)))
  44. return
  45. }
  46. sessMgr.Register <- peer.NewSession(newConnection(c, sessMgr))
  47. })
  48. ws.server = &http.Server{
  49. Addr: urlObj.Host,
  50. Handler: nil,
  51. }
  52. ws.addr = addr
  53. err = ws.server.ListenAndServe()
  54. if err != nil && err != http.ErrServerClosed {
  55. return fmt.Errorf("websocket ListenAndServe addr:%s failed:%v", addr, err)
  56. }
  57. return nil
  58. }
  59. func (ws *wsAcceptor) Stop(sessMgr *peer.SessionManager) {
  60. sessMgr.CloseAllSession()
  61. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  62. defer cancel()
  63. if err := ws.server.Shutdown(ctx); err != nil {
  64. ws.logger.Sugar().Errorf("server shutdown err:[%s]", err)
  65. }
  66. }