引言
這段時間團隊需要跟 Google Glass 進行交互,因此要做一個推送機制。而 MQTT 協(xié)議的推送是當今最火熱的一個。
Mosquitto 則是實現了 MQTT 協(xié)議的服務。
安裝
在 Ubuntu 下:
1
2
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
如果顯示apt-add-repository沒有識別,則可以:
1
sudo apt-get install python-software-properties
配置
安裝完成后,所有配置都會在 /etc/mosquitto目錄下。其中最重要的則是mosquitto.conf 文件,以下則是配置文件內容。
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# =================================================================
# General configuration
# =================================================================
# 客戶端心跳的間隔時間
#retry_interval 20
# 系統(tǒng)狀態(tài)的刷新時間
#sys_interval 10
# 系統(tǒng)資源的回收時間,0表示盡快處理
#store_clean_interval 10
# 服務進程的PID
#pid_file /var/run/mosquitto.pid
# 服務進程的系統(tǒng)用戶
#user mosquitto
# 客戶端心跳消息的最大并發(fā)數
#max_inflight_messages 10
# 客戶端心跳消息緩存隊列
#max_queued_messages 100
# 用于設置客戶端長連接的過期時間,默認永不過期
#persistent_client_expiration
# =================================================================
# Default listener
# =================================================================
# 服務綁定的IP地址
#bind_address
# 服務綁定的端口號
#port 1883
# 允許的最大連接數,-1表示沒有限制
#max_connections -1
# cafile:CA證書文件
# capath:CA證書目錄
# certfile:PEM證書文件
# keyfile:PEM密鑰文件
#cafile
#capath
#certfile
#keyfile
# 必須提供證書以保證數據安全性
#require_certificate false
# 若require_certificate值為true,use_identity_as_username也必須為true
#use_identity_as_username false
# 啟用PSK(Pre-shared-key)支持
#psk_hint
# SSL/TSL加密算法,可以使用“openssl ciphers”命令獲取
# as the output of that command.
#ciphers
# =================================================================
# Persistence
# =================================================================
# 消息自動保存的間隔時間
#autosave_interval 1800
# 消息自動保存功能的開關
#autosave_on_changes false
# 持久化功能的開關
persistence true
# 持久化DB文件
#persistence_file mosquitto.db
# 持久化DB文件目錄
#persistence_location /var/lib/mosquitto/
# =================================================================
# Logging
# =================================================================
# 4種日志模式:stdout、stderr、syslog、topic
# none 則表示不記日志,此配置可以提升些許性能
log_dest none
#還有一種寫入文件的模式
log_dest file '/var/lib/mosquitto/mosquitto.log'
# 選擇日志的級別(可設置多項)
#log_type error
#log_type warning
#log_type notice
#log_type information
#log_type all
# 是否記錄客戶端連接信息
#connection_messages true
# 是否記錄日志時間
#log_timestamp true
# =================================================================
# Security
# =================================================================
# 客戶端ID的前綴限制,可用于保證安全性
#clientid_prefixes
# 允許匿名用戶
#allow_anonymous true
# 用戶/密碼文件,默認格式:username:password
#password_file
# PSK格式密碼文件,默認格式:identity:key
#psk_file
# pattern write sensor/%u/data
# ACL權限配置,常用語法如下:
# 用戶限制:user <username>
# 話題限制:topic [read|write] <topic>
# 正則限制:pattern write sensor/%u/data
#acl_file
# =================================================================
# Bridges
# =================================================================
# 允許服務之間使用“橋接”模式(可用于分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# 設置橋接的客戶端ID
#clientid
# 橋接斷開時,是否清除遠程服務器中的消息
#cleansession false
# 是否發(fā)布橋接的狀態(tài)信息
#notifications true
# 設置橋接模式下,消息將會發(fā)布到的話題地址
# $SYS/broker/connection/<clientid>/state
#notification_topic
# 設置橋接的keepalive數值
#keepalive_interval 60
# 橋接模式,目前有三種:automatic、lazy、once
#start_type automatic
# 橋接模式automatic的超時時間
#restart_timeout 30
# 橋接模式lazy的超時時間
#idle_timeout 60
# 橋接客戶端的用戶名
#username
# 橋接客戶端的密碼
#password
# bridge_cafile:橋接客戶端的CA證書文件
# bridge_capath:橋接客戶端的CA證書目錄
# bridge_certfile:橋接客戶端的PEM證書文件
# bridge_keyfile:橋接客戶端的PEM密鑰文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile
# 自己的配置可以放到以下目錄中
include_dir /etc/mosquitto/conf.d
權限
為了避免任何人都可以往服務器去 pull&push,我們需要設置權限。
使用自帶工具進行設置賬號:
1
2
3
4
5
#add a count and then will ask you to enter a passwd
mosquitto_passwd -c /etc/mosquitto/passwd glassx
# delete
mosquitto_passwd -D /etc/mosquitto/passwd glassx
啟動
啟動服務很簡單,直接運行:
1
2
3
4
5
6
mosquitto -c /etc/mosquitto/mosquitto.conf -d
#-c 表示加載指定配置文件
#-d 表示以后臺服務運行
#-p 表示監(jiān)聽某個端口(默認為1883)
#-v 表示亢長的日志記錄模式,相當于設置 log_type all
mosquitto 是個異步 IO 框架,經過測試可以處理 20000 個以上的客戶端連接。
推送
上面所創(chuàng)建的只是服務器, MQTT 有三個角色, 發(fā)布角色,服務器角色,消費角色。 此時我們便使用 Ruby 來實現發(fā)布角色。
我們會使用到一個叫做 mqtt 的 gem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'rubygems'
require 'mqtt'
# Publish example
MQTT::Client.connect('mqtt://glassx:glassxpw@127.0.0.1') do |c|
p c.publish('topic', 'message')
end
# # Subscribe example
# MQTT::Client.connect('test.') do |c|
# # If you pass a block to the get method, then it will loop
# c.get('test') do |topic,message|
# puts "#{topic}: #{message}"
# end
# end
接收
接收則是各個客戶端的實現了,這里就不寫了。
引用
官網:http:///
簡要教程:http://blog.csdn.net/shagoo/article/details/7910598