client_ws.go 585 B

123456789101112131415161718192021222324252627282930313233343536
  1. package cpn
  2. import (
  3. "github.com/gorilla/websocket"
  4. "one.com/nnet"
  5. )
  6. //
  7. type WsClient struct {
  8. *Hub
  9. addr string
  10. pos int //
  11. }
  12. func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) nnet.IHub {
  13. return &WsClient{
  14. Hub: newHub(cf, cb, p),
  15. pos: 0,
  16. }
  17. }
  18. func (self *WsClient) NewConnection(addr string, id uint64) error {
  19. conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
  20. if err != nil {
  21. return err
  22. }
  23. self.wg.Add(1)
  24. go func() {
  25. ses := newSession(NewWsConn(conn), self)
  26. ses.UpdateId(id)
  27. ses.Do()
  28. self.wg.Done()
  29. }()
  30. return nil
  31. }