def connect(self, host, port=1883, keepalive=60): self.host = host self.port = port self.bad_connect_flag = False self.connected_flag = False self.keepalive = keepalive self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = self.cleansession connect.KeepAliveTimer = self.keepalive self.sock.send(connect.pack().encode())
def connect(self, cleansession=True): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.settimeout(5.0) self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = cleansession connect.KeepAliveTimer = 0 self.sock.send(connect.pack()) response, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.sock)) assert response.mh.MsgType == MQTTSN.CONNACK self.startReceiver()