diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-12-05 15:35:01 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-12-05 15:35:01 +0100 |
commit | 46a8ed79f6afdca1d3e1ccebfb90eb3c1b5feb68 (patch) | |
tree | 78b1313ca9cd41d65d5c189da244fb9cc3aaa5be /proto.js | |
parent | ace5f9ead8c72f74616f96f7fd396b77391247d9 (diff) |
Major brushup of javascript protocol handler.
Diffstat (limited to 'proto.js')
-rw-r--r-- | proto.js | 90 |
1 files changed, 69 insertions, 21 deletions
@@ -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); +} + + |