name5566 / leaf

A game server framework in Go (golang)
Apache License 2.0
5.3k stars 1.32k forks source link

请问一下 ws_client是怎么用的,start后没有返回链接,该怎么和服务器通讯呢?是和服务器同一个进程还是另生成一个exe? #117

Closed jjjjilyf closed 6 years ago

jjjjilyf commented 6 years ago

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()

for i := 0; i < client.ConnNum; i++ {
    client.wg.Add(1)
    go client.connect()
}

}

name5566 commented 6 years ago

client 被设计为服务器内部通讯使用。如果你需要单独做一个 client 出来,可以单独写一个程序去使用 client,这时候 leaf 作为整个程序的一个基础库。

jjjjilyf commented 6 years ago

好的 3Q

jjjjilyf commented 6 years ago

image 请问大哥 这里应该改为大写吧 不然出了这个包 就访问不到这些conn了,根本没法发数据啦是不?

jjjjilyf commented 6 years ago

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...)
}
name5566 commented 6 years ago

你的用法不对。不要暴露 conns。你可以参考 leafserver 的 tcp server 的使用。client 和 server 很类似。

jjjjilyf commented 6 years ago

谢谢解答 我看下