diff options
-rw-r--r-- | webchat/index.js | 80 | ||||
-rw-r--r-- | webchat/public/client.js | 2 | ||||
-rw-r--r-- | webchat/public/commands.js | 2 | ||||
-rw-r--r-- | webchat/public/functions.js | 63 | ||||
-rw-r--r-- | webchat/public/reset.css | 1 |
5 files changed, 73 insertions, 75 deletions
diff --git a/webchat/index.js b/webchat/index.js index d3bd0a71..cf045912 100644 --- a/webchat/index.js +++ b/webchat/index.js @@ -1,34 +1,51 @@ +'use strict'; var fs = require('fs'); var http = require('https'); var sockjs = require('sockjs'); var connect = require('connect'); var irc = require('irc'); -var Clients = []; -Clients.broadcast = function(object) { //broadcast to all clients - Clients.forEach(function(client) { - client.write(JSON.stringify(object)); +var serverstate = { + connected = false, + nicks = [], + lastping = 0, +}; + +var clients = []; + +clients.notifyAll = function (method, params) { + var object = { + method: method, + params: params, + } + clients.forEach(function (client) { + client.conn.write(JSON.stringify(object)); }); } + var serverCommands = {}; serverCommands.say = function (settings, params) { - var nick = settings.nick || settings.conn.remoteAddress - var message = params.message + var nick = settings.nick + var message = params.msg + params.nick = nick irc_client.say("#krebs", nick + ' → ' + message); - return Clients.broadcast({ type: 'message', nick: nick, message: message }) + return clients.notifyAll('message', params) +// return clients.broadcast({ method: 'message', params: params }) + } serverCommands.nick = function (settings, params) { var oldnick = settings.nick || settings.conn.remoteAddress var newnick = params.nick settings.nick = newnick - return Clients.broadcast({ type: 'nickchange', nick: oldnick, newnick: newnick }) + return clients.notifyAll('nickchange', { nick: oldnick, newnick: newnick }); +// return clients.broadcast({ method: 'nickchange', params: { nick: oldnick, newnick: newnick }}) } serverCommands.badcommand = function (settings, params) { - settings.conn.write(JSON.stringify({ type: 'usererror', message: 'bad command' })) + settings.conn.write(JSON.stringify({ method: 'usererror', params: { message: 'bad command' }})) } var irc_reconnect = function() { //reconnt to irc @@ -47,7 +64,7 @@ var irc_client = new irc.Client('irc.freenode.net', 'kweb', { //create irc_clien userName: 'kweb', //todo: read from local_config realName: 'kweb', //todo: read from local_config password: fs.readFileSync(__dirname+'/local_config/irc.key').toString(), - debug: false, + debug: true, showErrors: true, floodProtection: true, port: 6697, @@ -65,24 +82,32 @@ irc_client.on('ping', function(server) { //restart timer on server ping irc_client.on('message#krebs', function(from, message) { console.log({ from: from, message: message }); - Clients.broadcast({ type: 'message', from: from, message: message }); //broadcast irc messages to all connected clients + clients.notifyAll('message', { nick: from, msg: message }); +// clients.broadcast({ method: 'message', params: {nick: from, msg: message} }); //broadcast irc messages to all connected clients clearTimeout(lastping); }); irc_client.on('names#krebs', function(nicks) { - Clients.broadcast({type: 'nicklist', message: nicks}); +// clients.broadcast({method: 'nicklist', params: { nicklist: nicks }}); + clients.notifyAll('nicklist', { nicklist: nicks }) }); irc_client.on('join#krebs', function(nick, msg) { if (nick !== 'kweb'){ - Clients.broadcast({type: 'join', from: nick}); + clients.notifyAll('join', { from: nick }) +// clients.broadcast({method: 'join', params: { from: nick }}); } }); irc_client.on('part#krebs', function(nick, rs, msg) { - Clients.broadcast({type: 'quit', from: nick}); + clients.notifyAll('quit', { from: nick }) +// clients.broadcast({method: 'quit', params: { from: nick }}); }); +irc_client.on('error', function (error) { + console.log('irc-client error', error) +}) + var web_serv_options = { //certificates for https key: fs.readFileSync(__dirname+'/local_config/server_npw.key'), cert: fs.readFileSync(__dirname+'/local_config/server.crt'), @@ -92,29 +117,35 @@ var echo = sockjs.createServer(); echo.on('connection', function(conn) { var origin = conn.remoteAddress; var settings = { - conn: conn + conn: conn, + addr: conn.remoteAddress, + nick: conn.remoteAddress } - Clients.push(conn); - Clients.broadcast({type: 'join', from: origin}) + clients.push(settings); + clients.notifyAll('join', { from: settings.nick }) +// clients.broadcast({method: 'join', params: { from: settings.nick }}) // irc_client.say("#krebs", origin + ' has joined'); if (typeof irc_client.chans['#krebs'] === 'object') { - conn.write(JSON.stringify({type: 'nicklist', message: irc_client.chans['#krebs'].users})); //send current nicklist + conn.write(JSON.stringify({method: 'nicklist', params: { nicklist: irc_client.chans['#krebs'].users }})); //send current nicklist }; - conn.write(JSON.stringify({type: 'message', from: 'system', message: 'hello' })) //welcome message + conn.write(JSON.stringify({method: 'message', params: { nick: 'system', msg: 'hello' }})) //welcome message console.log(irc_client.chans['#krebs']) conn.on('data', function(data) { console.log('data:',data); try { var command = JSON.parse(data); - return (serverCommands[command.method] || serverCommands.badcommand)(settings, command.params) - } catch (error) { console.log(error); } + if (!command || typeof command !== 'object') { + command = {} + } + return (serverCommands[command.method] || serverCommands.badcommand)(settings, command.params) }); conn.on('close', function() { //propagate if client quits the page - Clients.splice(Clients.indexOf(conn)); - Clients.broadcast({type: 'quit', from: origin}) + clients.splice(clients.indexOf(conn)); + clients.notifyAll('quit', { from: settings.nick }) +// clients.broadcast({method: 'quit', params: { from: origin }}) // irc_client.say("#krebs", origin + ' has quit'); }); }); @@ -125,12 +156,13 @@ var app = connect() .use(connect.static(__dirname+'/public')) .use( function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); - page_template='<!doctype html>\n'; + var page_template='<!doctype html>\n'; page_template+='<link rel="stylesheet" type="text/css" href="reset.css">\n'; page_template+='<script src="sockjs-0.3.min.js"></script>\n'; page_template+='<script src="jquery-2.0.3.min.js"></script>\n'; page_template+='<script src="commands.js"></script>\n'; page_template+='<script src="functions.js"></script>\n'; + page_template+='<script src="handler.js"></script>\n'; page_template+='<script src="client.js"></script>\n'; page_template+='<div id="bg">'; page_template+='<div id="chatter">'; diff --git a/webchat/public/client.js b/webchat/public/client.js index 4842d9c2..5e0202a1 100644 --- a/webchat/public/client.js +++ b/webchat/public/client.js @@ -17,7 +17,7 @@ $(function connect() { console.log('message', e.data); try { var object = JSON.parse(e.data); - console.log(object.message); + console.log(object); clientParser(object); } catch (error) { diff --git a/webchat/public/commands.js b/webchat/public/commands.js index 8cd3bba1..20197fbb 100644 --- a/webchat/public/commands.js +++ b/webchat/public/commands.js @@ -3,7 +3,7 @@ var commands = {} commands.say = function (settings, params) { var sendObj = { method: 'say', - params: { message: params }, + params: { msg: params }, }; sock.send(JSON.stringify(sendObj)) } diff --git a/webchat/public/functions.js b/webchat/public/functions.js index ddd3aad6..adcf3169 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -9,50 +9,22 @@ function inputParser (str) { function clientParser(object) { - console.log(object) - switch (object.type) { - case 'message': - return printMessage(object); - case 'join': - return handleJoin(object); - case 'quit': - return handleQuit(object); - case 'nicklist': - return handleNicklist(object); - case 'nickchange': - return handleNickchange(object); - } -}; - -function handleJoin(object) { - var safe_from = $('<div/>').text(object.from).html(); - $('<tr><td class="chat_date">'+getCurTime()+'</td><td class="chat_from">'+safe_from+'</td><td class="chat_msg" style="color:#00FF00">joined</td></tr>').insertBefore('#foot'); - $('#nicklist').append('<div class="name">' + safe_from + '</div>') ; + console.log('parser: ',object) + return (handler[object.method] || console.log)(object.params) +//switch (object.method) { +// case 'message': +// return handler.message(object.params); +// case 'join': +// return handler.join(object.params); +// case 'quit': +// return handler.quit(object.params); +// case 'nicklist': +// return handler.nicklist(object.params); +// case 'nickchange': +// return handler.nickchange(object.params); +//} }; -function handleQuit(object) { - var safe_from = $('<div/>').text(object.from).html(); - $('<tr><td class="chat_date">'+getCurTime()+'</td><td class="chat_from">'+safe_from+'</td><td class="chat_msg" style="color:#FF0000">quit</td></tr>').insertBefore('#foot'); - console.log('removing', safe_from); - $(getNicklistElement(safe_from)).remove(); -}; - -function handleNicklist(object) { - Object.keys(object.message).forEach(function (nick) { -// console.log('nick',nick); - var hash_from = btoa(nick).replace(/=/g,'_'); -// $('.name').each(function (i,e) { console.log(i,e); if (e.innerHTML === 'kweb') { $(e).attr("style", "color:green") } }) - $('#nicklist').append('<div class="name">' + nick + '</div>') ; - }); -}; - -function handleNickchange(object) { - var safe_from = $('<div/>').text(object.nick).html(); - var safe_newnick = $('<div/>').text(object.newnick).html(); - $('<tr><td class="chat_date">'+getCurTime()+'</td><td class="chat_from">'+safe_from+'</td><td class="chat_msg">is now known as '+object.newnick+'</td></tr>').insertBefore('#foot'); - $(getNicklistElement(safe_from)).remove(); - $('#nicklist').append('<div class="name">' + safe_from + '</div>') ; -}; function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; @@ -89,13 +61,6 @@ function chatboxAppend (chat_from, chat_msg, type) { elem.scrollTop = elem.scrollHeight; }; -function printMessage(object) { - var safe_message = $('<div/>').text(object.message).html(); - safe_message = replaceURLWithHTMLLinks(safe_message); - var safe_from = $('<div/>').text(object.nick).html(); - return chatboxAppend(safe_from, safe_message) -}; - function getCurTime () { date = new Date; h = date.getHours(); diff --git a/webchat/public/reset.css b/webchat/public/reset.css index 4139aca5..64ba963e 100644 --- a/webchat/public/reset.css +++ b/webchat/public/reset.css @@ -63,6 +63,7 @@ q:before, q:after { font-weight: bold; text-align: right; font-size:12px; + white-space: nowrap; } .chat_from:after { content: ":"; |