-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
66 lines (58 loc) · 1.68 KB
/
index.html
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
<html>
<script>
/***************************************************
This app is a web app AND a Jasonette agent,
simultaneously.
The only different part is where it checks for
"window.$agent" (if it's true, than it means
the app is being loaded as Jasonette agent.
Otherwise it's just a regular web app)
This means you can bring your existing web apps
to build a native Jasonette app on top of.
All you need to do is plug in the $agent methods
***************************************************/
var host = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(host)
ws.onopen = function () {
connected();
}
ws.onmessage = function (ev) {
received(ev.data);
}
var send = function(message) {
ws.send(message);
}
var received = function(message) {
if (typeof message !== "object") { // ignore pings
if (window.$agent) {
// Jasonette Agent
$agent.trigger("onmessage", message)
} else {
// Web App
var line = document.createElement("pre");
line.innerHTML = message;
document.querySelector("#messages").appendChild(line);
}
}
}
var connected = function() {
if (window.$agent) {
// Jasonette Agent
$agent.trigger("onconnect")
} else {
// Web App
var line = document.createElement("pre");
line.innerHTML = "websocket connected ...";
document.querySelector("#messages").appendChild(line);
}
}
document.addEventListener("DOMContentLoaded", function(e) {
document.querySelector("#send").onclick = function(c) {
send(document.querySelector("#message").value)
}
})
</script>
<input type='text' placeholder='enter message' id='message'>
<button id='send'>Send</button>
<div id='messages'></div>
</html>