Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active January 27, 2016 08:17
Show Gist options
  • Save germanviscuso/9c81c5b9ab8cd085dff3 to your computer and use it in GitHub Desktop.
Save germanviscuso/9c81c5b9ab8cd085dff3 to your computer and use it in GitHub Desktop.
Chat example host and client for nodejs. Logs incoming messages on Kii Cloud.
var alljoyn = require('alljoyn');
require("node-jquery-xhr");
kii = require("./KiiSDK").create();
// process command line arguments
if(process.argv.length != 7){
console.log("Usage: node peer.js [host|client] kiiappid kiiappkey username password");
console.log("Create an app at developer.kii.com (US based) to get the app id and key");
console.log("Username and password will be created if they are not registered on Kii Cloud");
process.exit(1);
}
var myArgs = process.argv.slice(2);
var mode = myArgs[0];
var appid = myArgs[1];
var appkey = myArgs[2];
var uuid = myArgs[3];
var password = myArgs[4];
console.log("Initializing Kii Cloud...");
kii.Kii.initializeWithSite(appid, appkey, kii.KiiSite.US);
var bucketName = "org_alljoyn_bus_samples_chat";
console.log("Authenticating peer on Kii Cloud...");
// Authenticate the peer
kii.KiiUser.authenticate(uuid, password, {
// Called on successful authentication
success: function(peer) {
// Print some info to the log
console.log("KiiCloud: Peer authenticated");
//console.log(peer);
},
// Called on a failed authentication
failure: function(peer, errorString) {
// Print some info to the log
console.log("Error authenticating peer: " + errorString);
console.log("Trying peer registration...");
// Create the KiiUser object
var user = kii.KiiUser.userWithUsername(uuid, password);
// Register the user, defining callbacks for when the process completes
user.register({
// Called on successful registration
success: function(peer) {
// Print some info to the log
console.log("KiiCloud: Peer registered");
//console.log(peer);
},
// Called on a failed registration
failure: function(peer, errorString) {
// Print some info to the log
console.log("Error registering peer: " + errorString);
process.exit(2);
}
});
}
})
console.log("Loading alljoyn bus...");
var sessionId = 0;
var portNumber = 27;
var serviceName = "org.alljoyn.bus.samples.chat.test";
var interfaceName = "org.alljoyn.bus.samples.chat";
var bus = alljoyn.BusAttachment("chat");
var inter = alljoyn.InterfaceDescription();
console.log("CreateInterfaceInBus " + bus.createInterface(interfaceName, inter));
console.log("AddingSignal " + inter.addSignal("Chat", "s", "msg"));
var busListener = alljoyn.BusListener(
function(name){
console.log("BusListener: FoundAdvertisedBusName", name);
sessionId = bus.joinSession(name, portNumber, 0);
console.log("BusListener: JoinedSession " + sessionId);
},
function(name){
console.log("BusListener: LostAdvertisedBusName", name);
},
function(name){
console.log("BusListener: BusNameOwnerChanged", name);
}
);
console.log("Registering bus listener...");
bus.registerBusListener(busListener);
console.log("StartingBus " + bus.start());
console.log("Creating chat service object...");
var chatObject = alljoyn.BusObject("/chatService");
console.log("AddInterfaceToObject " + chatObject.addInterface(inter));
console.log("RegisterSignalHandler " + bus.registerSignalHandler(chatObject, function(msg, info){
//console.log("Signal received: ", msg, info);
console.log(info["sender"], msg["0"]);
//log received message on Kii Cloud
//create an application scope bucket
var appBucket = kii.Kii.bucketWithName(bucketName);
// Create the object with key/value pairs
var obj = appBucket.createObject();
obj.set("message", msg["0"]);
obj.set("sender", info["sender"]);
obj.set("session_id", info["session_id"]);
obj.set("timestamp", info["timestamp"]);
obj.set("member_name", info["member_name"]);
obj.set("object_path", info["object_path"]);
obj.set("signature", info["signature"]);
// Save the object
obj.save({
success: function(obj) {
//console.log("Msg logged");
//console.log(obj);
},
failure: function(obj, errorString) {
console.log("Error logging msg: " + errorString);
}
});
}, inter, "Chat"));
console.log("RegisterBusObject " + bus.registerBusObject(chatObject));
console.log("ConnectBus " + bus.connect());
var portListener = alljoyn.SessionPortListener(
function(port, joiner){
console.log("PortListener: AcceptSessionJoiner", port, joiner);
//return port == portNumber;
return true;
},
function(port, sId, joiner){
sessionId = sId;
console.log("PortListener: SessionJoinedByJoiner", port, sessionId, joiner);
}
);
if(mode.localeCompare("host") == 0){ // host advertises service
console.log("BusRequestName " + bus.requestName(serviceName));
console.log("BusBindSessionPortWithPortListener " + bus.bindSessionPort(27, portListener));
console.log("BusAdvertiseName " + bus.advertiseName(serviceName));
} else { // client looks for service rather than advertise it
console.log("BusFindAdvertisedName " + bus.findAdvertisedName(serviceName));
}
// Added Chat to example
var stdin = process.stdin;
// uncomment if you want one message per key pressed
//stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// i don't want binary, do you?
stdin.setEncoding( 'utf8' );
// on any data into stdin
stdin.on( 'data', function( key ){
// ctrl-c ( end of text )
if ( key === '\u0003' ) {
process.exit();
}
// write the key to stdout all normal like
process.stdout.write( ":me " + key);
chatObject.signal(null, sessionId, inter, "Chat", key.replace(/(\r\n|\n|\r)/gm,""));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment