-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.html
More file actions
119 lines (102 loc) · 4.28 KB
/
index.html
File metadata and controls
119 lines (102 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!doctype html>
<html>
<head>
<title></title>
<script src="cordova.js" type="text/javascript"></script>
<script src="scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script type="text/javascript" src="http://messaging-public.realtime.co/js/2.1.0/ortc.js"></script>
<script type="text/javascript">
var loadScript = function (url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
};
</script>
<script type="text/javascript">
var ortcClient = null,
url = 'http://ortc-developers.realtime.co/server/2.1/',
applicationKey = 'YOUR_APPLICATION_KEY',
authenticationToken = 'YOUR_AUTHENTICATION_TOKEN',
channel = 'YOUR_CHANNEL';
// Log function
var Log = function (text) {
document.getElementById('taLog').value += "\n" + text;
}
// Sends a message
function send() {
var message = document.getElementById('txtMessage').value;
Log("Message sent to channel: " + channel + ": " + message);
ortcClient.send(channel, message);
};
// Displays a message received
var onMessage = function (client, channel, message) {
Log('Message received from channel ' + channel + ': ' + message);
};
// Creates the client and the connection
var createClient = function () {
loadOrtcFactory(IbtRealTimeSJType, function (factory, error) {
// Checks if we have successfuly created the factory
if (error != null) {
console.error(error);
}
else {
// Creates the factory
ortcClient = factory.createClient();
ortcClient.setClusterUrl(url);
// Callback for when we're connected
ortcClient.onConnected = function (ortc) {
Log("Connected to Realtime server " + ortcClient.getUrl());
Log("Transport used: " + ortcClient.getProtocol());
ortcClient.subscribe(channel, true, onMessage);
};
// Callback for when we're subscribed to a channel
ortcClient.onSubscribed = function (ortc, channel) {
Log("Subscribed channel " + channel);
};
// Callback for when we get an exception
ortcClient.onException = function (ortc, exception) {
Log('Exception: ' + exception);
};
// Connects to the ORTC server
Log("Connecting...");
ortcClient.connect(applicationKey, authenticationToken);
}
});
};
$(function () {
document.addEventListener("deviceready", function () {
var isAndroid = (navigator.userAgent.match(/Android/i)) == "Android" ? true : false;
if(isAndroid){
loadScript("scripts/phonegap-websocket.js",function(){
createClient();
});
}else{
createClient();
}
});
});
</script>
</head>
<body>
<div id="dMessage">
<input type="text" id="txtMessage" style="width: 100%" />
<input type="button" id="btnSendMessage" value="Send Message" onclick="send();" />
</div>
<div>
<textarea id="taLog" rows="2" cols="20" readonly="readonly" style="height: 300px; width: 100%"></textarea>
</div>
</body>
</html>