var http = require('http'), net = require('net'), sockjs = require('sockjs'), ADDR_GPS = "127.0.0.1", // , - PORT_GPS = 3201, // server = sockjs.createServer(); server.on('connection', function(conn) { // - SockJS // , , // , var com = new Commander(ADDR_GPS, PORT_GPS, conn, function(e){ console.log("! We had an Error in socket: ", e, "at ", new Date()); conn.close(); }); conn.on('data', function(data) { // JSON // var dat = JSON.parse(data); if(dat.command == "@auth") { // , com.auth(dat.param.log, dat.param.pwd); } else if(dat.command == "@bye") { // - com.bye(); conn.close(); } }); conn.on('close', function() { console.log("Websocket connection closed"); com = null; }); }); // -HTTP- // SockJS, http://mydomen.com:8081/data var srv = http.createServer(); server.installHandlers(srv, {prefix:'/data'}); srv.listen(8081, '0.0.0.0'); var Commander = function (adr, port, clientConn, onError) { // var self = this; this.status = 0; this.chunk = ""; // this.answers = []; // this.connection = clientConn; // , // this.client = new net.Socket(); // this.client.connect(port,adr,function(){ // console.log("New connect to created..."); }); this.client.on('data', function(data) { // - self.onData(data); }); this.client.on('error', function(e) { // onError(e); self.client.destroy(); }); }; Commander.prototype.auth = function(login, pass) { // console.log("written auth for "+ login); this.client.write('(auth "'+login+'" "'+pass+'")\n'); }; Commander.prototype.bye = function() { // this.client.write('(exit)\n'); }; Commander.prototype.onData = function(data) { // , , // -, // , // - var pos; this.chunk+=data.toString(); pos=this.chunk.indexOf('\n'); if(pos > -1) { this.connection.write(this.chunk.substring(0,pos)); this.chunk = this.chunk.substring(pos); } };
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title> </title> <style> body { padding:0; margin: 0; font: 10pt sans-serif, Arial, Tahoma; } h1 { font-size: 2em; margin: 0.8em 0; } h3 { font-size:1.5em; margin: 0.1em; } #content { position: relative; margin: 0 auto; width:960px; min-width:800px; } #left { position:absolute; top:0px; left:0px; padding:2px; width:220px; height:560px; } #right { position:absolute; top:0px; left:250px; padding:2px; width:710px; height:560px; } #scroller { position:relative; width: 400px; height:90%; overflow-y:auto; border:1px dotted black; padding:5px; margin-top:10px; } .off { color:red; } .on { color: green; } .inBottom { position: absolute; bottom: 20px; } </style> <script src="sockjs-0.3.4.min.js" type="text/javascript"></script> <script> var sock;stat = document.getElementById("status"); function connect() { // sock = new SockJS('http://mysite.com:8081/data'); var l = document.getElementById("login").value, p = document.getElementById("passw").value stat = document.getElementById("status"); setTimeout(function(){ // , sock.send(toJSON("@auth", { log: l, pwd: p })); },2000); sock.onopen = function() { // , stat.innerHTML = "ON"; stat.className = "on"; }; sock.onmessage = function(e) { // , , // "data" document.getElementById("scroller").innerHTML += "<p>"+e.data+"</p>"; }; sock.onclose = function() { // , stat.innerHTML = "OFF"; stat.className = "off"; }; } function disconnect() { // if(sock !== undefined) { sock.send(toJSON("@bye", {})); } } function toJSON (com, param){ return JSON.stringify({ command: com, param: param }); } </script> </head> <body> <div id="content"> <div id="left"> <p style="width:100%;"> <input type="text" id="login" style="float:right;"></p> <p style="width:100%;"> <input type="password" id="passw" style="float:right;"></p> <button onclick="connect();"></button><button onclick="disconnect();"></button><br> <p class="inBottom">: <span id="status"></span></p> </div> <div id="right"> <div id="scroller"></div> </div> </div> </body> </html>
Source: https://habr.com/ru/post/186352/
All Articles