diff options
author | lassulus <lassulus@googlemail.com> | 2013-11-11 20:16:45 +0100 |
---|---|---|
committer | lassulus <lassulus@googlemail.com> | 2013-11-11 20:16:45 +0100 |
commit | c6a7cd9027150fca42500b3bbc78be01993856f6 (patch) | |
tree | 0fbf009f8cbec49087f1621553a5fa4e92ce16dd /webchat/public/client.js | |
parent | 52ee854fa72607eb00229f5c251393ac392fd4f8 (diff) |
webchat: request handling
Diffstat (limited to 'webchat/public/client.js')
-rw-r--r-- | webchat/public/client.js | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/webchat/public/client.js b/webchat/public/client.js index 5e0202a1..8f7becb7 100644 --- a/webchat/public/client.js +++ b/webchat/public/client.js @@ -6,26 +6,54 @@ $(function updateTime () { return true; }); +var gensym = (function () { + var i = 0 + return function () { + return ++i; + } +})() + +settings.waiting_callbacks = {} + +function request (settings, method, params, callback) { + var id = gensym() + settings.waiting_callbacks[id] = callback + settings.sock.send({method: method, params: params, id: id}); +} + $(function connect() { - sock = new SockJS('/echo'); + settings.sock = new SockJS('/echo'); - sock.onopen = function() { + settings.sock.onopen = function() { console.log('open'); - sock.send('open'); + request(settings, 'coi', {}, function (error, result) { + if (error) { + console.log('coi error', error) + } else { + settings.nick = result.nick //TODO: write to display + settings.addr = result.addr //TODO: write to display + } + }) }; - sock.onmessage = function(e) { + settings.sock.onmessage = function(e) { console.log('message', e.data); try { var object = JSON.parse(e.data); console.log(object); - clientParser(object); } catch (error) { console.log(error); throw error; } - }; - sock.onclose = function(event) { + if (typeof object.method === 'string') { + return methodDispatcher(settings, object); + } else if (typeof object.result === 'string') { + return resultDispatcher(settings, object); + } else { + console.log('bad message:', object) + } + } + settings.sock.onclose = function(event) { console.log('close'); switch (event.code) { case 1006: //abnormal closure |