Using Node. js to reconstruct a smart home server

Source: Internet
Author: User
Tags sendmsg

Using Node. js to reconstruct a smart home server

 

Previously, I was responsible for the development of a smart home project and outsourced to a company in Chongqing. We mainly developed server monitoring and cluster version management.

The remote communication between the mobile terminal and the set-top box interacts with the intermediate server. The server uses the mina nio framework, which is non-blocking. You can refer to the previous blog to learn about the framework of a smart home project, for other information, see java nio principles. Based on the MINA framework, you can quickly develop network applications.

After logging on to the mobile terminal or the set-top box, spring security is used for encryption. the encryption is mainly based on the user name and password to generate a unique identifier. When the server comes to a request, it will check the corresponding identifier to send the agreed commands. If you log on to the mobile terminal and send a name to the server, the server will generate a identifier such as 522f9e2a459de81d6a9e9eadfa9468d1, if the set-top-box set contains the corresponding identifier of the master, send it to him.

Recently, I have been paying attention to Node. js. Isn't it Node's NIO feature?

Let's start refactoring, taking advantage of the inherent advantages of Node. js, high concurrency, non-blocking

First encapsulate the connection

Var MyClient = function (client, username, password, type) {this. client = client; this. username = username; this. password = password; this. type = type; // 0 is the set-top box, 1 is the client} MyClient. prototype. write = function (msg) {this. client. write (msg + '');} module. exports = MyClient;

Each connection has its user name and password, as well as its client, that is, Socket. There is also a identifier, indicating whether it is the master or the client

Then add a prototype method to send information to the current client.

The following describes how to write the main program. It is very easy to develop network applications using Node. js.

// Tcpvar net = require ('net'); var crypto = require ('crypto'); var MyClient = require ('. /MyClient '); var server = net. createServer (); // The client, such as the platform or mobile client, is put in this array var clientArr = []; // The Master, the main installation is the connection of the set-top box var boxArr = []; server. on ('connection', function (client) {client. setEncoding ('utf-8'); client. write ('plase input name | password | type: '); var myClient; var message = ''; // send the message client. on ('data', function (data) {// if it is not a carriage return Accumulate if (''! = Data | data = ''| data = null) {message + = data;} else {// indicates the registered clientif (myClient) {sendMsg (message, myClient);} else {// This is the first time var userInfo = message. split ('|'); var md5 = crypto. createHash ('md5'); // use the user name and password for encryption, and put md5.update (userInfo [0] + userInfo [1]) in the password; var password = md5.digest ('hex'); myClient = new MyClient (client, userInfo [0], password, + userInfo [2]); // if the client is if (myClient. type) {clie NtArr. push (myClient);} else {boxArr. push (myClient);} console. log ('new user' + password);} message = '';}}) // remove the client when the client is disconnected. 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? 'Mobile terminal ': 'master' + 'send message... ') ;};}}console. log ('listening ....');

Let's test, use telnet to log on using the agreed protocol, cqut 123456 1, cqut 123456 0, cqut2 123456 1, (here it is not a space, but an I symbol, the article shows that there is a problem, but the code is clearly divided.) You Can See That cqut is only sent to the cqut set-top box, while cqut2 is not accepted.

Only send to the corresponding device, not to other devices

Of course, Mina also has its powerful filter, which can be well implemented using Node. js middleware. Please study it on your own.

End from http://www.hacke2.cn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.