tcp-emit-server
v0.0.3a ~ Some testing still required before I take it out of alpha.
This module works in conjunction with tcp-emit-client.js
Inspired by the ease of application of Socket.io. This is an event emitter based tcp server manager built using the net module native to NodeJS. It handles emit on all sockets, single sockets or rooms. Management of the connections are taken care of in the module.
Extra Features
Built into the server and client module is the ability to send and receive files using the .fileSend() method. This is a controlled buffer over TCP/IP read and write using only the 'fs' modules read and write streams.
Installation
npm install tcp-emit-server -save
Getting Started
Here is a quick server example.
var tcpserver = require('tcp-emit-server');
var server = new tcpserver('127.0.0.1',3000);
server.on('connection',function(socket){
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">`</span>Socket <span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">socket</span>.<span class="pl-smi">ipaddress</span><span class="pl-pse">}</span></span>:<span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">socket</span>.<span class="pl-smi">ipport</span><span class="pl-pse">}</span></span> connected<span class="pl-pds">`</span></span>);
<span class="pl-c"><span class="pl-c">//</span>you can create any listener you like, ie. 'register'</span>
<span class="pl-smi">socket</span>.<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>register<span class="pl-pds">'</span></span>,<span class="pl-k">function</span>(<span class="pl-smi">username</span>,<span class="pl-smi">password</span>){
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">`</span>Registering <span class="pl-s1"><span class="pl-pse">${</span>username<span class="pl-pse">}</span></span>,<span class="pl-s1"><span class="pl-pse">${</span>password<span class="pl-pse">}</span></span><span class="pl-pds">`</span></span>);
<span class="pl-smi">socket</span>.<span class="pl-en">emit</span>(<span class="pl-s"><span class="pl-pds">'</span>register-done<span class="pl-pds">'</span></span>);
});
<span class="pl-c"><span class="pl-c">//</span>here is native events that emit on the socket object</span>
<span class="pl-smi">socket</span>.<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>data<span class="pl-pds">'</span></span>,<span class="pl-k">function</span>(<span class="pl-smi">buffer</span>){
<span class="pl-k">var</span> data <span class="pl-k">=</span> <span class="pl-smi">buffer</span>.<span class="pl-c1">toString</span>();
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">`</span>RAW SOCKET DATA: <span class="pl-s1"><span class="pl-pse">${</span>data<span class="pl-pse">}</span></span><span class="pl-pds">`</span></span>);
});
<span class="pl-smi">socket</span>.<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>disconnect<span class="pl-pds">'</span></span>,<span class="pl-k">function</span>(){
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">`</span>Socket <span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">socket</span>.<span class="pl-smi">ipaddress</span><span class="pl-pse">}</span></span>:<span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">socket</span>.<span class="pl-smi">ipport</span><span class="pl-pse">}</span></span> DISCONNECTED<span class="pl-pds">`</span></span>);
});
<span class="pl-smi">socket</span>.<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>err<span class="pl-pds">'</span></span>,<span class="pl-k">function</span>(<span class="pl-smi">err</span>){
<span class="pl-en">console</span>.<span class="pl-c1">log</span>(err);
<span class="pl-c"><span class="pl-c">//</span>when sockets don't close properly, this event will be emitted along side the disconnect event.</span>
});
});
Below is a tcp-emit-client sample to connect to the server code above.
npm install tcp-emit-client
var tcpclient = require('tcp-emit-client');
var socket = new tcpclient('127.0.0.1',3000);
socket.on('connect',function(){
console.log('connected to server');
socket.emit("register","myudername","mypassword");
});
socket.on('data',function(buffer){
//use this method is you purely want to read data received on the tcp socket.
var data = buffer.toString();
console.log(</span>RAW SOCKET DATA: <span class="pl-s1"><span class="pl-pse">${</span>data<span class="pl-pse">}</span></span><span class="pl-pds">
);
});
socket.on('register-done',function(){
console.log("This client has been registered");
socket.close(); //closes the tcp connection to the server and destroyed all socket objects
});
socket.on('disconnect',function(){
console.log('Disconnected From Server');
});
socket.on('reconnect',function(){
console.log('Reconnecting to Server');
});
socket.on('err',function(err){
console.log(err);
});
Basic Server Methods
- Create TCP Server Object
new tcpserver(server_ip_address,port_to_run_on);
var tcpserver = require('tcp-emit-server');
var server = new tcpserver('127.0.0.1',3000);
- Connection Event
serverObject.on('connect',function(){});
var tcpserver = require('tcp-emit-server');
var server = new tcpserver('127.0.0.1