summaryrefslogtreecommitdiff
path: root/js/chat.js
blob: a2d8c0075712072e887af1446c2bbb08139131b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 = $('<div/>', {'class': 'chwin chshr'}).data('user', user);
      var $chhead = $('<div class="chhead"><span class="chtitle">'+title+'</span></div>');
      $child.append($chhead);
      $chhead.append($('<div/>', {'class': 'chshrbtn'}).click(function (e) {
        bullechat.gui.updown($child);
      }));
      $chhead.append($('<div/>', {'class': 'chxbtn'}).click(function (e) {
        bullechat.gui.kill($child);
      }));
      var $chbody = $('<ul/>', {'class': 'chlist'});
      $child.append($chbody);
      $child.data('body', $chbody);
      $child.append($('<input>', {'type': 'text', 'class': 'chinput', 'placeholder': 'press <enter> to send'}).keypress(function (e) {
        if (e.which == 13) {
          var $item = bullechat.gui.addline($child, $(this).val(), 'You', false);
          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, recvd) {
      var $item;
      if (nick) {
        $item = $('<li/>', {'class': (recvd ? 'recvd' : 'sent')}).text(msg);
      } else {
        $item = $('<li/>', {'class': 'chspecial'}).text(msg);
      }
      $elem.data('body').append($item);
      $elem.data('body').animate({scrollTop: $elem.data('body').scrollHeight}, 250);
      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.bulletinusa.com',
    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'], data['nick']);
      bullechat.gui.addline(bullechat.gui.$windows[data['uname']],
                            data['msg'], data['nick'], true);
    },
    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();
  }
});