summaryrefslogtreecommitdiffstats
path: root/webchat
diff options
context:
space:
mode:
authorlassulus <lassulus@googlemail.com>2013-11-11 20:16:45 +0100
committerlassulus <lassulus@googlemail.com>2013-11-11 20:16:45 +0100
commitc6a7cd9027150fca42500b3bbc78be01993856f6 (patch)
tree0fbf009f8cbec49087f1621553a5fa4e92ce16dd /webchat
parent52ee854fa72607eb00229f5c251393ac392fd4f8 (diff)
webchat: request handling
Diffstat (limited to 'webchat')
-rw-r--r--webchat/index.js2
-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
-rw-r--r--webchat/serverCommands.js11
6 files changed, 62 insertions, 21 deletions
diff --git a/webchat/index.js b/webchat/index.js
index d9d7ea87..52f41d1c 100644
--- a/webchat/index.js
+++ b/webchat/index.js
@@ -118,7 +118,7 @@ echo.on('connection', function(conn) {
if (!command || typeof command !== 'object') {
command = {}
}
- return (serverCommands[command.method] || serverCommands.badcommand)(serverstate, settings, command.params)
+ return (serverCommands[command.method] || serverCommands.badcommand)(serverstate, settings, command.params, command.id)
});
conn.on('close', function() { //propagate if client quits the page
clients.splice(clients.indexOf(conn));
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();
diff --git a/webchat/serverCommands.js b/webchat/serverCommands.js
index dd755a97..303c4805 100644
--- a/webchat/serverCommands.js
+++ b/webchat/serverCommands.js
@@ -1,5 +1,10 @@
var serverCommands = {};
-serverCommands.say = function (serverstate, settings, params) {
+
+serverCommands.coi = function (serverstate, settings, params, id) {
+ return settings.conn.write({ result: { nick: settings.nick, addr: settings.addr}, id: id})
+}
+
+serverCommands.say = function (serverstate, settings, params, id) {
var nick = settings.nick
var message = params.msg
params.nick = nick
@@ -7,14 +12,14 @@ serverCommands.say = function (serverstate, settings, params) {
return serverstate.clients.notifyAll('message', params)
}
-serverCommands.nick = function (serverstate, settings, params) {
+serverCommands.nick = function (serverstate, settings, params, id) {
var oldnick = settings.nick
var newnick = params.nick
settings.nick = newnick
return serverstate.clients.notifyAll('nickchange', { nick: oldnick, newnick: newnick });
}
-serverCommands.badcommand = function (serversate, settings, params) {
+serverCommands.badcommand = function (serversate, settings, params, id) {
settings.conn.write(JSON.stringify({ method: 'usererror', params: { message: 'bad command' }}))
}