mqtt server 5分钟入门

mqtt server android python client

比较熟悉的paho的android实现,最近要写到想写一个小服务器,想着拉下mqtt server来参考一下

所以这个是 step by step 5分钟入门性质的文章

mac mqtt server

拉代码下来到编译执行

git clone https://github.com/eclipse/mosquitto.git
cd mosquitto
cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2k  -DOPENSSL_LIBRARIES=/usr/local/Cellar/openssl/1.0.2k/lib .
make
cd src
./mosquitto

mac python client

virtualenv paho-mqtt
source paho-mqtt/bin/activate
pip install paho-mqtt
vim test.py
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("$SYS/#")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
python test.py

看到服务器端输出

1504871184: mosquitto version 1.4.14 (build date 2017-09-08 19:43:48+0800) starting
1504871184: Using default config.
1504871184: Opening ipv6 listen socket on port 1883.
1504871184: Opening ipv4 listen socket on port 1883.

1504872475: New connection from 127.0.0.1 on port 1883.
1504872475: New client connected from 127.0.0.1 as J4ew]DJ7S2aOvLF@j3i?o3MsKRilCH`ci3TWeWuVAYr:E[J23@od2j[gi4tFxpmR (c1, k60).

然后接下来就是看代码了。

哦,有遇到一点问题

cmake .

CMake Error at /usr/local/Cellar/cmake/3.9.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.9.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:377 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.9.2/share/cmake/Modules/FindOpenSSL.cmake:388 (find_package_handle_standard_args)
  CMakeLists.txt:61 (find_package)


-- Configuring incomplete, errors occurred!
See also "/Users/yeshen/Documents/opensource/mosquitto/CMakeFiles/CMakeOutput.log".

solution

cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2k \
 -DOPENSSL_LIBRARIES=/usr/local/Cellar/openssl/1.0.2k/lib .

你可能感兴趣的:(other)