๐Ÿ’ซ 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 : ๋ชจ๋“  ํ†ต์‹ ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ต์…˜)

mosquitto -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 = "";
        }
    }
}

ย 

By Dozzing

Related Post

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค