|
@@ -1,25 +1,81 @@
|
|
|
package cpn
|
|
package cpn
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "github.com/gorilla/websocket"
|
|
|
|
|
"git.wanbits.io/joe/nnet"
|
|
"git.wanbits.io/joe/nnet"
|
|
|
|
|
+ "github.com/gorilla/websocket"
|
|
|
|
|
+ "net"
|
|
|
|
|
+ "time"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ WS = 1
|
|
|
|
|
+ TCP = 2
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-//
|
|
|
|
|
-type WsClient struct {
|
|
|
|
|
|
|
+type ClientOption func(*Client)
|
|
|
|
|
+type connector func(string, uint64) error
|
|
|
|
|
+
|
|
|
|
|
+func WithReconn(interval time.Duration) ClientOption {
|
|
|
|
|
+ return func(c *Client) {
|
|
|
|
|
+ c.reconnInterval = interval
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type Client struct {
|
|
|
*Hub
|
|
*Hub
|
|
|
- addr string
|
|
|
|
|
- pos int //
|
|
|
|
|
|
|
+ addr string
|
|
|
|
|
+ id uint64
|
|
|
|
|
+ ctr connector
|
|
|
|
|
+ reconnInterval time.Duration
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) nnet.IHub {
|
|
|
|
|
- return &WsClient{
|
|
|
|
|
- Hub: newHub(cf, cb, p),
|
|
|
|
|
- pos: 0,
|
|
|
|
|
|
|
+func NewClient(protocol int, cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, opts ...ClientOption) nnet.IHub {
|
|
|
|
|
+ c := &Client{
|
|
|
|
|
+ Hub: newHub(cf, cb, p),
|
|
|
|
|
+ reconnInterval: 0,
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ for _, opt := range opts {
|
|
|
|
|
+ opt(c)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ c.ctr = c.connectTcp
|
|
|
|
|
+ if protocol == WS {
|
|
|
|
|
+ c.ctr = c.connectWs
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return c
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, opts ...ClientOption) nnet.IHub {
|
|
|
|
|
+ return NewClient(WS, cf, cb, p, opts...)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (self *Client) NewConnection(addr string, id uint64) error {
|
|
|
|
|
+ self.addr, self.id = addr, id
|
|
|
|
|
+
|
|
|
|
|
+ if self.reconnInterval == 0 {
|
|
|
|
|
+ return self.ctr(addr, id)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ self.StartReconn()
|
|
|
|
|
+ return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (self *WsClient) NewConnection(addr string, id uint64) error {
|
|
|
|
|
|
|
+func (self *Client) StartReconn() {
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ var err error
|
|
|
|
|
+ for {
|
|
|
|
|
+ err = self.ctr(self.addr, self.id)
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ time.Sleep(self.reconnInterval)
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (self *Client) connectWs(addr string, id uint64) error {
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
|
|
conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -34,3 +90,23 @@ func (self *WsClient) NewConnection(addr string, id uint64) error {
|
|
|
|
|
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func (self *Client) connectTcp(addr string, id uint64) error {
|
|
|
|
|
+ tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ conn, err := net.DialTCP("tcp", nil, tcpAddr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ self.wg.Add(1)
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ ses := newSession(TcpConn{conn}, self)
|
|
|
|
|
+ ses.UpdateId(id)
|
|
|
|
|
+ ses.Do()
|
|
|
|
|
+ self.wg.Done()
|
|
|
|
|
+ }()
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|