emqtt 是採用erlang語言開發,全面支援mqtt v3.1.1協議,支援集群和大規模連線的開源mqtt訊息伺服器,主要應用於物聯網,移動網際網路的訊息服務。在python語言中可以使用第三方庫nyamuk提供的api實現emqtt的訪問連線,實現訂閱/發布訊息佇列服務(目前該第三方庫只支援python2 版本)。
3. 進入目錄,有以下命令可以啟動服務(linux 下使用/
,windows下使用\
)
.\bin
\emqttd console # 控制台啟動emqtt服務
.\bin
\emqttd start # 守護程序啟動emqtt服務
import sys
from nyamuk import *
# nloop 函式:傳送發布訊息→讀取訂閱訊息→佇列形式返回乙個訊息
defnloop
(client):
client.packet_write() # flush write buffer (messages sent to mqtt server)
client.loop() # fill read buffer (enqueue received messages)
return client.pop_event() # return 1st received message (dequeued)
client = nyamuk("test_nyamuk", server="localhost")
# nyamuk:
# def __init__(self, client_id, username = none, password = none,
# server = "localhost", port = none, keepalive = nc.keepalive_val,
# log_level = logging.debug,
# ssl = false, ssl_opts=):
ret = client.connect(version=3)
ret = nloop(client) # ret should be eventconnack object
# 連線確認
ifnot isinstance(ret, eventconnack) or ret.ret_code != 0:
print ('connection failed'; sys.exit(1))
# 訂閱主題
client.subscribe('foo/bar', qos=1)
ret = nloop(client)
# 訂閱主題確認
ifnot isinstance(ret, eventsuback):
print
'suback not received'; sys.exit(2)
print
'granted qos is', ret.granted_qos[0]
# 開始偵聽訊息
try:
while
true:
evt = nloop(client)
if isinstance(evt, eventpublish):
print
'we received a message: (topic= )'.format(evt.msg.payload, evt.msg.topic)
# received message is either qos 0 or 1
# in case of qos 1, we must send back puback message with same packet-id
if evt.msg.qos == 1:
client.puback(evt.msg.mid)
except keyboardinterrupt:
pass
client.disconnect()
import sys
from nyamuk import *
defnloop
(client):
client.packet_write() # flush write buffer (messages sent to mqtt server)
client.loop() # fill read buffer (enqueue received messages)
return client.pop_event() # return 1st received message (dequeued)
client = nyamuk("test_nyamuk", server="localhost")
ret = client.connect(version=3)
ret = nloop(client) # ret should be eventconnack object
ifnot isinstance(ret, eventconnack) or ret.ret_code != 0:
print
'connection failed'; sys.exit(1)
client.publish('foo/bar', 'this is a test', qos=1)
ret = nloop(client) # ret should be eventpuback
client.disconnect()
2017-04-25
08:26:37,111 - test_nyamuk - info - connecting to server ....localhost # 發起連線中
2017-04-25
08:26:37,141 - test_nyamuk - info - connack reveived # 確認連線
2017-04-25
08:26:37,142 - test_nyamuk - info - subscribe: foo/bar # 訂閱主題foo/bar
2017-04-25
08:26:37,144 - test_nyamuk - info - suback received # 訂閱成功確認
granted qos is
1# qos=0最多一次;qos=1最少一次;qos=2只有一次的傳輸
再開乙個命令列視窗2執行發布端指令碼: python publish.py
2017-04-25 08:30
:42,881 -
clientid
-info
-connecting
toserver ....localhost
2017-04-25 08:30
:42,891 -
clientid
-info
-connack
reveived
2017-04-25 08:30
:42,892 -
clientid
-debug
-send
publish # 發布主題+訊息
2017-04-25 08:30
:42,894 -
clientid
-info
-puback
received # 發布成功確認
2017-04-25 08:30
:42,894 -
clientid
-info
-disconnect # 斷開連線
視窗1列印訂閱訊息:
2017-04-25
08:27:54,075 - test_nyamuk - debug - publish received
we received a message: this is a test (topic= foo/bar)
2017-04-25
08:27:54,075 - test_nyamuk - debug - received publish(dup = 0,qos=1,retain=0
2017-04-25
08:27:54,075 - test_nyamuk - debug - mid=1, topic=foo/bar, payloadlen=14
2017-04-25
08:27:54,075 - test_nyamuk - info - send puback (msgid=1)
此外視窗1還有2分鐘一次的心跳機制:
2017-04-25 08:34
:42,986 -
test_nyamuk
-debug
-send
pingreq
2017-04-25 08:34
:42,987 -
test_nyamuk
-debug
-pingresp
received
2017-04-25 08:36
:43,033 -
test_nyamuk
-debug
-send
pingreq
2017-04-25 08:36
:43,036 -
test_nyamuk
-debug
-pingresp
received
至此,確認ok啦~ CentOS下搭建Emqtt伺服器(emqx)
簡介 我把壓縮包放到 emqtt目錄下。那麼這個軟體的絕對路徑是 root mqtt emqx 首先進入 emqtt 目錄 需先建立emqtt目錄 上傳並解壓檔案 cd emqtt unzip emqx centos7 v3.1.1.zip 為了方便使用bin目錄下的命令,我們配置一下emqtt的環...
emqtt伺服器開啟日誌
官方文件介紹 設定日誌輸出到檔案中,日誌級別debug etc emq.conf console log.enum off,file,console,both log.console file console log level.enum debug,info,notice,warning,erro...
emqtt伺服器重啟配置不生效
按官方文件介紹的 重啟emttt服務的方法 是 bin目錄下 emqttd restart 但是我改了 etc 下的 emq.conf 然後按上面方法重啟emqtt 修改的配置並沒有生效 關閉emqtt服務 emqttd stop 然後 ps aux grep emq 可以看到 還有個emqtt 守...