json tcp socket
1.0.0
Node.js 透過 TCP 套接字進行 JSON 訊息傳遞
npm 安裝 json-tcp-socket
伺服器:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});var server = new JSONTCPSOCKET.Server();server.on('連接', function (socket) { console.log('客戶端已連線:', socket.socket.remoteAddress);socket.on('data', function (data) {console.log('json:', data.item);});var a = {項目:'A',a:'asda'};var b = {項目:'B',b:'asdasa'};var c = {項目:'asdadsadsadsasd87a8ds7ads67ad6sa6dsa',a:1,b:2 ,c : 3, d: 'asda', d: 'asdadsa'};var d = '{"item": "asdaasda", "a": "1", "b": "2", "c" : " 3", "d": "阿斯達", "d": "阿斯達"}';var e = '{"item": "ee", "a": "1", "d ": "643", "d": "123"}';var f = '{"item": "dd", "a": "1", "d": "643", "d": " 123"}';套接字.write(a);socket.write(a);socket.write(a);socket.write(b);setInterval(function () {socket.write(e);}, 1000);setInterval(function () {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('伺服器監聽0.0.0.0:5055');
客戶:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});var socket = new JSONTCPSOCKET.Socket();socket.on('connect', funcet());socket.on('connect', funcet('consolet.on('connect', funcet()); .log('已連接');socket.on('data', function (data) {console.log('json:', data.item);});var a = {item: 'A', a : '阿斯達'};var b = {項目:'B',b:'阿斯達'};var c = {項目:'阿斯達',a:1,b:2,c:3 ,d:'阿斯達', 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(function () {socket.write(e);}, 1000);setInterval(function () {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');
伺服器:
var fs = require('fs');var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = new JSONTCPSOCKET({tls: true});var server = new JSONTCPSOCKET.Server({key: fs. readFileSync(__dirname+'/cert/tls.key'),cert: fs.readFileSync(__dirname+'/cert/tls.crt')});server.on('secureConnection', function (socket) {console.log('客戶端已連線: ', socket.socket.remoteAddress);socket.on('data', function (data) {console.log('json:', data.item);});var a = {item: 'A ', a: 'asdasda'};var b = {item: 'B', b: 'asdasda'};var c = {item: '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(function () {socket.write(e);}, 1000);setInterval(function () {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('伺服器透過 TLS 監聽 0.0.0.0:5055');
客戶:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = new JSONTCPSOCKET({tls: true});var socket = new JSONTCPSOCKET.Socket();socket.on('secureConnect', funcet();socket.on('secureConnect', funcet(); .log('已連接');socket.on('data', function (data) {console.log('json:', data.item);});var a = {item: 'A', a : '阿斯達'};var b = {項目:'B',b:'阿斯達'};var c = {項目:'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(function () {socket.write(e);}, 1000);setInterval(function () {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 ',rejectUnauthorized: false});
麻省理工學院