delphi7 使用原生控件clientsocket 实现 阿里云三元组 连接阿里云mqtt
procedure TFmqtt.Button1Click(Sender: TObject);
var
ipname:string;
clientId:string;
t:string;
signmethod:string;
passbegin:string;
back:PAnsiChar;
code:integer;
begin
ProductKey:=trim(self.Edit1.Text);
DeviceName:=trim(self.Edit2.Text);
DeviceSecret:=trim(self.Edit3.Text);
ipname:=ProductKey+’.iot-as-mqtt.cn-shanghai.aliyuncs.com’;
serverip:=GetIPFromName(ipname);
self.Memo1.Lines.Add(‘serverip=’+serverip);
clientId:=self.IdIPWatch1.LocalIP;
t:=inttostr((DateTimeToUnix(now)-86060)*1000);
if self.RadioGroup1.ItemIndex=0 then
begin
signmethod:=‘hmacmd5’;
end else
begin
signmethod:=‘hmacsha1’;
end;
mqttUserName:= DeviceName + ‘&’ + ProductKey;
passbegin:=‘clientId’+clientId+‘deviceName’+deviceName+‘productKey’+productKey+‘timestamp’+t;
self.Memo1.Lines.Add(‘passbegin=’+passbegin);
if self.RadioGroup1.ItemIndex=0 then
begin
code:=hmacmd5(PAnsiChar(AnsiString(DeviceSecret)),PAnsiChar(AnsiString(passbegin)),back);
if code=0 then
begin
mqttPassword:=back;
self.Memo1.Lines.Add(‘mqttPassword=’+mqttPassword);
end;
end else
begin
code:=hmacsha1(PAnsiChar(AnsiString(DeviceSecret)),PAnsiChar(AnsiString(passbegin)),back);
if code=0 then
begin
mqttPassword:=back;
self.Memo1.Lines.Add(‘mqttPassword=’+mqttPassword);
end;
end;
mqttClientId := clientId + ‘|securemode=3,signmethod=’ + signmethod + ‘,timestamp=’ + t + ‘|’;
if self.RadioButton2.Checked then
begin
PubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/update’;
SubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/get’;
end else
begin
PubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/user/update’;
SubTopic := ‘/’ + ProductKey + ‘/’ + DeviceName + ‘/user/get’;
end;
rrpcTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/request/+’;
rrpcrecTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/request/’;
rrpcsendTopic:=’/sys/’+ProductKey+’/’+DeviceName+’/rrpc/response/’;
ClientSocket1.Active:=False;
ClientSocket1.Address:=serverip;
ClientSocket1.Port:=1883;
ClientSocket1.Active:=True; //526887369
end;