nodejs+mqtt协议的聊天室

/ 0评 / 0

安装nodejs

  1. <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>
  2. <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>
  3. <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>
  4. <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>
  5. <span class="pun">./</span><span class="pln">configure</span>
  6. <span class="pln">make</span>
  7. <span class="pln">make install</span>

检测是否安装成功

  1. <span class="pln">node </span><span class="pun">-</span><span class="pln">v</span>

选择安装mqtt broker

这里选择mosca,在项目目录执行

  1. <span class="pln">npm install mosca </span><span class="pun">--</span><span class="pln">save</span>

会生成目录node_modules,目录包含项目所需要的模块,里面已经有mosca模块。

服务端代码

  1. <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>
  2. <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>
  3. <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>
  4. <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>
  5. <span class="kwd">var</span><span class="pln"> app </span><span class="pun">=</span><span class="pln"> express</span><span class="pun">()</span>
  6. <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>
  7. <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>
  8. <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>
  9. <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>

运行

  1. <span class="pln">sudo npm install forever </span><span class="pun">-</span><span class="pln">g</span>
  2. <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

  1. <span class="tag"><html></span>
  2. <span class="pln"> </span><span class="tag"><head></span>
  3. <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>
  4. <span class="pln"> </span><span class="tag"></head></span>
  5. <span class="pln"> </span><span class="tag"><body></span>
  6. <span class="pln"> </span><span class="tag"><script></span><span class="pln"> </span>
  7. <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>
  8. <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>
  9. <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>
  10. <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>
  11. <span class="pln">client</span><span class="pun">.</span><span class="pln">end</span><span class="pun">();</span><span class="pln"> </span>
  12. <span class="pun">});</span><span class="pln"> </span>
  13. <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>
  14. <span class="pln"> </span><span class="tag"></script></span>
  15. <span class="pln"> </span><span class="tag"></body></span>
  16. <span class="tag"></html></span>
来自为知笔记(Wiz)