()
| 199 | } |
| 200 | |
| 201 | func (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 | |
| 247 | func NewTcpClient(ip string, port int) *TcpClient { |
| 248 | addrStr := fmt.Sprintf("%s:%d", ip, port) |
no test coverage detected