diff options
author | lassulus <lassulus@googlemail.com> | 2013-11-06 18:22:33 +0100 |
---|---|---|
committer | lassulus <lassulus@googlemail.com> | 2013-11-06 18:22:33 +0100 |
commit | 401ca308d6f1c501788729433a9e3c58ddfab590 (patch) | |
tree | 6e59114100f09e5f6b9df25e85742869cf3fdbb8 | |
parent | 7e68e86baf90dfe91abae7c4589091f089f8565b (diff) |
webchat: now with /nick
-rw-r--r-- | webchat/hello_web.js | 24 | ||||
-rw-r--r-- | webchat/public/client.js | 25 |
2 files changed, 35 insertions, 14 deletions
diff --git a/webchat/hello_web.js b/webchat/hello_web.js index aa580652..6f658901 100644 --- a/webchat/hello_web.js +++ b/webchat/hello_web.js @@ -47,18 +47,24 @@ irc_client.on('message#krebs', function(from, message) { var echo = sockjs.createServer(); echo.on('connection', function(conn) { - var name = '['+conn.remoteAddress+':'+conn.remotePort+']'; + var origin = '['+conn.remoteAddress+':'+conn.remotePort+']'; Clients.push(conn); - Clients.broadcast({from: 'system', message: name + ' has joined'}) irc_client.say("#krebs", name + ' has joined'); -conn.write(JSON.stringify({from: 'system', message: 'hello'})) - conn.on('data', function(message) { - console.log('data:',message); + Clients.broadcast({from: 'system', message: origin + ' has joined'}) + conn.write(JSON.stringify({from: 'system', message: 'hello'})) + conn.on('data', function(data) { + console.log('data:',data); try { - var object = JSON.parse(message); - object.from = name + var object = JSON.parse(data); + if (/^\/nick\s+(.+)$/.test(object.message)) { + object.from = origin; + } else if (typeof object.nick === 'string') { + object.from = object.nick; + } else { + object.from = origin; + }; console.log(object.message); - irc_client.say("#krebs", name + ' → ' + object.message); + irc_client.say("#krebs", object.from + ' → ' + object.message); Clients.broadcast(object); } catch (error) { @@ -67,8 +73,8 @@ conn.write(JSON.stringify({from: 'system', message: 'hello'})) }); conn.on('close', function() { Clients.splice(Clients.indexOf(conn)); - Clients.broadcast({from: 'system', message: name + ' has quit'}) irc_client.say("#krebs", name + ' has quit'); + Clients.broadcast({from: 'system', message: origin + ' has quit'}) }); }); diff --git a/webchat/public/client.js b/webchat/public/client.js index 6bcb24cd..95b67ad0 100644 --- a/webchat/public/client.js +++ b/webchat/public/client.js @@ -1,7 +1,15 @@ -function replaceURLWithHTMLLinks(text) { +function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"<a href='$1'>$1</a>"); } +function setMaybeNick (input) { + var match = /^\/nick\s+(.+)$/.exec(input); + if (match) { + nick = match[1]; + } +} + +var nick; $(function connect() { sock = new SockJS('/echo'); @@ -34,14 +42,21 @@ $(function connect() { }); $(function() { - $("#input").keydown(function(e) { - if( e.keyCode === 13) { + $('#input').keydown(function(e) { + if (e.keyCode === 13) { e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); - sock.send(JSON.stringify({ + setMaybeNick($('#input').val()); + var sendObj = { message: $('#input').val(), - })); + }; + + if (typeof nick === 'string') { + sendObj.nick = nick; + }; + + sock.send(JSON.stringify(sendObj)); $('#input').val(''); return; } |