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('connection', function (socket) { console.log('クライアント接続: ', socket.socket.remoteAddress);socket.on('data', function (data) {console.log('json:', data.item);});var a = {item: 'A', a: ' asda'};var b = {item: 'B', b: 'asdasa'};var c = {item: '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 = '{ "アイテム": "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 ('サーバーは 0.0.0.0:5055 をリッスンしています');
クライアント:
var JSONTCPSOCKET = require('json-tcp-socket');var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});varソケット = new JSONTCPSOCKET.Socket();socket.on('connect', function () {console .log('接続');socket.on('データ', 関数 (データ) {console.log('json:', data.item);});var a = {item: 'A', a: 'asda'};var b = {item: 'B', b: 'asda' };var c = {アイテム: 'asda', a: 1, b: 2, c: 3, d: 'asda', d: 'asda'};var d = '{"アイテム": "98989182391829381", "a": "1"、"b": "2"、"c": "3"、"d": "asda"、"d": "1231321"}';var e = ' {"アイテム": "ee", "a": "1", "d": "643", "d": "123"}';var f = '{"アイテム": "dd", "a": "1", "d": "643", "d": "123"}';socket.write(a);socket.write(d);ソケット.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 = '{"アイテム": "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ソケット = new JSONTCPSOCKET.Socket();socket.on('secureConnect', function () {console .log('接続');socket.on('データ', 関数 (データ) {console.log('json:', data.item);});var a = {item: 'A', a: 'asda'};var b = {item: 'B', b: 'asda' };var c = {項目: '1231', a: 1, b: 2, c: 3, d: '1231', d: '1231'};var d = '{"項目": "1231"、"a": "1"、"b": "2"、"c": "3"、"d": "123"、"d": "123132"} ';var e = '{"アイテム": "ee", "a": "1", "d": "643", "d": "123"}';var f = '{"アイテム": "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'、拒否未承認: false});
マサチューセッツ工科大学