0.1.0 • Published 8 years ago
sectord17-communication v0.1.0
SECTOR D17 Communication Server
Works on Linux x64 machines (tested and created on Ubuntu 16.04 LTS x64)
Installation
npm install
- that would download all dependencies and setup boost
Documentation
Remarks:
- Hold somewhere
Server {}
object from library - loosing that reference would allow GarbageCollector to destroy that object and library would crash. In example reference is kept because of periodic existence test. - Hold all
Connection {}
objects as well - Initialize callbacks as in example
Server {
// Connection is another library object
onConnection(function (Connection) {
...
})
// Error is Buffer object holding plain utf8 string
onError(function (Error) {
...
})
// start listening for incoming connections
start()
}
Connection {
// Message is Buffer object holding received data
onMessage(function (Message) {
...
})
// Error same as in Server
onError(function (Error) {
...
})
// send data as Buffer object
sendUDP(Buffer)
sendTCP(Buffer)
// start sending and receiving data
start()
}
Example library usage:
var serverlib = require('sectord17-communication');
var server = serverlib.Server("localhost", "20002"); // pass ip and port as strings
var connections = [];
// That here is the most important thing!
console.log(server);
var msg = "Nothing occurred as for now ...";
server.onConnection(function (connection) {
console.log(connection);
// message is in Buffer format
connection.onMessage(function (message) {
counter += 1;
var string = message.toString();
console.log(counter, "Message: ", string);
msg = message;
});
// error is in Buffer as well
connection.onError(function (error) {
var string = error.toString();
console.log("Error: ", string);
});
connection.start();
connections.push(connection);
});
server.onError(function (error) {
console.log(error);
});
server.start();
// periodically broadcast message to all connected clients
function broadcastMessage() {
connections.forEach(function (connection) {
// send in Buffer as well
var buffer = Buffer.from('this is a tést');
connection.sendUDP(buffer);
});
}
setInterval(broadcastMessage, 2000);
// those below would keep Server object reference from being collected by Garbage Collector
// just keep is as part of something and everything should work :)
function testAfterAWhile() {
console.log("Existence test", server);
setTimeout(testAfterAWhile, 10000);
}
setTimeout(testAfterAWhile, 10000);