• Stars
    star
    3
  • Rank 3,963,521 (Top 79 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON messaging over TCP sockets for Node.js

json-tcp-socket

JSON messaging over TCP sockets for node.js

Download count

Install with npm

npm install json-tcp-socket

Without TLS

Server:

var JSONTCPSOCKET = require('json-tcp-socket');

var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
var server = new JSONTCPSOCKET.Server();

server.on('connection', function (socket) {
    console.log('client connected: ', 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 = '{"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('Server listening 0.0.0.0:5055');

Client:

var JSONTCPSOCKET = require('json-tcp-socket');

var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
var socket = new JSONTCPSOCKET.Socket();

socket.on('connect', function () {
    console.log('connected');

    socket.on('data', function (data) {
        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(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');

TLS

Server:

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('client connected: ', 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('Server listening 0.0.0.0:5055 over TLS');

Client:

var JSONTCPSOCKET = require('json-tcp-socket');

var JSONTCPSOCKET = new JSONTCPSOCKET({tls: true});
var socket = new JSONTCPSOCKET.Socket();

socket.on('secureConnect', function () {
    console.log('connected');

    socket.on('data', function (data) {
        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(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({
    port: 5055,
    host: '127.0.0.1',
    rejectUnauthorized: false
});

License

MIT

More Repositories

1

gdb-frontend

β˜• GDBFrontend is an easy, flexible and extensible gui debugger. Try it on https://debugme.dev
JavaScript
2,813
star
2

cebsocket

Lightweight WebSocket library for C.
C
85
star
3

nodes.js

🌌 nodes.js is a nodes/particles animation useable for backgrounds
JavaScript
27
star
4

gdb-frontend-live

GDBFrontendLive is a server that creates GDBFrontend instances and provides online sharable IDEs for each individiual users. πŸ›ΈπŸ‘½πŸŒŒ
C
24
star
5

jsonic

β­• Tricky, super fast and dumb JSON library for C/C++
C
20
star
6

virtual-joystick

Virtual Joystick plugin for Godot Engine
GDScript
13
star
7

GodotCarouselMenu

Horizontal carousel menu for Godot 4
GDScript
13
star
8

vegetables

Multiplayer deathmatch shooter game with cute vegetable characters.
GDScript
13
star
9

MoneroSharp

Monero library for C#
C#
12
star
10

react-kitten

React desktop environment for the web. React-ish and highly customizable. 🐱
TypeScript
9
star
11

jquery.datepicker

⌚ a futuristic datepicker for web
JavaScript
9
star
12

libhash

A fast and efficient non-iterating hashmap library
C
6
star
13

node-Win32Volume

Node.js, set volume level or mute functions for Win32 platform.
C++
4
star
14

pokerunicorn-server

PokerUnicorn Server
C
3
star
15

BigCat

Big number library for Godot Engine (GDScript)
GDScript
3
star
16

3DAudioVisualization

3D audio visualization thing in Godot Engine
GDScript
3
star
17

python-jsonic

Python bindings for Jsonic JSON reader library.
C
2
star
18

GodotBorderRadius

Pixel-sized border radius shader for Godot 4
GDScript
2
star
19

CatPong

Low-level multiplayer pong game with pure TCP protocol and SDL2 graphics for the Cat tutorial on meowingcat.io.
C
2
star
20

snake

Snake is a snake game implementation on HTML5.
JavaScript
2
star
21

meow

Quick screen recorder to GIF
JavaScript
2
star
22

HexBinDecConverter

Hexadecimal, Binary and Decimal number converter plugin for Sublime Text 3
Python
1
star
23

semserv

High-performance async semaphore service useable with long string ipc keys stored in memory.
C
1
star
24

WinDrag

Linux Desktop Environments-Like Alt+Drag Window Mover for Windows
C#
1
star
25

catcrypt

Simple RSA public key encryption library for C/C++.
C
1
star
26

sound-level-protect

Maximum sound level protection for headphones on Win32 platform.
C++
1
star