Last active
October 23, 2017 01:53
-
-
Save kaetemi/77f675d6f20ac7d44aeede4e6b34b76d to your computer and use it in GitHub Desktop.
Proposed API usage for NeL Network Message Node.js implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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