Closed jjjjilyf closed 6 years ago
client 被设计为服务器内部通讯使用。如果你需要单独做一个 client 出来,可以单独写一个程序去使用 client,这时候 leaf 作为整个程序的一个基础库。
好的 3Q
请问大哥 这里应该改为大写吧 不然出了这个包 就访问不到这些conn了,根本没法发数据啦是不?
type WSClient struct { sync.Mutex Addr string ConnNum int ConnectInterval time.Duration PendingWriteNum int MaxMsgLen uint32 HandshakeTimeout time.Duration AutoReconnect bool NewAgent func(*WSConn) Agent dialer websocket.Dialer conns WebsocketConnSet wg sync.WaitGroup closeFlag bool *Conns []WSConn** }
func (client *WSClient) connect() { defer client.wg.Done() log.Debug("1") reconnect: log.Debug("2") conn := client.dial() if conn == nil { return } log.Debug("3") conn.SetReadLimit(int64(client.MaxMsgLen))
client.Lock()
if client.closeFlag {
client.Unlock()
conn.Close()
return
}
client.conns[conn] = struct{}{}
client.Unlock()
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen)
**client.Conns = append(client.Conns, wsConn)**
agent := client.NewAgent(wsConn)
agent.Run()
Connect执行不下去,只能打印到2,不会打印到3,一个都创建不成功,连的地址就是“127.0.0.1:80”,请问是不是还需要别的参数?
c := &network.WSClient{
**Addr: "ws://localhost:80",**
ConnNum: 10,
NewAgent:func(conn * network.WSConn) network.Agent{
a := &agent{conn: conn}
return a
},
}
c.Start()
builder := flatbuffers.NewBuilder(0)
for i, con := range c.Conns {
<-time.After(time.Duration(util.RandInterval(500, 3000)))
// 登录
strOff := builder.CreateString(strconv.Itoa(i+1))
msg.LoginOpenIDCheckREQStart(builder)
msg.LoginOpenIDCheckREQAddOpenID(builder, strOff)
mOff := msg.LoginOpenIDCheckREQEnd(builder)
builder.Finish(mOff)
buf := builder.FinishedBytes()
data := assambleFB(12, buf)
builder.Reset()
con.WriteMsg(data...)
}
你的用法不对。不要暴露 conns。你可以参考 leafserver 的 tcp server 的使用。client 和 server 很类似。
谢谢解答 我看下
type WSClient struct { sync.Mutex Addr string ConnNum int ConnectInterval time.Duration PendingWriteNum int MaxMsgLen uint32 HandshakeTimeout time.Duration AutoReconnect bool NewAgent func(*WSConn) Agent dialer websocket.Dialer conns WebsocketConnSet wg sync.WaitGroup closeFlag bool }
func (client *WSClient) Start() { client.init()
}