json tcp socket
1.0.0
Mensajería JSON a través de sockets TCP para node.js
npm instala json-tcp-socket
Servidor:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = nuevo JSONTCPSOCKET({tls: false});var servidor = nuevo JSONTCPSOCKET.Server();server.on('conexión', función (socket) { console.log('cliente conectado: ', socket.socket.remoteAddress);socket.on('datos', función (datos) {console.log('json:', data.item);});var a = {item: 'A', a: 'asda'};var b = {item: 'B', b: 'asdasa' };var c = {elemento: 'asdadsadsadsasd87a8ds7ads67ad6sa6dsa', a: 1, b: 2, c: 3, d: 'asda', d: 'asdadsa'};var d = '{"item": "asdaasda", "a": "1", "b": "2", "c": "3", "d": "asda", "d": "asdasda"}';var e = '{"item": "ee", "a": "1", "d": "643", "d": "123"}';var f = '{"elemento": "dd", "a": "1", "d": "643", "d": "123"}';socket.write(a);socket.write(a);socket.write(a);socket.write(b);setInterval(function () {socket. escribir(e);}, 1000);setInterval(función () {socket.write(f);}, 1500);socket.write(d);socket.write(c);socket.write(b);socket.write(b);});server.listen(5055, '0.0.0.0');console.log ('Servidor escuchando 0.0.0.0:5055');
Cliente:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = nuevo JSONTCPSOCKET({tls: false});var socket = nuevo JSONTCPSOCKET.Socket();socket.on('conectar', función () {consola .log('conectado');socket.on('datos', función (datos) {console.log('json:', data.item);});var a = {item: 'A', a: 'asda'};var b = {item: 'B', b: 'asda'};var c = {item: 'asda ', a: 1, b: 2, c: 3, d: 'asda', d: 'asda'};var d = '{"item": "98989182391829381", "a": "1", "b": "2", "c": "3", "d": "asda", "d": "1231321"}';var e = '{"item": "ee" , "a": "1", "d": "643", "d": "123"}';var f = '{"item": "dd", "a": "1", "d ": "643", "d": "123"}';socket.write(a);socket.write(d);socket.write(b);setInterval(función () {socket.write(e);}, 1000);setInterval(función () {socket.write(f);}, 1500);socket.write(b);socket.write(a);socket.write(a);socket.write(a);socket.write(c);});socket.connect(5055, '127.0. 0,1');
Servidor:
var fs = require('fs');var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = nuevo JSONTCPSOCKET({tls: true});var servidor = nuevo JSONTCPSOCKET.Server({key: fs. readFileSync(__dirname+'/cert/tls.key'),cert: fs.readFileSync(__dirname+'/cert/tls.crt')});server.on('secureConnection', función (socket) {console.log('cliente conectado: ', socket.socket.remoteAddress);socket.on ('datos', función (datos) {console.log('json:', datos.item);});var a = {item: 'A', a: 'asdasda'};var b = {elemento: 'B', b: 'asdasda'};var c = {elemento: '1231', a: 1, b: 2, c: 3, d: '1231321', d: '1231321'};var d = '{"item": "98989182391829381", "a": "1", "b": "2", "c": "3", "d": "1231", "d": "1231231"}';var e = '{"item": "ee", "a": "1" , "d": "643", "d": "123"}';var f = '{"item": "dd", "a": "1", "d": "643", "d ": "123"}';socket.write(a);socket.write(a);socket.write(a);socket.write(b);setInterval(función () {socket.write(e);}, 1000 );setInterval(función () {socket.write(f);}, 1500);socket.write(d);socket.write(c);socket.write(b);socket.write(b);});server.listen(5055, '0.0.0.0');console.log ('Servidor escuchando 0.0.0.0:5055 sobre TLS');
Cliente:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = nuevo JSONTCPSOCKET({tls: true});var socket = nuevo JSONTCPSOCKET.Socket();socket.on('secureConnect', función () {consola .log('conectado');socket.on('datos', función (datos) {console.log('json:', data.item);});var a = {item: 'A', a: 'asda'};var b = {item: 'B', b: 'asda'};var c = {item: '1231 ', a: 1, b: 2, c: 3, d: '1231', d: '1231'};var d = '{"item": "1231", "a": "1", "b ": "2", "c": "3", "d": "123", "d": "123132"}';var e = '{"item": "ee", "a": "1" , "d": "643", "d": "123"}';var f = '{"item": "dd", "a": "1", "d": "643", "d ": "123"}';socket.write(a);socket.write(d);socket.write(b);setInterval(función () {socket.write(e);}, 1000);setInterval(función () {socket.write(f);}, 1500);socket.write(b);socket.write(a);socket.write(a);socket.write(a);socket.write(c);});socket.connect({puerto: 5055, host: '127.0.0.1', rechazar no autorizado: falso});
MIT