MCPcopy
hub / github.com/aceld/zinx / Start

Method Start

zinx_app_demo/mmo_game/client_AI_robot.go:201–245  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

199}
200
201func (this *TcpClient) Start() {
202 go func() {
203 for {
204 //读取服务端发来的数据 ==》 SyncPID
205 //1.读取8字节
206 //第一次读取,读取数据头
207 headData := make([]byte, 8)
208
209 if _, err := io.ReadFull(this.conn, headData); err != nil {
210 fmt.Println(err)
211 return
212 }
213 pkgHead, err := this.Unpack(headData)
214 if err != nil {
215 return
216 }
217 //data
218 if pkgHead.Len > 0 {
219 pkgHead.Data = make([]byte, pkgHead.Len)
220 if _, err := io.ReadFull(this.conn, pkgHead.Data); err != nil {
221 return
222 }
223 }
224
225 //处理服务器回执业务
226 this.DoMsg(pkgHead)
227 }
228 }()
229
230 // 10s后,断开连接
231 for {
232 select {
233 case <-this.isOnline:
234 go func() {
235 for {
236 this.AIRobotAction()
237 time.Sleep(time.Second)
238 }
239 }()
240 case <-time.After(time.Second * 10):
241 _ = this.conn.Close()
242 return
243 }
244 }
245}
246
247func NewTcpClient(ip string, port int) *TcpClient {
248 addrStr := fmt.Sprintf("%s:%d", ip, port)

Callers 1

mainFunction · 0.95

Calls 4

UnpackMethod · 0.95
DoMsgMethod · 0.95
AIRobotActionMethod · 0.95
CloseMethod · 0.80

Tested by

no test coverage detected