1. 程式人生 > >mqtt的開源實現mosquitto的交叉編譯與使用

mqtt的開源實現mosquitto的交叉編譯與使用

mqtt協議是最近比較流行的即時推送的協議
到 http://mosquitto.org/download/ 下載原始碼
或者跟我一樣我直接下git
$ git clone git://git.eclipse.org/gitroot/mosquitto/org.eclipse.mosquitto.git
$ cd  org.eclipse.mosquitto/
編譯pc平臺的
$ make WITH_SRV=no
執行
$ cp src/mosquitto mosquitto_pc
$ cp client/mosquitto_pub mosquitto_pub_pc
$ sudo make install
執行伺服器
$ mosquitto_pc -v
pc ip是 192.168.0.108

編譯arm平臺的
make clean
make WITH_SRV=no CC=arm-linux-gcc CXX=arm-linux-g++
需要
./lib/libmosquitto.so.1
./client/mosquitto_sub拷貝到開發板上
(這種方式編譯還需要libssl, libcrypto庫,請自行拷貝)

如果要指定ssl的庫位置
make WITH_SRV=no CC=arm-linux-gcc CXX=arm-linux-g++ CFLAGS="-I/thirdparty/include/ -L/thirdparty/lib" LDFLAGS="-L/thirdparty/lib -lssl -lcrypto"


在開發板上執行,訂閱hello
mosquitto_sub -v -t hello -h  192.168.0.108

pc 機中推送
mosquitto_pub -t hello -h 192.168.0.108 -m "world"
【-h】指定MQTT代理伺服器主機,指向PC機IP地址

開發板上會收到會列印:hello world

mosquitto用的是poll, 有一種epoll版本的,能支援更多的併發連線
https://bitbucket.org/wowsoso/mosquitto/src/e3a9efbbb06b6820057ecc40b91df79f0fabce93/src/loop.c?at=default