client_ws.go 765 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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) *WsClient {
  13. return &WsClient{
  14. Hub: newHub(cf, cb, p),
  15. pos: 0,
  16. }
  17. }
  18. func (self *WsClient) Start() error {
  19. return nil
  20. }
  21. func (self *WsClient) NewConnection(addr string, id uint64) error {
  22. conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
  23. if err != nil {
  24. return err
  25. }
  26. self.wg.Add(1)
  27. go func() {
  28. ses := newSession(NewWsConn(conn), self)
  29. ses.UpdateId(id)
  30. ses.Do()
  31. self.wg.Done()
  32. }()
  33. return nil
  34. }
  35. func (self *WsClient) DoJob(int) {
  36. }
  37. func (self *WsClient) Stop() error {
  38. close(self.chQuit)
  39. self.wg.Wait()
  40. return nil
  41. }