Skip to content

Instantly share code, notes, and snippets.

@kaetemi
Last active October 23, 2017 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaetemi/77f675d6f20ac7d44aeede4e6b34b76d to your computer and use it in GitHub Desktop.
Save kaetemi/77f675d6f20ac7d44aeede4e6b34b76d to your computer and use it in GitHub Desktop.
Proposed API usage for NeL Network Message Node.js implementation
let nnmessage = require('nnmessage');
let server = nnmessage.createServer();
server.onConnected(function(connection) {
// called whe new client connects
connection.onMessage(....);
});
server.onDisconnected(function(connection, err) {
// called when disconnected from a successful connection, err contains disconnection cause
});
server.onMessage("CHAT", function(connection, buffer) { // can set on either server or per connection, per-connection overrides
// do something
});
server.onRequest("ECHO", function(connection, seq, buffer) {
connection.sendResponse(seq, buffer);
});
let buf = ...;
connection.sendMessage("CHAT", buf, function(err) {
// function called when sent successfully, return with error if send fails (due to disconnect)
});
connection.sendRequest("ECHO", buf, function(err, buffer) {
// api will keep note of the used sequence id, and call the callback when response with the id is received (remove callback from queue)
// in case server disconnects before response is received, callback is called with err set to disconnect reason, and bufer null
});
server.listen(40000, function(err) {
// callback on success, or failure with err set
});
server.disconnect(); // immediate disconnect
client = nnmessage.createClient();
client.onDisconnected(function(err) {
// called when disconnected from a successful connection, err contains disconnection cause
});
client.connect(ip, port, function(err) {
// ...
});
client.onMessage(...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment