From 1a7fa60f6cb2ee26843ca19e37bb4ff44104ad55 Mon Sep 17 00:00:00 2001 From: Bulletin Date: Thu, 29 Dec 2016 16:42:32 -0500 Subject: Added existing progress --- js/chat.js | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 js/chat.js (limited to 'js/chat.js') diff --git a/js/chat.js b/js/chat.js new file mode 100644 index 0000000..1053fa9 --- /dev/null +++ b/js/chat.js @@ -0,0 +1,121 @@ +var bullechat = { + nickfmt: function (nick) { + return nick; + }, + gui: { + $windows: {}, + updown: function ($elem) { + if ($elem.hasClass('chexp')) { + $elem.addClass('chshr'); + $elem.removeClass('chexp'); + } else { + $elem.removeClass('chshr'); + $elem.addClass('chexp'); + } + }, + create: function (user, title) { + if (!title) title = user; + var $child = $('
', {'class': 'chwin chshr'}).data('user', user); + var $chhead = $('
'+title+'
'); + $child.append($chhead); + $chhead.append($('
', {'class': 'chshrbtn'}).click(function (e) { + bullechat.gui.updown($child); + })); + $chhead.append($('
', {'class': 'chxbtn'}).click(function (e) { + bullechat.gui.kill($child); + })); + var $chbody = $('
    ', {'class': 'chlist'}); + $child.append($chbody); + $child.data('body', $chbody); + $child.append($('', {'type': 'text', 'class': 'chinput', 'placeholder': 'press to send'}).keypress(function (e) { + if (e.which == 13) { + var $item = bullechat.gui.addline($child, $(this).val(), 'You'); + if (!bullechat.socket.sendto(user, $(this).val())) $item.addClass('chfailed').click(function (e) { + $(this).hide(750); + }); + $(this).val(''); + } + })); + + + $child.draggable({ + create: function (e, ui) { + $(this).css({ + position: 'fixed', + bottom: 0, + right: 0, + }); + } + }); + bullechat.gui.$windows[user] = $child; + $('body').append($child); + bullechat.gui.updown($child); + }, + addline: function ($elem, msg, nick) { + var $item; + if (nick) { + $item = $('
  • ').append($('',{'class':'chnick'}).text(bullechat.nickfmt(nick))).append(document.createTextNode(msg)); + } else { + $item = $('
  • ', {'class': 'chspecial'}).text(msg); + } + $elem.data('body').append($item); + return $item; + }, + end: function () { + $.each(bullechat.gui.$windows, function (k, v) { + bullechat.gui.kill(v); + }); + }, + kill: function (w) { + w.hide(250); + bullechat.gui.$windows[w.data('user')] = null; + }, + }, + socket: { + sockfd: null, + server: 'chat.bulletinalpha.tk', + port: 2442, + send: function (data) { + if (!bullechat.socket.sockfd) return false; + bullechat.socket.sockfd.send(JSON.stringify(data)); + return true; // implement testing to check if it went through + }, + sendto: function (user, msg) { + return bullechat.socket.send({'user': user, 'msg': msg}); + }, + opened: function () { + bullechat.socket.send(auth); + }, + closed: function () { + bullechat.gui.end(); + }, + error: function (e) { + console.error(e); + }, + message: function (e) { + var data = JSON.parse(e.data); + if (!data || !data['uname']) return; + if (!bullechat.gui.$windows[data['uname']]) bullechat.gui.create(data['uname']); + bullechat.gui.addline(bullechat.gui.$windows[data['uname']], + data['msg'], data['nick']); + }, + start: function () { + if (window.WebSocket) { + bullechat.socket.sockfd = new WebSocket('wss://'+bullechat.socket.server+':'+bullechat.socket.port+'/bullechat'); + bullechat.socket.sockfd.onopen = bullechat.socket.opened; + bullechat.socket.sockfd.onclose = bullechat.socket.closed; + bullechat.socket.sockfd.onerror = bullechat.socket.error; + bullechat.socket.sockfd.onmessage = bullechat.socket.message; + } + }, + }, + start: function () { + bullechat.socket.start(); + } +}; + +$(function () { + if (auth.id) { + bullechat.start(); + } +}); -- cgit v1.2.3