| | |
| | | Writer *bufio.Writer |
| | | // 写入通道 |
| | | writeChan chan []byte |
| | | // 退出通道 |
| | | exitChan chan int8 |
| | | // 连接状态 |
| | | state State |
| | | // 报文头 |
| | |
| | | c.readLock = new(sync.Mutex) |
| | | c.writeLock = new(sync.Mutex) |
| | | c.writeChan = make(chan []byte) |
| | | c.exitChan = make(chan int8) |
| | | c.state = StateInit |
| | | c.tmpByte4Slice = make([]byte, 4) |
| | | c.waitGroup = &util.WaitGroupWrapper{} |
| | |
| | | c.Logger.Debug("registering...") |
| | | data := c.deviceRegister |
| | | msgData, _ := json.Marshal(data) |
| | | _, err := c.WriteMsg(c.deviceId, aiot.MSG_TYPE_REGISTER, aiot.REQ_TYPE_REQUEST, msgData, c.GetMsgProto("")) |
| | | _, err := c.WriteMsg(c.deviceId, aiot.MSG_TYPE_REGISTER, aiot.REQ_TYPE_REQUEST, msgData, GetMsgProto("")) |
| | | if err != nil { |
| | | c.Logger.Error("Fail to send device register", zap.Any("msg", msgData)) |
| | | } |
| | |
| | | t.Stop() |
| | | return |
| | | } |
| | | go c.WriteMsg(c.deviceId, aiot.MSG_TYPE_HEART_BEAT, aiot.REQ_TYPE_REQUEST, pingData, c.GetMsgProto("")) |
| | | go c.WriteMsg(c.deviceId, aiot.MSG_TYPE_HEART_BEAT, aiot.REQ_TYPE_REQUEST, pingData, GetMsgProto("")) |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | // 拼装消息ID |
| | | func (c *Client) GetMsgProto(msgId string) *aiot.MsgIdProto { |
| | | func GetMsgProto(msgId string) *aiot.MsgIdProto { |
| | | // 新消息 |
| | | if msgId == "" { |
| | | return &aiot.MsgIdProto{ |
| | |
| | | if c.IsConnected() { |
| | | c.clientCallback.OnClose(c) |
| | | } |
| | | close(c.writeChan) |
| | | } |
| | | |
| | | // 设置连接属性 |