Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Windows\system32>c:
C:\Windows\system32>cd..
C:\Windows>cd..
C:\>cd emqx
C:\emqx>cd bin
C:\emqx\bin>dir
驱动器 C 中的卷没有标签。
卷的序列号是 DA83-2A7F
C:\emqx\bin 的目录
2019/11/05 20:10 <DIR> .
2019/11/05 20:10 <DIR> ..
2019/08/04 12:59 304,973 cuttlefish
2019/08/04 12:53 19,192 emqx
2019/08/04 12:53 7,563 emqx.cmd
2019/08/04 12:53 2,416 emqx_ctl
2019/08/04 12:53 2,489 emqx_ctl.cmd
2019/08/04 12:53 468 emqx_env
2019/08/04 12:53 6,203 install_upgrade_escript
2019/08/04 12:53 12,433 nodetool
2019/08/04 13:01 30,360 no_dot_erlang.boot
2019/08/04 13:01 30,360 start_clean.boot
10 个文件 416,457 字节
2 个目录 47,299,125,248 可用字节
C:\emqx\bin>emqx
usage: emqx (install|uninstall|start|stop|restart|console|ping|list|attach)
C:\emqx\bin>emqx start
C:\emqx>emqx_ctl status
'emqx_ctl' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:\emqx>cd bin
C:\emqx\bin>emqx_ctl status
Node 'emqx@127.0.0.1' is started
emqx 3.2.2 is running
C:\emqx\bin>cd..
C:\emqx>cd
C:\emqx
C:\emqx>cd..
C:\>ipconfig
Windows IP 配置
以太网适配器 本地连接:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::fd2e:705d:5b79:8b4e%11
IPv4 地址 . . . . . . . . . . . . : 192.168.221.58
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.221.254
隧道适配器 isatap.{86AE2A60-5600-4E8E-918B-CA4D01D021E6}:
媒体状态 . . . . . . . . . . . . : 媒体已断开
连接特定的 DNS 后缀 . . . . . . . :
C:\>
网址http://localhost:18083/#/login?redirect=%2F
android:架包app libs
public class MainActivity extends AppCompatActivity {
String host = "tcp://192.168.221.58:1883";
String userName = "admin";
String userPwd = "public";
MqttClient client;
MqttConnectOptions options;
String topic = "a/b/c";
ScheduledExecutorService scheduledExecutorService;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
handler=new Handler()
{
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
if(msg.what==0x01)
{
Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
}
if(msg.what==0x02)
{
Toast.makeText(MainActivity.this,"连接成功!",Toast.LENGTH_SHORT).show();
try {
client.subscribe(topic,1);
} catch (MqttException e) {
e.printStackTrace();
}
}
if(msg.what==0x03)
{
Toast.makeText(MainActivity.this,"连接失败!",Toast.LENGTH_SHORT).show();
}
}
};
startReconnect();
}
public void init() {
try {
client = new MqttClient(host, "test01", new MemoryPersistence());
options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(userName);
options.setPassword(userPwd.toCharArray());
options.setConnectionTimeout(10);
options.setKeepAliveInterval(20);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
Message msg=new Message();
msg.what=0x01;
msg.obj=s+"--"+ mqttMessage.toString();
handler.sendMessage(msg);
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
public void connect()
{
new Thread(){
@Override
public void run(){
try{
client.connect(options);
handler.sendEmptyMessage(0x02);
}catch(MqttException e){
e.printStackTrace();
handler.sendEmptyMessage(0x03);
}
}
}.start();
}
public void startReconnect()
{
scheduledExecutorService= Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if(!client.isConnected())
{
connect();
}
}
},1*1000,10*1000, TimeUnit.MILLISECONDS);
}
}
C#加载库M2M
Form1.Designer.cs
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.txtReceiveMsg = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(109, 81);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// txtReceiveMsg
//
this.txtReceiveMsg.Location = new System.Drawing.Point(65, 38);
this.txtReceiveMsg.Name = "txtReceiveMsg";
this.txtReceiveMsg.Size = new System.Drawing.Size(129, 21);
this.txtReceiveMsg.TabIndex = 1;
this.txtReceiveMsg.TextChanged += new System.EventHandler(this.txtReceiveMsg_TextChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.txtReceiveMsg);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtReceiveMsg;
}
}
Form1.cs:
namespace MQTT
{
public partial class Form1 : Form
{
string enpoint = "192.168.221.55";
int port = 1883;
string user = "admin";
string pwd = "public";
string clientid = Guid.NewGuid().ToString();
MqttClient client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
client = new MqttClient(enpoint, port, false, MqttSslProtocols.None, null, null);
client.Connect(clientid, user, pwd);
if (client.IsConnected)
{
MessageBox.Show("连接成功");
client.Subscribe(new string[] { "as" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ShowMsg(string msg)
{
txtReceiveMsg.AppendText(msg);
}
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Action<string> showAct = new Action<string>(ShowMsg);
Invoke(showAct, new string[] { Encoding.UTF8.GetString(e.Message) });
//Invoke(new Action(() =>
// {
// button1.AppendText(Encoding.UTF8.GetString(e.Message));
// }
// ));
}
private void txtReceiveMsg_TextChanged(object sender, EventArgs e)
{
}
}
}
Text 命名txtReceiveMsg
button