安装nodejs
<span class="pln">yum install libtool automake autoconf gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> openssl</span><span class="pun">-</span><span class="pln">devel </span>
<span class="pln">wget https</span><span class="pun">:</span><span class="com">//nodejs.org/dist/v0.12.7/node-v0.12.7.tar.gz</span>
<span class="pln">tar zxvf node</span><span class="pun">-</span><span class="pln">v0</span><span class="pun">.</span><span class="lit">12.7</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="pln">cd node</span><span class="pun">-</span><span class="pln">v0</span><span class="pun">.</span><span class="lit">12.7</span>
<span class="pun">./</span><span class="pln">configure</span>
<span class="pln">make</span>
<span class="pln">make install</span>
检测是否安装成功
<span class="pln">node </span><span class="pun">-</span><span class="pln">v</span>
选择安装mqtt broker
这里选择mosca,在项目目录执行
<span class="pln">npm install mosca </span><span class="pun">--</span><span class="pln">save</span>
会生成目录node_modules,目录包含项目所需要的模块,里面已经有mosca模块。
服务端代码
<span class="kwd">var</span><span class="pln"> mosca </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">"mosca"</span><span class="pun">);</span>
<span class="kwd">var</span><span class="pln"> broker </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> mosca</span><span class="pun">.</span><span class="typ">Server</span><span class="pun">({});</span>
<span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">"express"</span><span class="pun">);</span>
<span class="kwd">var</span><span class="pln"> http </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">"http"</span><span class="pun">);</span>
<span class="kwd">var</span><span class="pln"> app </span><span class="pun">=</span><span class="pln"> express</span><span class="pun">()</span>
<span class="kwd">var</span><span class="pln"> srv </span><span class="pun">=</span><span class="pln"> http</span><span class="pun">.</span><span class="pln">createServer</span><span class="pun">(</span><span class="pln">app</span><span class="pun">)</span>
<span class="kwd">var</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">"path"</span><span class="pun">);</span>
<span class="pln">app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">express</span><span class="pun">.</span><span class="kwd">static</span><span class="pun">(</span><span class="pln">path</span><span class="pun">.</span><span class="pln">dirname</span><span class="pun">(</span><span class="kwd">require</span><span class="pun">.</span><span class="pln">resolve</span><span class="pun">(</span><span class="str">"mosca"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"/public"</span><span class="pun">))</span>
<span class="pln">app</span><span class="pun">.</span><span class="pln">listen</span><span class="pun">(</span><span class="lit">3000</span><span class="pun">)</span>
运行
<span class="pln">sudo npm install forever </span><span class="pun">-</span><span class="pln">g</span>
<span class="pln">forever start </span><span class="pun">-</span><span class="pln">l app</span><span class="pun">.</span><span class="pln">log </span><span class="pun">-</span><span class="pln">o </span><span class="kwd">out</span><span class="pun">.</span><span class="pln">log </span><span class="pun">-</span><span class="pln">e err</span><span class="pun">.</span><span class="pln">log </span><span class="pun">-</span><span class="pln">a mqtt</span><span class="pun">/</span><span class="pln">app</span><span class="pun">.</span><span class="pln">js</span>
客户端测试
新建index.html
<span class="tag"><html></span>
<span class="pln"> </span><span class="tag"><head></span>
<span class="pln"> </span><span class="tag"><script</span><span class="pln"> </span><span class="atn">src</span><span class="pun">=</span><span class="atv">"/mqtt.js"</span><span class="tag">></script></span>
<span class="pln"> </span><span class="tag"></head></span>
<span class="pln"> </span><span class="tag"><body></span>
<span class="pln"> </span><span class="tag"><script></span><span class="pln"> </span>
<span class="kwd">var</span><span class="pln"> client </span><span class="pun">=</span><span class="pln"> mqtt</span><span class="pun">.</span><span class="pln">connect</span><span class="pun">();</span><span class="pln"> </span>
<span class="pln">client</span><span class="pun">.</span><span class="pln">subscribe</span><span class="pun">(</span><span class="str">"mqtt/demo"</span><span class="pun">);</span><span class="pln"> </span>
<span class="pln">client</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="str">"message"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">topic</span><span class="pun">,</span><span class="pln"> payload</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span>
<span class="pln">alert</span><span class="pun">([</span><span class="pln">topic</span><span class="pun">,</span><span class="pln"> payload</span><span class="pun">].</span><span class="pln">join</span><span class="pun">(</span><span class="str">": "</span><span class="pun">));</span><span class="pln"> </span>
<span class="pln">client</span><span class="pun">.</span><span class="pln">end</span><span class="pun">();</span><span class="pln"> </span>
<span class="pun">});</span><span class="pln"> </span>
<span class="pln">client</span><span class="pun">.</span><span class="pln">publish</span><span class="pun">(</span><span class="str">"mqtt/demo"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"hello world!"</span><span class="pun">);</span><span class="pln"> </span>
<span class="pln"> </span><span class="tag"></script></span>
<span class="pln"> </span><span class="tag"></body></span>
<span class="tag"></html></span>