Skip to content

Instantly share code, notes, and snippets.

@jparreira
Created November 25, 2013 20:53
Show Gist options
  • Save jparreira/7648692 to your computer and use it in GitHub Desktop.
Save jparreira/7648692 to your computer and use it in GitHub Desktop.
Realtime Cloud Messaging (ORTC) announcement channels test
<html>
<head>
<title>Announcement channels test</title>
<script type="text/javascript" src="http://dfdbz2tdq3k01.cloudfront.net/js/2.1.0/ortc.js"></script>
</head>
<body>
<script>
loadOrtcFactory(IbtRealTimeSJType, function (factory, error) {
if (error != null) {
alert("Factory error: " + error.message);
} else {
if (factory != null) {
realtimeClient = factory.createClient();
realtimeClient.setClusterUrl('http://ortc-developers.realtime.co/server/2.1/');
realtimeClient.setConnectionMetadata("The client metadata");
realtimeClient.onConnected = function (ortc) {
realtimeClient.subscribe("test-channel", true, function (ortc, channel, message) {
console.log(message);
});
};
realtimeClient.onSubscribed = function(ortc, channel) {
console.log("This client subscribed channel " + channel + " sucessfully!");
console.log("Will now disconnect");
realtimeClient.disconnect();
}
realtimeClient.onException = function (ortc, exception) {
console.error(exception);
};
var appKey = '2Ze1dz'; // CHANGE THIS FOR YOUR APPLICATION KEY
var token = 'myAuthenticationToken'; // THIS DEMO APPLICATION KEY HAS NO AUTH SO ANY TOKEN WILL DO
realtimeClient.connect(appKey, token);
}
}
});
</script>
</body>
</html>
var OrtcNodeclient = require('ibtrealtimesjnode').IbtRealTimeSJNode;
var connectionUrl = 'http://ortc-developers.realtime.co/server/2.1';
var appKey = '2Ze1dz'; // CHANGE THIS FOR YOUR APPLICATION KEY
var authToken = 'AUTHENTICATION_TOKEN'; // THIS DEMO APPLICATION KEY HAS NO AUTH SO ANY TOKEN WILL DO
var isCluster = true;
var client = new OrtcNodeclient();
client.setClusterUrl(connectionUrl);
client.setConnectionMetadata("The server metadata");
/***********************************************************
* Client callbacks
***********************************************************/
client.onConnected = clientConnected;
client.onReconnecting = clientReconnecting;
client.onReconnected = clientReconnected;
client.onDisconnected = clientDisconnected;
client.onException = clientException;
/***********************************************************
* Client methods
***********************************************************/
function onMessage(ortc, channel, message) {
log('Received: ' + message + ' at channel: ' + channel);
}
function clientConnected(ortc) {
log('Connected to: ' + ortc.getUrl());
// Subscribe announcement channels
ortc.subscribe("ortcClientConnected", true, onMessage);
ortc.subscribe("ortcClientDisconnected", true, onMessage);
ortc.subscribe("ortcClientSubscribed", true, onMessage);
ortc.subscribe("ortcClientUnsubscribed", true, onMessage);
};
function clientReconnecting(ortc) {
log('Reconnecting to ' + connectionUrl);
};
function clientReconnected(ortc) {
log('Reconnected to: ' + ortc.getUrl());
};
function clientDisconnected(ortc) {
log('Disconnected');
};
function clientException(ortc, error) {
log('Error: ' + error);
};
/***********************************************************
* Aux methods
***********************************************************/
function log(text, isSameLine) {
if (text) {
var currTime = new Date();
text = currTime + ' - ' + text;
}
if (isSameLine) {
process.stdout.write(text);
}
else {
console.log(text);
}
};
log('Connecting to ' + connectionUrl);
client.connect(appKey, authToken);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment