mqtt打造消息聊天软件

mqtt打造消息聊天软件

基于mqtt打造消息聊天软件,非常简单而适用。比传统的基于tcp\websocket socket打造要省事得多。

mqtt是典型的发布(pub)\订阅(sub消息模式。

mqtt server就用现成的,无须自己打造,以Mosquitto为例

https://www.cnblogs.com/hnxxcxg/p/12393808.html

mqtt client使用tms mqtt client控件

/// <author>cxg 2021-1-30</author>

unit Unit2;

interface

uses
  TMS.MQTT.Global, TMS.MQTT.Client,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    fMsgs: TStringList;
    fclient: TTMSMQTTClient;   //既是发布者,又是订阅者
    procedure sub; //可订阅多个主题
    procedure Received(ASender: TObject; APacketID: Word; ATopic: string; APayload: TArray<System.Byte>);
    procedure ConnectedStatusChanged(ASender: TObject; const AConnected: Boolean; AStatus: TTMSMQTTConnectionStatus);
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

var topic: string = 'chat';  //主题

procedure TForm2.Button1Click(Sender: TObject);
begin
  fclient.Publish(topic, '咏南中间件:' + Edit1.Text, qosExactlyOnce, False);  //发布消息
end;

procedure TForm2.ConnectedStatusChanged(ASender: TObject;
  const AConnected: Boolean; AStatus: TTMSMQTTConnectionStatus);
begin
  case AStatus of
    csConnectionLost: ShowMessage('connection lost');
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  fMsgs := TStringList.Create;
  fclient := TTMSMQTTClient.Create(nil);
  fclient.BrokerHostName := '127.0.0.1';
  fclient.BrokerPort := 1883;
  fclient.OnConnectedStatusChanged := ConnectedStatusChanged;
  fclient.OnPublishReceived := Received;
  fclient.Connect();
  sub;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  fclient.Free;
  fMsgs.Free;
end;

procedure TForm2.Received(ASender: TObject; APacketID: Word; ATopic: string;
  APayload: TArray<System.Byte>);
begin
  fMsgs.Add(TEncoding.UTF8.GetString(APayload));
end;

procedure TForm2.sub;
begin
  fclient.Subscribe(topic, qosExactlyOnce);
  fclient.Subscribe('topic', qosExactlyOnce);
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  if fMsgs.Count > 0 then
    for var i: Integer := 0 to fMsgs.Count - 1 do
      Memo1.Lines.Add(fMsgs[i]);
    fMsgs.Clear;
end;

end.

  

消息质量说明

TTMSMQTTQoS = (

> / / / <总结 

///最多一次交付 

/ / / < /总结> 

qosAtMostOnce = 0, 

> / / / <总结 

///至少一次交付 

/ / / < /总结> 

qosAtLeastOnce = 1,

> / / / <总结 

///精确一次交货 

/ / / < /总结> 

qosExactlyOnce = 2, 

> / / / <总结 

/// Forbidden -保留 

/ / / < /总结> 

qosRESERVED = 3);

 

mqtt client 既可以是消息的发布者,又可以是消息的订阅者

同一个mqtt client可以同时订阅n个主题的消息

 

主题(topic

不论是发布消息,还是订阅消息,都必须提供主题,也就是说主题是发布消息和订阅消息的唯一依据,没有主题,便不能发布和订阅消息。

以聊天软件为例,主题就是好友的id或已加入群的id

mqtt 客户端
-能被 VCL,FMX and LCL 应用程序使用
- 支持 Windows, iOS, Android, macOS, Linux, Raspberry Pi
不安装,直接引用源码单元使用,可支持10.4

支持DELPHI,LAZARUS开发

配合MSGPACK,BSON等序列,可发送 文字、图片、文件、视频。。。

MQTT v3规范定义的允许的最大消息大小(256MB)。这已经能满足许多应用场合数据传输的要求。

posted @ 2021-01-30 12:10  delphi中间件  阅读(2225)  评论(0编辑  收藏  举报