๐ซ MQTT๋?
MQTT(๋ฉ์์ง ํ์ ํ
๋ ๋ฉํธ๋ฆฌ ํธ๋์คํฌํธ, Message Queuing Telemetry Transport)๋ ISO ํ์ค(ISO/IEC PRF 20922) ๋ฐํ-๊ตฌ๋
๊ธฐ๋ฐ์ ๋ฉ์์ง ํ๋กํ ์ฝ์ด๋ค.
TCP/IP ํ๋กํ ์ฝ ์์์ ๋์ํ๋ค. "์์ ์ฝ๋ ๊ณต๊ฐ"(small code footprint)์ด ํ์ํ๊ฑฐ๋ ๋คํธ์ํฌ ๋์ญํญ์ด ์ ํ๋๋ ์๊ฒฉ ์์น์์ ์ฐ๊ฒฐ์ ์ํด ์ค๊ณ๋์ด ์๋ค. ๋ฐํ-๊ตฌ๋
๋ฉ์์ง ํจํด์ ๋ฉ์์ง ๋ธ๋ก์ปค๊ฐ ํ์ํ๋ค.
ย
๐ซ ์์ ์ค๋ช
ํด๋ผ์ด์ธํธ ํ๋ก๊ทธ๋จ ๋๊ฐ๋ฅผ ์์ฑํด์ ๊ฐ์ ํด๋ผ์ด์ธํธ์์ ๋ณด๋ด๋ ๋ฉ์ธ์ง๋ฅผ ์๋ ํด๋ผ์ด์ธํธ์ ๋จ๋๋ก ํ๋ ํ๋ก์ ํธ์ด๋ค .
ย
๐ซ ์คํ ์ ๋ค์ด๋ฐ์์ผํ๋ ๊ฒ๋ค
1. ๋ธ๋ก์ปค๋ Mosquitto๋ฅผ ๋ค์ด๋ฐ์ ์ฌ์ฉ
2. NuGet ํจํค์ง M2Mqtt
์ค์น
๐ซ Mosquitto ์์
1. cmd์ฐฝ์์ Mosquitto๊ฐ ์ค์น๋ ๊ฒฝ๋ก๋ก ์ด๋
cd C:\Program Files\mosquitto
2. ์ด๋ํ๋ค Mosquitto broker ์คํ(-v : ๋ชจ๋ ํต์ ๊ณผ์ ์ ๋ณด์ฌ์ฃผ๋ ์ต์ )
๐ซ ํด๋ผ์ด์ธํธ form ํํ
๐ซ ํด๋ผ์ด์ธํธ ์์
1. Start ๋ฒํผ์ ๋๋ฅด๋ฉด ํด๋ผ์ด์ธํธ-broker ์ฐ๊ฒฐ ์์
๐ซ ์ ์ก ๋ฐ ์ข ๋ฃ
1. ๋ฉ์ธ์ง ์ ์ก
์ ์ก๋ฒํผ์ ๋๋ฅด๋ฉด ์์ ํด๋ผ์ด์ธํธ์ ์์ ๋ฉ์ธ์ง label์ ๋ฉ์ธ์งํ์
2. ํด๋ผ์ด์ธํธ๊ฐ ์ข ๋ฃ๋์์๋
๐ซ ํด๋ผ์ด์ธํธ ์์ค ์ฝ๋
ํด๋ผ์ด์ธํธ ์์ค ์ฝ๋๋ ์์ /๋ฐ์ ํ ํฝ๋ง ๋ค๋ฆ!!
1. Client 1
using System;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace Client
{
public partial class Form1 : Form
{
private MqttClient client;
public Form1()
{
InitializeComponent();
//ํผ ๋ซ๊ธฐ ์ด๋ฒคํธ ์ ์ธ
this.FormClosed += Form1_FormClosing;
}
/// <summary>
/// ํผ ๋ซ๊ธฐ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ ์ธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
if (client != null && client.IsConnected)
{
client.Disconnect();
}
}
/// <summary>
/// ์์ ๋ฉ์ธ์ง ํ์
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// ์์ ๋ ๋ฉ์์ง๋ฅผ ๋ณ์์ ์ ์ฅ
string receivedMessage = Encoding.UTF8.GetString(e.Message);
// UI ์
๋ฐ์ดํธ๋ฅผ ์ํด UI ์ค๋ ๋๋ก ์ ๋ฌ
Invoke(new Action(() =>
{
// receivedMessage๋ฅผ Form label ReceivedText์ ํ์
ReceivedText.Text = receivedMessage;
}));
}
/// <summary>
/// start ๋ฒํผ ํด๋ฆญํ์๋ ์คํ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Startbutton_Click(object sender, EventArgs e)
{
// MQTT ๋ธ๋ก์ปค ์ฃผ์
string MQTT_BROKER_ADDRESS = "127.0.0.1";
// ํด๋ผ์ด์ธํธ ์ธ์คํด์ค ์์ฑ
client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
// ๋ฉ์์ง ์์ ์ ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ฑ๋ก
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
// ๊ณ ์ ํ ํด๋ผ์ด์ธํธ ID ์์ฑ
string clientId = "COOKIE";
try
{
client.Connect(clientId);
//์ฐ๊ฒฐ์ด ์ฑ๊ณตํ๋ฉด Serverconnect์ ์ฑ๊ณต ๋ฉ์ธ์ง์ถ๋ ฅ
if (client.IsConnected) Serverconnect.Text = "SERVER " + MQTT_BROKER_ADDRESS + "์ ์ฐ๊ฒฐ ์ฑ๊ณต!";
}
catch (Exception ex)
{
//์ฐ๊ฒฐ์ด ์คํจํ๋ฉด Serverconnect์ ์คํจ ๋ฉ์ธ์ง, ์ค๋ฅ๋ฉ์ธ์ง ์ถ๋ ฅ
Serverconnect.Text = "๋ธ๋ก์ปค์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.\n" + ex.Message + "์ฐ๊ฒฐ ์ค๋ฅ";
}
// ์์ ํ ํฝ "coorong" ๊ตฌ๋
client.Subscribe(new string[] { "coorong" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
/// <summary>
/// Send๋ฒํผ ํด๋ฆญํ์๋ ์คํ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sendbutton_Click(object sender, EventArgs e)
{
// ๋ณด๋ผ ๋ฉ์์ง
string messageToSend = SendText.Text;
// ๋ฐ์ ํ ํฝ "cookie", ๋ฉ์ธ์ง ๋ฐ์ก
client.Publish("cookie", Encoding.UTF8.GetBytes(messageToSend), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
//๋ฉ์ธ์ง๋ฐ์ค ์ด๊ธฐํ
SendText.Text = "";
}
}
}
ย
2. Client 2
using System;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace Client
{
public partial class Form1 : Form
{
private MqttClient client;
public Form1()
{
InitializeComponent();
//ํผ ๋ซ๊ธฐ ์ด๋ฒคํธ ์ ์ธ
this.FormClosed += Form1_FormClosing;
}
/// <summary>
/// ํผ ๋ซ๊ธฐ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ ์ธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
if (client != null && client.IsConnected)
{
client.Disconnect();
}
}
/// <summary>
/// ์์ ๋ฉ์ธ์ง ํ์
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// ์์ ๋ ๋ฉ์์ง๋ฅผ ๋ณ์์ ์ ์ฅ
string receivedMessage = Encoding.UTF8.GetString(e.Message);
// UI ์
๋ฐ์ดํธ๋ฅผ ์ํด UI ์ค๋ ๋๋ก ์ ๋ฌ
Invoke(new Action(() =>
{
// receivedMessage๋ฅผ Form label ReceivedText์ ํ์
ReceivedText.Text = receivedMessage;
}));
}
/// <summary>
/// start ๋ฒํผ ํด๋ฆญํ์๋ ์คํ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Startbutton_Click(object sender, EventArgs e)
{
// MQTT ๋ธ๋ก์ปค ์ฃผ์
string MQTT_BROKER_ADDRESS = "127.0.0.1";
// ํด๋ผ์ด์ธํธ ์ธ์คํด์ค ์์ฑ
client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
// ๋ฉ์์ง ์์ ์ ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ฑ๋ก
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
// ๊ณ ์ ํ ํด๋ผ์ด์ธํธ ID ์์ฑ
string clientId = "COORONG";
try
{
client.Connect(clientId);
//์ฐ๊ฒฐ์ด ์ฑ๊ณตํ๋ฉด Serverconnect์ ์ฑ๊ณต ๋ฉ์ธ์ง์ถ๋ ฅ
if (client.IsConnected) Serverconnect.Text = "SERVER " + MQTT_BROKER_ADDRESS + "์ ์ฐ๊ฒฐ ์ฑ๊ณต!";
}
catch (Exception ex)
{
//์ฐ๊ฒฐ์ด ์คํจํ๋ฉด Serverconnect์ ์คํจ ๋ฉ์ธ์ง, ์ค๋ฅ๋ฉ์ธ์ง ์ถ๋ ฅ
Serverconnect.Text = "๋ธ๋ก์ปค์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.\n" + ex.Message + "์ฐ๊ฒฐ ์ค๋ฅ";
}
// ์์ ํ ํฝ "cookie" ๊ตฌ๋
client.Subscribe(new string[] { "cookie" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
/// <summary>
/// Send๋ฒํผ ํด๋ฆญํ์๋ ์คํ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sendbutton_Click(object sender, EventArgs e)
{
// ๋ณด๋ผ ๋ฉ์์ง
string messageToSend = SendText.Text;
// ๋ฐ์ ํ ํฝ "coorong", ๋ฉ์ธ์ง ๋ฐ์ก
client.Publish("coorong", Encoding.UTF8.GetBytes(messageToSend), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
//๋ฉ์ธ์ง๋ฐ์ค ์ด๊ธฐํ
SendText.Text = "";
}
}
}
ย