summaryrefslogtreecommitdiff
path: root/proto.js
diff options
context:
space:
mode:
Diffstat (limited to 'proto.js')
-rw-r--r--proto.js90
1 files changed, 69 insertions, 21 deletions
diff --git a/proto.js b/proto.js
index 74fa3ab..73d9894 100644
--- a/proto.js
+++ b/proto.js
@@ -3,26 +3,16 @@
function get_appropriate_ws_url()
{
- var pcol;
- var u = document.URL;
-
- /*
- * We open the websocket encrypted if this page came on an
- * https:// url itself, otherwise unencrypted
- */
-
- if (u.substring(0, 5) == "https") {
- pcol = "wss://";
- u = u.substr(8);
- } else {
- pcol = "ws://";
- if (u.substring(0, 4) == "http")
- u = u.substr(7);
- }
-
- u = u.split('/');
-
- return pcol + u[0];
+ // From: https://gist.github.com/2428561
+ var parser = document.createElement('a');
+ parser.href = document.URL;
+
+ // We open the websocket encrypted if this page came on an
+ // https:// url itself, otherwise unencrypted
+ if(parser.protocol == "http:") parser.protocol = "ws:";
+ if(parser.protocol == "https:") parser.protocol = "wss:";
+
+ return parser.href;
}
var socket_task = new WebSocket(get_appropriate_ws_url(), "lws-task-protocol");
@@ -42,7 +32,7 @@ try {
socket_task.onmessage = function got_packet(msg) {
var messageEvent = new CustomEvent("messageEvent", {
detail: {
- message: msg.data,
+ message: "recv [" + msg.data + "]",
time: new Date(),
},
bubbles: true,
@@ -162,3 +152,61 @@ try {
alert('<p>Error' + exception + '</p>');
}
+function transmit(msg)
+{
+ //LogEvent(msg);
+ var messageEvent = new CustomEvent("messageEvent", {
+ detail: {
+ message: "send [" + msg + "]",
+ time: new Date(),
+ },
+ bubbles: true,
+ cancelable: true
+ });
+ document.dispatchEvent(messageEvent);
+
+ socket_task.send(msg);
+}
+
+function login(user, password)
+{
+ transmit("login "+user+" "+password);
+}
+
+function logout()
+{
+ transmit("logout");
+}
+
+function observe(id)
+{
+ transmit("observe "+id);
+}
+
+function unobserve(id)
+{
+ transmit("unobserve "+id);
+}
+
+function create(id, parent)
+{
+ transmit("create "+id+" "+parent);
+}
+
+
+function update(id, name, value)
+{
+ transmit("update "+id+" "+name+" "+value);
+}
+
+function remove(id)
+{
+ transmit("remove "+id);
+}
+
+function move(id, parent)
+{
+ transmit("move "+id+" "+parent);
+}
+
+