Created
July 22, 2014 08:33
-
-
Save nevalla/98c57a03b172b4b37219 to your computer and use it in GitHub Desktop.
Node.js examples for Qt Cloud Services MWS
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
/** | |
* client.js | |
* This script will open socket connection and listen for incoming messages | |
*/ | |
var qtc = require("qtc"); | |
var mws = new qtc.Mws({gatewayId: "GATEWAY_ID"}); | |
// open socket connection | |
mws.open = function(options) { | |
this.getSocketAddress(function(e,res){ | |
if(!e){ | |
var address = res.uri; | |
console.log("Opening websocket at " + address); | |
// open WebSocket | |
var socket = new mws.socket(address); | |
// set some event handlers | |
socket.on('open', function() { | |
options.onOpen(); | |
}); | |
socket.on('close', function() { | |
options.onClose(); | |
}); | |
socket.on('message', function(data) { | |
options.onMessage(data); | |
}); | |
} else { | |
options.onError(e, res) | |
} | |
}); | |
} | |
var options = { | |
onOpen: function() { console.log('connected') }, | |
onClose: function() { console.log('disconnected'); }, | |
onMessage: function(data) { console.log('message', data); }, | |
onError: function(e, res) { console.log("Oops! Something went wrong!", e, res); } | |
}; | |
mws.open(options); |
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
/** | |
* server.js | |
* This script will broadcast a message to all sockets | |
*/ | |
var qtc = require("qtc"); | |
var mws = new qtc.Mws({gatewayId: "GATEWAY_ID"}); | |
// broadcast message to all socket id's | |
var callback = function(result, body) { | |
console.log(body) | |
} | |
mws.send("Hello World!", { sockets: ["*"] }, callback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment