Building MosQuitto server in Windows environment
The construction of MosQuitto server
Building a server in Windows environment
Reference article: [Mobile] Mosquitto Brief Tutorial (Install/Use/Test)
Download and install MosQuitto server
First, in the DownLoad interface, download the server source code, one is to download it according to the source code, and then compile it locally through Cygwin Source
, and the other is to directly download the compiled one provided by the official website Binary Installation
.
When I used the first source code installation, I encountered a problem, which has not been solved for the time being after installing openssl.
Therefore, this article adopts the second method. According to the prompt of the readme, if it is missing libeay32.dll,ssleay32.dll,pthreadVC2.dll
, you can install the corresponding environment according to the prompt or download the corresponding dll file directly. The test of the individual dll file downloaded by the author was successful.
Dependencies - win32
--------------------
* OpenSSL
Link: http://slproweb.com/products/Win32OpenSSL.html
Install "Win32 OpenSSL <version>"
Required DLLs: libeay32.dll ssleay32.dll
* pthreads
Link: ftp://sourceware.org/pub/pthreads-win32
Install "pthreads-w32-<version>-release.zip
Required DLLs: pthreadVC2.dll
Please ensure that the required DLLs are on the system path, or are in the same directory as
the mosquitto executable.
Dependencies - Cygwin
---------------------
* OpenSSL
Link: http://slproweb.com/products/Win32OpenSSL.html
Install "Win32 OpenSSL <version>"
* pthreads
Link: ftp://sourceware.org/pub/pthreads-win32
Install "pthreads-w32-<version>-release.zip
* Cygwin
Link: https://www.cygwin.com/setup-x86.exe
Required packages: libgcc1, openssl, zlib0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
After the installation is complete, it will look like the following picture:
Some of the main functions are as follows:
module | Function |
---|---|
mosquitto.conf | Configuration file for mosquitto server |
mosquito | Used to start the broker |
mosquitto_passwd | Used to set the user's account and password |
mosquitto_pub | to publish messages |
mosquitto_sub | to subscribe to news |
pwfile.example | The file that stores the user's account password through mosquitto_passwd |
test server
Creating a Broker, subscribing, and publishing messages can be done through the command line. Let's first test whether the server can run normally:
- Start the server
mosquitto -c mosquitto.conf
and run it through the command line. If there is no other prompt, it means success. At this time, we can publish and subscribe to the message to conduct the following test.
- Subscribe and Publish
- 通过
mosquitto_sub -v -t {topic}
来订阅Topic,其中{topic}
就是订阅的主题。 - 通过
mosquitto_pub -t {topic} -m {message}
来发布消息,其中{topic}
即发布的主题,{message}
即要发布的信息。 - 演示Gif:可以看到我们测试中,接收
topic
的主题,当发布之后成功的接收到了订阅的主题。
- 通过
补充功能
设置用户的账号与密码:
mosquitto_passwd [ -c | -D ] passwordfile username
-c
表示创建一个新的用户并写入到一个文件中。创建成功之后,我们打开该文件,可以看到里面保存了我们刚刚创建的用户的账号,而密码则帮我们进行了加密。
创建之后,不要忘记修改
mosquitto.conf
中的password_file pwfile.example
, 将其修改成我们刚刚创建的passwordfile使其生效。
总结与预告
本次成功的在Windows环境下部署了Mosquitto服务器,下篇文章将演示如果在手机客户端上接受到推送的信息。