Using node. js to reconstruct a smart home server

Source: Internet
Author: User
Tags sendmsg

Original digest self of front-end blog, welcome everyone to visit

http://www.hacke2.cn

Previously responsible for a smart home project development, outsourcing a company in Chongqing, we mainly develop server monitoring and cluster version management.

Mobile and set-top box remote communication is the use of intermediate server to complete the interaction, the server uses the Mina NIO framework, non-blocking, you can look at the previous blog to understand a smart Home project Framework Learning Summary, or other information Java NIO principle, based on the Mina framework for rapid development of network application program.

When the mobile or set-top box is logged in, it is encrypted with spring security, which is encrypted with the user name and password, generating a unique identifier. When the server comes to a request, it checks the corresponding identifier to send the relevant contract commands, such as logging on to the mobile side to send a name to the server, the server generates identifiers such as 522F9E2A459DE81D6A9E9EADFA9468D1, If there is a corresponding identifier master in the set-top box set, send it to him.

Recent attention to node. JS, isn't that the feature of node NiO?

Let's start with refactoring, using the innate 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 + ' \ r \ n ');}Module.exports = myclient;

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

Then add a prototype method to send the message to the current client

The following is the programming of the main program, using node. js for the development of Web applications is very simple, we can read the details

//tcpvar Net = require(' Net ');var Crypto = require(' Crypto ');var myclient = require('./myclient ');var Server = Net.Createserver();//client, such as platform, mobile come in and put it in this arrayvar Clientarr = [];//Main terminal, the main installation is the set-top box connectionvar Boxarr = [];Server. on(' Connection ', function(Client){Client.setencoding(' Utf-8 ');Client.Write(' plase input name|password|type: \ n ');var myclient; var message = "';//Send MessageClient. on(' Data ', function(Data){//If the non-return is cumulativeif(' \ r \ n ' != Data || Data == "' || Data == NULL) {message += Data;}Else {//Description is a registered clientif(myclient) {sendmsg(message, myclient);}Else{//Description is the first time to come invar UserInfo = message.Split(' | ');var MD5 = Crypto.CreateHash(' MD5 ');//Use username and password to encrypt, put into passwordMD5.Update(UserInfo[0] + UserInfo[1]);var Password = MD5.Digest(' Hex ');myclient = New myclient(Client, UserInfo[0], Password, +UserInfo[2]);//If the clientif(myclient.type) {Clientarr.Push(myclient);}Else {Boxarr.Push(myclient);}Console.Log(' new users ' + Password);}message = "';}})//Remove this client when disconnectedClient. on(' End ', function(Data){Console.Log(' End .... ');//exit if you have not logged inif(myclient) {if(myclient.type) {Clientarr.Splice(Clientarr.indexOf(myclient), 1)}Else {Boxarr.Splice(Boxarr.indexOf(myclient), 1)}}})})Server.Listen( the);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 side ' : ' main control ' + ' Send Message .... ');};};}Console.Log(' listening ... ');

Let us test, using Telnet, using the agreed protocol to log in, Cqut 123456 1,cqut 123456 0,cqut2 123456 1, (here is not a space, but I symbol, in the article shows a problem, the specific look at the code segmentation will understand) can see, Cqut just sends Cqut's set-top box, and Cqut2 's acceptance is not.

Send only to the corresponding device, not to other devices

Of course, Mina also has its powerful filters, using node. JS middleware can be very well implemented, ask the reader to study their own

End from http://www.hacke2.cn

Using node. js to reconstruct a smart home server

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.