broker.local # apt-get install mosquitto
broker.local # netstat -nap | grep mosquitto tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 1512/mosquitto tcp6 0 0 :::1883 :::* LISTEN 1512/mosquitto
# apt-get install mosquitto-clients
# mosquitto_sub -d -t hello/world Received CONNACK Received SUBACK Subscribed (mid: 1): 0
# mosquitto_pub -d -t hello/world -m "Hello World" Received CONNACK Sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (11 bytes))Sub 클라이언트는 "Hello World"를 전송 받는다.
# gem install mqtt지원하는 메시지 타입이다.
require 'rubygems'
require 'mqtt'
require 'readline'
# nickname 설정
nickname=ARGV[0]
# localhost에서 테스트 했다.
mqtt = MQTT::Client.new('localhost')
mqtt.connect do |client|
client.subscribe('chat/public')
client.subscribe("chat/private/#{nickname}")
enter_msg = "#{nickname} enter room!!"
client.publish 'chat/public', enter_msg
# Publish Thread
Thread.new do
while message = Readline.readline("", true)
case message
when /^\/priv\s*(\w*)\s*(.*)/ # 개인 메시지
client.publish "chat/private/#{$1}", "<#{nickname}> : #{$2}"
when /^\/quit\s*(.*)/ # 나가기
client.publish 'chat/public', "#{nickname} has quit (#{$1})"
exit 1
else # 공개 메시지
client.publish 'chat/public', "#{nickname} : #{message}"
end
end
end
loop do
topic, message = client.get
print message,"\n"
end
end
| Mnemonic | Enumeration | Description |
| Reserved | 0 | Reserved |
| CONNECT | 1 | Client request to connect to Server |
| CONNACK | 2 | Connect Acknowledgment |
| PUBLISH | 3 | Publish message |
| PUBACK | 4 | Publish Acknoledgment |
| PUBREC | 5 | Publish Received (assured delivery part 1) |
| PUBREL | 6 | Publish Received (assured delivery part 2) |
| PUBCOMP | 7 | Publish Complete (assured delivery part 3) |
| SUBSCRIBE | 8 | Client Subscribe request |
| SUBACK | 9 | Subscribe Acknoledgment |
| UNSUBSCRIBE | 10 | Unsubscribe Acknoledgment |
| UNSUBACK | 11 | Unsubscribe Acknoledgment |
| PINGREQ | 12 | PING Request |
| PINGRESP | 13 | PING Response |
| DISCONNECT | 14 | Client is Disconnecting |
| Reserved | 15 | Reserved |
| Bit position | Name | Description |
| 3 | DUP | Duplicate delivery |
| 2-1 | QoS | Quality of Service |
| 0 | RETAIN | RETAIN flag |
| QoS value | bit 2 | bit 1 | Description | ||
| 0 | 0 | 0 | 최대 한번 | Fire and Forget | <=1 |
| 1 | 0 | 1 | 적어도 한번 | Acknowledged delivery | >=1 |
| 2 | 1 | 0 | 정확히 한번 | Fire and Forget | <=1 |
| 3 | 1 | 1 | 최대한번 | Fire and Forget | <=1 |
| 4 | 1 | 1 | Reserved | ||