Skip to content

Instantly share code, notes, and snippets.

@fllfl
Forked from miketamis/document.md
Last active February 9, 2017 21:50
Show Gist options
  • Save fllfl/d727bbf38386dd1f7228b7b4c5cffdb2 to your computer and use it in GitHub Desktop.
Save fllfl/d727bbf38386dd1f7228b7b4c5cffdb2 to your computer and use it in GitHub Desktop.

import these variables to listen to events, these are the 'accepted events':

import { 
  READ_DATA_EVENT_NAME, //fired after a successfull read
  CLOSE_READ_STREAM_EVENT_NAME, //fired after async socket closes the read stream
  DISCONNECT_EVENT_NAME, //fired after a disconnect - event has error as its 'body'
  CONNECT_EVENT_NAME, //fired after  connect - event has port and ip
  ERROR_EVENT_NAME, //fired after error - event the error, a message to say which method broke, and a tag if supplied
  SOCKET_DID_SECURE_EVENT_NAME, //fired whe a connction is secured
  DID_WRITE_PARTIAL_DATA_EVENT_NAME, //fired when part of a data write has happened -- progress
  DID_READ_PARTIAL_DATA_EVENT_NAME, //fired when part of a data read has happened -- progress
} from 'Socket';

Create new transport layer obj via calling Transport with IP

const transport = new Transport('192.168.1.1');
transport.tryConnect((err, res) => {
  if(err) {
    console.log(err);
  } else {
    console.log('we are connected');
    //do something
  }
});

To write a message over the socket use write, this will return a promise. The promise will only resolve if an ack is recieved.

transport.write('Hello how are you doing', (err) => {
  if(err) {
    // AHHH error
    return;
  }
  console.log('message sent properly');
});

You can listen to message from the server through .listen, this is a callback with the message.

transport.listen((err, message) => {
  console.log('Message from server', message);
});
transport.onClose((err) => {
  dosomething();
  leteveryoneknow to stop send shit;
  try to reconnect;
  dispatch(apiActions.onClose());
)
});

To end and writeOut the writes that are buffered.

transport.end()

To DESRTROY aaaaaahh don't don't hurt me

transport.destory()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment