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.
VarMyClient=function(Client,Username,Password,Type){This.Client=Client;This.Username=Username;This.Password=Passwordthis. Type = type//0 is 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
TcpVarNet=Require(' Net ');VarCrypto=Require(' Crypto ');VarMyClient=Require('./myclient ');VarServer=Net.Createserver();The client, such as the platform, the mobile side comes in this arrayVarClientarr=[];Main control side, the main installation is the set-top box connectionVarBoxarr=[];Server.On(' Connection ',function(Client){Client.Setencoding(' Utf-8 ');Client.Write(' Plase input name|password|type: \ n ');VarMyClient;VarMessage=‘‘;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{The first time I came in.VarUserInfo=Message.Split(|);VarMd5=Crypto.CreateHash(' MD5 ');Encrypt with user name and password and put in passwordMd5.Update(UserInfo[0]+UserInfo[1]);VarPassword=Md5.Digest(' Hex ');MyClient=NewMyClient(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 disconnectingClient.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(3000);functionSendmsg(Msg,MyClient){Console.Log(' Sendmsg: '+Msg);VarArray=MyClient.Type==1?Boxarr:Clientarr;For(VarI=0;I<Array.Length;I++){If(MyClient.Password==Array[I].Password){array[i]. Write (msgconsole. Log (myclient. Name + myclient. Type == 1 ' mobile ' ' master ' + ' send message ... ' ); }; }; }console. Log (
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