Skip to content

A library for passing JSON messages through tcp socket. Library contains both a server and client.

Notifications You must be signed in to change notification settings

mlp1802/easy-tcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

Library for easily creating a TCP socket server that listen to JSON messages. Server and client included.

Install

npm install easy-tcp --save

Server

listen = require('easy-tcp').listen
port = 3000;
//start server
listen(port, function(client){
  console.log("Client connected");
  //listen for "chat" message (message marked with _name=chat)
  client.on("chat", function(m){
    client.send({
      _name: "chat", //this is a reserved word, the name of the message
      text: "Got your message buddy !"
    });
  });
});

Client

connect = require('easy-tcp').connect
//connect to server
connect("localhost", port, function(client){
  //listen for servers response, which is also a chat message
  client.on("chat", function(message){
     console.log("Chat message: " + message.text);
  });
  //send a message
  client.send({
    _name: "chat", //this is a reserved word, the name of the message
    text: "Let's chat !"
  });
});

When done with client

client.close()

If you need to access the underlying socket

var socket = client.socket

About

A library for passing JSON messages through tcp socket. Library contains both a server and client.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published