waxmiguelito / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

chat #257

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. send a message on chat-example page

What is the expected output? What do you see instead?
the main.js script enters everytime I send a new message in a new loop of 
"chat.getMessages"

What version of the product are you using? On what operating system?
Mongoose 3.1 , Linux OpenSuse

Original issue reported on code.google.com by Virus0...@googlemail.com on 9 Jun 2011 at 7:27

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I fixed it like this:

// This file is part of Mongoose project, http://code.google.com/p/mongoose

var chat = {
  // Backend URL, string.
  // 'http://backend.address.com' or '' if backend is the same as frontend
  backendUrl: '',
  maxVisibleMessages: 10,
  errorMessageFadeOutTimeoutMs: 2000,
  errorMessageFadeOutTimer: null,
  lastMessageId: 0,
  getMessagesIntervalMs: 1000,
};

chat.normalizeText = function(text) {
  return text.replace('<', '<').replace('>', '>');
};

chat.refresh = function(data) {

  if(data === undefined)
    return;

  $.each(data, function(index, entry) {
    var row = $('<div>').addClass('message-row').appendTo('#mml');
    var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString();
    $('<span>')
      .addClass('message-timestamp')
      .html('[' + timestamp + ']')
      .prependTo(row);
    $('<span>')
      .addClass('message-user')
      .addClass(entry.user ? '' : 'message-user-server')
      .html(chat.normalizeText((entry.user || '[server]') + ':'))
      .appendTo(row);
    $('<span>')
      .addClass('message-text')
      .addClass(entry.user ? '' : 'message-text-server')
      .html(chat.normalizeText(entry.text))
      .appendTo(row);
    chat.lastMessageId = Math.max(chat.lastMessageId, entry.id) + 1;
  });

  // Keep only chat.maxVisibleMessages, delete older ones.
  while ($('#mml').children().length > chat.maxVisibleMessages) {
    $('#mml div:first-child').remove();
  }
};

chat.getMessages = function(enter_loop) {
  $.ajax({
    dataType: 'jsonp',
    url: chat.backendUrl + '/ajax/get_messages',
    data: {last_id: chat.lastMessageId},
    success: chat.refresh,
    error: function() {
    },
  });
  if(enter_loop)
    window.setTimeout('chat.getMessages(true)', chat.getMessagesIntervalMs);
};

chat.handleMenuItemClick = function(ev) {
  $('.menu-item').removeClass('menu-item-selected');  // Deselect menu buttons
  $(this).addClass('menu-item-selected');  // Select clicked button
  $('.main').addClass('hidden');  // Hide all main windows
  $('#' + $(this).attr('name')).removeClass('hidden');  // Show main window
};

chat.showError = function(message) {
  $('#error').html(message).fadeIn('fast');
  window.clearTimeout(chat.errorMessageFadeOutTimer);
  chat.errorMessageFadeOutTimer = window.setTimeout(function() {
      $('#error').fadeOut('slow');
  }, chat.errorMessageFadeOutTimeoutMs);
};

chat.handleMessageInput = function(ev) {
  var input = ev.target;
  if (ev.keyCode != 13 || !input.value)
    return;
  //input.disabled = true;
  $.ajax({
    dataType: 'jsonp',
    url: chat.backendUrl + '/ajax/send_message',
    data: {text: input.value},
    success: function(ev) {
      input.value = '';
      input.disabled = false;
      chat.getMessages(false);
    },
    error: function(ev) {
      chat.showError('Error sending message');
      input.disabled = false;
    },
  });
};

$(document).ready(function() {
  $('.menu-item').click(chat.handleMenuItemClick);
  $('.message-input').keypress(chat.handleMessageInput);
  chat.getMessages(true);
});

// vim:ts=2:sw=2:et

I also fixed a little bug in chat.refresh without that fix i got many errors 
something like "object not defined"

Original comment by Virus0...@googlemail.com on 9 Jun 2011 at 8:31

GoogleCodeExporter commented 9 years ago
Can you send a patch please?
It is hard to see what exactly was changed.

Original comment by valenok on 22 Sep 2012 at 3:13

GoogleCodeExporter commented 9 years ago

Original comment by Virus0...@googlemail.com on 23 Sep 2012 at 10:16

Attachments:

GoogleCodeExporter commented 9 years ago
Applied (commit ba6c01e), thank you!

Original comment by valenok on 23 Sep 2012 at 12:56