summaryrefslogtreecommitdiffstats
path: root/webchat/public/client.js
blob: 8865fcc08092da68849a29e24f77f5f65137333d (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
var settings = {}

$(function updateTime () {
  $('#time').html(getCurTime());
  setTimeout(updateTime,'1000');
  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() {
  settings.sock = new SockJS('/echo');
  var transport = make_sockjs_client_transport(settings.sock)
  var rpc = new RPC(transport)
  rpc.register('msg', {type: 'string', nick: 'string', msg: 'string'}, function(params, callback) {
    var safe_message = $('<div/>').text(params.msg).html();
    safe_message = replaceURLWithHTMLLinks(safe_message);
    var safe_from = $('<div/>').text(params.nick).html();
    chatboxAppend(safe_from, safe_message, 'web_msg')
    return callback(null)
  })
  rpc.register('nick', {type: 'string', newnick: 'string', oldnick: 'string'}, function(params, callback) {
    var safe_oldnick = $('<div/>').text(params.oldnick).html();
    var safe_newnick = $('<div/>').text(params.newnick).html();
    var safe_type = $('<div/>').text(params.type).html();
    $(getNicklistElement(safe_oldnick,safe_type)).remove();
    $('#nicklist').append('<div class="'+safe_type+'_name">' + safe_newnick + '</div>') ;
    chatboxAppend(safe_oldnick, 'is now known as ' + safe_newnick, 'nick');
    return callback(null)
  })
  rpc.register('your_nick', {nick: 'string'}, function(params, callback) {
    var safe_nick = $('<div/>').text(params.nick).html();
    settings.nick = safe_nick
    return callback(null)
  })
  rpc.register('join', {type: 'string', nick: 'string'}, function(params, callback) {
    var safe_nick = $('<div/>').text(params.nick).html();
    var safe_type = $('<div/>').text(params.type).html();
    $('#nicklist').append('<div class="'+safe_type+'_name">' + safe_nick + '</div>') ;
    chatboxAppend(safe_nick, 'has joined');
    return callback(null)
  })
  rpc.register('part', {type: 'string', nick: 'string'}, function(params, callback) {
    var safe_nick = $('<div/>').text(params.nick).html();
    var safe_type = $('<div/>').text(params.type).html();
    $(getNicklistElement(safe_nick,safe_type)).remove();
    chatboxAppend(safe_nick, 'has parted');
    return callback(null)
  })
});
$(function() {
  $('#input').keydown(function(e) {
    if (e.keyCode === 13) {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      var input = ($('#input').val());
      $('#input').val('');

      var command = inputParser(input)
      return (commands[command.method] || commands.badcommand)(settings, command.params)
    }
  });

});