summaryrefslogtreecommitdiffstats
path: root/webchat/public
diff options
context:
space:
mode:
Diffstat (limited to 'webchat/public')
-rw-r--r--webchat/public/client.js42
-rw-r--r--webchat/public/commands.js5
-rw-r--r--webchat/public/functions.js13
-rw-r--r--webchat/public/handler.js10
4 files changed, 53 insertions, 17 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
diff --git a/webchat/public/commands.js b/webchat/public/commands.js
index 20197fbb..5a570556 100644
--- a/webchat/public/commands.js
+++ b/webchat/public/commands.js
@@ -5,16 +5,15 @@ commands.say = function (settings, params) {
method: 'say',
params: { msg: params },
};
- sock.send(JSON.stringify(sendObj))
+ settings.sock.send(JSON.stringify(sendObj))
}
commands.nick = function (settings, params) {
- settings.nick = params
var sendObj = {
method: 'nick',
params: { nick: params },
}
- sock.send(JSON.stringify(sendObj))
+ settings.sock.send(JSON.stringify(sendObj))
}
commands.badcommand = function (settings, params) {
diff --git a/webchat/public/functions.js b/webchat/public/functions.js
index b669a928..45c8ad3f 100644
--- a/webchat/public/functions.js
+++ b/webchat/public/functions.js
@@ -8,9 +8,18 @@ function inputParser (str) {
}
-function clientParser(object) {
+function methodDispatcher (settings, object) {
console.log('parser: ',object)
- return (handler[object.method] || console.log)(object.params)
+ return (handler[object.method] || console.log)(settings, object.params)
+};
+
+function resultDispatcher (settings, object) {
+ console.log('parser: ',object)
+ var callback = settings.waiting_callbacks[object.id]
+ delete settings.waiting_callbacks[object.id]
+ if (typeof callback === 'function') {
+ callback(object.error, object.result)
+ }
};
diff --git a/webchat/public/handler.js b/webchat/public/handler.js
index 4c13ba80..0930646d 100644
--- a/webchat/public/handler.js
+++ b/webchat/public/handler.js
@@ -1,32 +1,32 @@
var handler = {}
-handler.message = function(object) {
+handler.message = function(settings, object) {
var safe_message = $('<div/>').text(object.msg).html();
safe_message = replaceURLWithHTMLLinks(safe_message);
var safe_from = $('<div/>').text(object.nick).html();
return chatboxAppend(safe_from, safe_message, 'msg')
};
-handler.join = function(object) {
+handler.join = function(settings, object) {
var safe_from = $('<div/>').text(object.from).html();
$('#nicklist').append('<div class="name">' + safe_from + '</div>') ;
return chatboxAppend(safe_from, 'joined', 'join')
};
-handler.quit = function(object) {
+handler.quit = function(settings, object) {
var safe_from = $('<div/>').text(object.from).html();
$(getNicklistElement(safe_from)).remove();
return chatboxAppend(safe_from, 'quit', 'quit')
};
-handler.nicklist = function(object) {
+handler.nicklist = function(settings, object) {
Object.keys(object.nicklist).forEach(function (nick) {
var hash_from = btoa(nick).replace(/=/g,'_');
$('#nicklist').append('<div class="name">' + nick + '</div>') ;
});
};
-handler.nickchange = function(object) {
+handler.nickchange = function(settings, object) {
var safe_from = $('<div/>').text(object.nick).html();
var safe_newnick = $('<div/>').text(object.newnick).html();
$(getNicklistElement(safe_from)).remove();