From 46a8ed79f6afdca1d3e1ccebfb90eb3c1b5feb68 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 5 Dec 2013 15:35:01 +0100 Subject: Major brushup of javascript protocol handler. --- proto.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 21 deletions(-) (limited to 'proto.js') 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('

Error' + exception + '

'); } +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); +} + + -- cgit v1.2.3