Skip to content

Instantly share code, notes, and snippets.

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

Callback format: The first argument of the callback is reserved for an error object. If an error occurred, it will be returned by the first err argument. The second argument of the callback is reserved for any successful response data. If no error occurred, err will be set to null and any successful data will be returned in the second argument.

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