利用Node.js對某智能家居伺服器重構

來源:互聯網
上載者:User

利用Node.js對某智能家居伺服器重構
  

 

之前負責過一個智能家居項目的開發,外包重慶一家公司的,我們主要程式開發伺服器監控和叢集版管理。

移動端和機頂盒的遠程通訊是用中間伺服器完成互動,伺服器使用MINA NIO架構,非阻塞式的,可以看看以前部落格瞭解下某智能家居項目架構學習總結,或者其他資料JAVA NIO原理,基於MINA架構快速開發網路應用程式。

在移動端或者機頂盒登入後會使用spring security 進行加密,主要是結合使用者名稱和密碼來加密,產生一個唯一標示符。伺服器來到一個請求時會檢查對應的標示符來發送相關約定好的命令,如登入到移動端向伺服器發送命名,伺服器會產生如522f9e2a459de81d6a9e9eadfa9468d1的標示符,如果在機頂盒集合裡也存在相應標示符的主控,則給他發送。

最近關注Node.js,這不就是Node的特性NIO嗎?

讓我們來著手重構一下,利用Node.js的先天優勢,高並發,非阻塞式

首先對串連封裝一下

var MyClient = function (client, username, password, type){this.client = client;this.username = username;this.password = password;this.type = type; //0是機頂盒,1是用戶端}MyClient.prototype.write = function(msg) {this.client.write(msg + '');}module.exports = MyClient;

每一個串連都有它的使用者名稱和密碼,也有它的client,也就是Socket。也有一個標示符,表示是主控還是用戶端

然後添加一個原型方法,用來向當前client發送資訊

下面就是編寫主程式了,使用Node.js進行網路應用程式的開發很簡單,詳細大家能看懂

//tcpvar net = require('net');var crypto = require('crypto');var MyClient = require('./MyClient');var server = net.createServer();//用戶端,如平台、移動端進來放在這個數組中var clientArr = [];//主控端,主要裝的是機頂盒的串連var boxArr = [];server.on('connection', function(client){client.setEncoding('utf-8');client.write('plase input name|password|type :');var myClient; var message = '';//發送訊息client.on('data', function(data){//如果是非斷行符號則累加if('' != data || data == '' || data == null) {message += data;}else {//說明是已經註冊的clientif(myClient) {sendMsg(message, myClient);}else{//說明是第一次進來var userInfo = message.split('|');var md5 = crypto.createHash('md5');//使用使用者名稱和密碼進行加密,放入password中md5.update(userInfo[0] + userInfo[1]);var password = md5.digest('hex');myClient = new MyClient(client, userInfo[0], password, +userInfo[2]);//如果是用戶端if(myClient.type) {clientArr.push(myClient);}else {boxArr.push(myClient);}console.log('新加使用者' + password);}message = '';}})//斷開時移除這個用戶端client.on('end', function(data){console.log('end....');//有還未登入就退出的情況if(myClient) {if(myClient.type) {clientArr.splice(clientArr.indexOf(myClient), 1)}else {boxArr.splice(boxArr.indexOf(myClient), 1)}}})})server.listen(3000);function sendMsg(msg, myClient) {console.log(' sendMsg : ' + msg);var array = myClient.type == 1 ? boxArr : clientArr;for (var i = 0; i < array.length; i++) {if (myClient.password == array[i].password) { array[i].write(msg); console.log(myClient.name + myClient.type == 1 ? '移動端' : '主控' + '發送訊息....');};};}console.log('listening....');

我們來測試一下,利用telnet,使用約定好的協議進行登入,cqut 123456 1,cqut 123456 0,cqut2 123456 1,(這裡不是空格,而是I符號,在文章內顯示有問題,具體看代碼分割就明白了)可以看到,cqut只是給cqut的機頂盒發送,而cqut2的接受不到。

只給對應的裝置發,給其他裝置不會發送

當然Mina還有其強大的過濾器,利用Node.js的中介軟體就能很好的實現,請讀者自行研究

end from http://www.hacke2.cn

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.