<ul class="pagehead-actions">
  •     <div class="select-menu js-menu-container js-select-menu">
          <a data-ga-click="Repository, click Watch settings, action:blob#show" aria-label="Toggle repository notifications menu" aria-expanded="false" aria-haspopup="true" role="button" class="btn btn-sm btn-with-count select-menu-button js-menu-target" href="/claytonbez/tcp-emit-server/subscription">
            <span class="js-select-button">
                <svg aria-hidden="true" height="16" width="16" version="1.1" viewBox="0 0 16 16" class="octicon octicon-eye"><path d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z" fill-rule="evenodd"></path></svg>
                Unwatch
            </span>
          </a>
          <a aria-label="1 user is watching this repository" href="/claytonbez/tcp-emit-server/watchers" class="social-count js-social-count">
            1
          </a>
    
        <div class="select-menu-modal-holder">
          <div class="select-menu-modal subscription-menu-modal js-menu-content">
            <div tabindex="-1" class="select-menu-header js-navigation-enable">
              <svg height="16" width="12" version="1.1" viewBox="0 0 12 16" aria-label="Close" role="img" class="octicon octicon-x js-menu-close"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z" fill-rule="evenodd"></path></svg>
              <span class="select-menu-title">Notifications</span>
            </div>
    
              <div role="menu" class="select-menu-list js-navigation-container">
    
                <div tabindex="0" role="menuitem" class="select-menu-item js-navigation-item">
                  <svg aria-hidden="true" height="16" width="12" version="1.1" viewBox="0 0 12 16" class="octicon octicon-check select-menu-item-icon"><path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z" fill-rule="evenodd"></path></svg>
                  <div class="select-menu-item-text">
                    <input value="included" id="do_included" name="do" type="radio">
                    <span class="select-menu-item-heading">Not watching</span>
                    <span class="description">Be notified when participating or @mentioned.</span>
                    <span class="js-select-button-text hidden-select-button-text">
                      <svg aria-hidden="true" height="16" width="16" version="1.1" viewBox="0 0 16 16" class="octicon octicon-eye"><path d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z" fill-rule="evenodd"></path></svg>
                      Watch
                    </span>
                  </div>
                </div>
    
                <div tabindex="0" role="menuitem" class="select-menu-item js-navigation-item selected">
                  <svg aria-hidden="true" height="16" width="12" version="1.1" viewBox="0 0 12 16" class="octicon octicon-check select-menu-item-icon"><path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z" fill-rule="evenodd"></path></svg>
                  <div class="select-menu-item-text">
                    <input checked="checked" value="subscribed" id="do_subscribed" name="do" type="radio">
                    <span class="select-menu-item-heading">Watching</span>
                    <span class="description">Be notified of all conversations.</span>
                    <span class="js-select-button-text hidden-select-button-text">
                      <svg aria-hidden="true" height="16" width="16" version="1.1" viewBox="0 0 16 16" class="octicon octicon-eye"><path d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z" fill-rule="evenodd"></path></svg>
                        Unwatch
                    </span>
                  </div>
                </div>
    
                <div tabindex="0" role="menuitem" class="select-menu-item js-navigation-item">
                  <svg aria-hidden="true" height="16" width="12" version="1.1" viewBox="0 0 12 16" class="octicon octicon-check select-menu-item-icon"><path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z" fill-rule="evenodd"></path></svg>
                  <div class="select-menu-item-text">
                    <input value="ignore" id="do_ignore" name="do" type="radio">
                    <span class="select-menu-item-heading">Ignoring</span>
                    <span class="description">Never be notified.</span>
                    <span class="js-select-button-text hidden-select-button-text">
                      <svg aria-hidden="true" height="16" width="16" version="1.1" viewBox="0 0 16 16" class="octicon octicon-mute"><path d="M8 2.81v10.38c0 .67-.81 1-1.28.53L3 10H1c-.55 0-1-.45-1-1V7c0-.55.45-1 1-1h2l3.72-3.72C7.19 1.81 8 2.14 8 2.81zm7.53 3.22l-1.06-1.06-1.97 1.97-1.97-1.97-1.06 1.06L11.44 8 9.47 9.97l1.06 1.06 1.97-1.97 1.97 1.97 1.06-1.06L13.56 8l1.97-1.97z" fill-rule="evenodd"></path></svg>
                        Stop ignoring
                    </span>
                  </div>
                </div>
    
              </div>
    
            </div>
          </div>
        </div>
    
  • 0
    0
  • 0
  •   <h1 class="public">
    

    claytonbez/tcp-emit-server

    </div>
    

    Permalink

    <div class="select-menu-modal">
      <div class="select-menu-header">
        <svg height="16" width="12" version="1.1" viewBox="0 0 12 16" aria-label="Close" role="img" class="octicon octicon-x js-menu-close"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z" fill-rule="evenodd"></path></svg>
        <span class="select-menu-title">Switch branches/tags</span>
      </div>
    
      <div class="select-menu-filters">
        <div class="select-menu-text-filter">
          <input placeholder="Find or create a branch…" class="form-control js-filterable-field js-navigation-enable" id="context-commitish-filter-field" aria-label="Find or create a branch…" type="text">
        </div>
        <div class="select-menu-tabs">
          <ul>
            <li class="select-menu-tab">
              <a role="tab" class="js-select-menu-tab" data-filter-placeholder="Find or create a branch…" data-tab-filter="branches" href="#">Branches</a>
            </li>
            <li class="select-menu-tab">
              <a role="tab" class="js-select-menu-tab" data-filter-placeholder="Find a tag…" data-tab-filter="tags" href="#">Tags</a>
            </li>
          </ul>
        </div>
      </div>
    
      <div role="menu" data-tab-filter="branches" class="select-menu-list select-menu-tab-bucket js-select-menu-tab-bucket">
    
        <div data-filterable-type="substring" data-filterable-for="context-commitish-filter-field">
    
    
            <a rel="nofollow" data-skip-pjax="true" data-name="master" href="/claytonbez/tcp-emit-server/blob/master/README.md" class="select-menu-item js-navigation-item js-navigation-open selected">
              <svg aria-hidden="true" height="16" width="12" version="1.1" viewBox="0 0 12 16" class="octicon octicon-check select-menu-item-icon"><path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z" fill-rule="evenodd"></path></svg>
              <span class="select-menu-item-text css-truncate-target js-select-menu-filter-text">
                master
              </span>
            </a>
        </div>
    
          <form method="post" action="/claytonbez/tcp-emit-server/branches" class="js-create-branch select-menu-item select-menu-new-item-form js-navigation-item js-new-item-form"><input value="✓" type="hidden" name="utf8"><input value="Ba33cceaczVs62y57QqqUciPrQxaqe207xM8hNV3dyUQRtnfDdfw/BquvxPuBJbGzvPeVBLTU3on+KeFMOLHkA==" name="authenticity_token" type="hidden">
          <svg aria-hidden="true" height="16" width="10" version="1.1" viewBox="0 0 10 16" class="octicon octicon-git-branch select-menu-item-icon"><path d="M10 5c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v.3c-.02.52-.23.98-.63 1.38-.4.4-.86.61-1.38.63-.83.02-1.48.16-2 .45V4.72a1.993 1.993 0 0 0-1-3.72C.88 1 0 1.89 0 3a2 2 0 0 0 1 1.72v6.56c-.59.35-1 .99-1 1.72 0 1.11.89 2 2 2 1.11 0 2-.89 2-2 0-.53-.2-1-.53-1.36.09-.06.48-.41.59-.47.25-.11.56-.17.94-.17 1.05-.05 1.95-.45 2.75-1.25S8.95 7.77 9 6.73h-.02C9.59 6.37 10 5.73 10 5zM2 1.8c.66 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2C1.35 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2zm0 12.41c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm6-8c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z" fill-rule="evenodd"></path></svg>
            <div class="select-menu-item-text">
              <span class="select-menu-item-heading">Create branch: <span class="js-new-item-name"></span></span>
              <span class="description">from ‘master’</span>
            </div>
            <input class="js-new-item-value" type="hidden">
            <input value="master" id="branch" name="branch" type="hidden">
            <input value="README.md" id="path" name="path" type="hidden">
    
      <div data-tab-filter="tags" class="select-menu-list select-menu-tab-bucket js-select-menu-tab-bucket">
        <div data-filterable-type="substring" data-filterable-for="context-commitish-filter-field">
    
    
        </div>
    
        <div class="select-menu-no-results">Nothing to show</div>
      </div>
    
    </div>
    
    <div class="BtnGroup float-right">
      <a data-hotkey="t" data-pjax="" class="js-pjax-capture-input btn btn-sm BtnGroup-item" href="/claytonbez/tcp-emit-server/find/master">
        Find file
      </a>
      
        Copy path
      
    </div>
    <div class="breadcrumb" id="blob-path">
      <span class="repo-root js-repo-root"><span class="js-path-segment"><a href="/claytonbez/tcp-emit-server" data-pjax="true"><span>tcp-emit-server</span></a></span></span><span class="separator">/</span><strong class="final-path">README.md</strong>
    </div>
    
    17ec7ee Jul 1, 2018
    <div class="commit-tease-contributors">
      <button data-facebox="#blob_contributors_box" class="btn-link muted-link contributors-toggle" type="button">
        <strong>1</strong>
         contributor
      </button>
      
    </div>
    
    <div style="display:none" id="blob_contributors_box">
      <h2 data-facebox-id="facebox-header" class="facebox-header">Users who have contributed to this file</h2>
      <ul data-facebox-id="facebox-description" class="facebox-user-list">
          <li class="facebox-user-list-item">
            <a href="/claytonbez" data-octo-dimensions="link_type:self" data-octo-click="hovercard-link-click" data-hovercard-user-id="6775201" class="d-inline-block"><img alt="@claytonbez" height="24" width="24" src="https://avatars1.githubusercontent.com/u/6775201?s=48&amp;v=4"></a>
            <a href="/claytonbez" data-octo-dimensions="link_type:self" data-octo-click="hovercard-link-click" data-hovercard-user-id="6775201">claytonbez</a>
          </li>
      </ul>
    </div>
    
    <div class="BtnGroup">
      <a href="/claytonbez/tcp-emit-server/raw/master/README.md" class="btn btn-sm BtnGroup-item" id="raw-url">Raw</a>
        <a href="/claytonbez/tcp-emit-server/blame/master/README.md" data-hotkey="b" class="btn btn-sm js-update-url-with-hash BtnGroup-item">Blame</a>
      <a href="/claytonbez/tcp-emit-server/commits/master/README.md" class="btn btn-sm BtnGroup-item" rel="nofollow">History</a>
    </div>
    
        <a data-ga-click="Repository, open with desktop, type:windows" aria-label="Open this file in GitHub Desktop" href="https://desktop.github.com" class="btn-octicon tooltipped tooltipped-nw">
            <svg aria-hidden="true" height="16" width="16" version="1.1" viewBox="0 0 16 16" class="octicon octicon-device-desktop"><path d="M15 2H1c-.55 0-1 .45-1 1v9c0 .55.45 1 1 1h5.34c-.25.61-.86 1.39-2.34 2h8c-1.48-.61-2.09-1.39-2.34-2H15c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm0 9H1V3h14v8z" fill-rule="evenodd"></path></svg>
        </a>
    
          <form method="post" action="/claytonbez/tcp-emit-server/edit/master/README.md" class="inline-form js-update-url-with-hash"><input value="✓" type="hidden" name="utf8"><input value="a0K+Jr0k7RTekyTmbyvuXdcGfWQF2koVUrLymI9UOjNEHYMkOGo8GUlCmdsd8lDRqw5CUguvu7jO3L0EAMZzDA==" name="authenticity_token" type="hidden">
            <button data-disable-with="" data-hotkey="e" aria-label="Edit this file" type="submit" class="btn-octicon tooltipped tooltipped-nw">
              <svg aria-hidden="true" height="16" width="14" version="1.1" viewBox="0 0 14 16" class="octicon octicon-pencil"><path d="M0 12v3h3l8-8-3-3-8 8zm3 2H1v-2h1v1h1v1zm10.3-9.3L12 6 9 3l1.3-1.3a.996.996 0 0 1 1.41 0l1.59 1.59c.39.39.39 1.02 0 1.41z" fill-rule="evenodd"></path></svg>
            </button>
    
    230 lines (202 sloc) 8.4 KB

    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