Created
May 27, 2016 23:45
-
-
Save mozkurt27/a1bd215b3829f23e1d8d87b038dc9f5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var messageContainer, submitButton; | |
var pseudo = ""; | |
$(function() { | |
messageContainer = $('#messageInput'); | |
submitButton = $("#submit"); | |
bindButton(); | |
window.setInterval(time, 1000*10); | |
$("#alertPseudo").hide(); | |
$('#modalPseudo').modal('show'); | |
$("#pseudoSubmit").click(function() {setPseudo()}); | |
$("#chatEntries").slimScroll({height: '600px'}); | |
submitButton.click(function() {sentMessage();}); | |
setHeight(); | |
$('#messageInput').keypress(function (e) { | |
if (e.which == 13) {sentMessage();}}); | |
}); | |
var socket = io.connect(); | |
socket.on('connect', function() { | |
console.log('Bağlandı'); | |
}); | |
socket.on('nbUsers', function(msg) { | |
$("#nbUsers").html(msg.nb); | |
}); | |
socket.on('message', function(data) { | |
addMessage(data['message'], data['pseudo'], new Date().toISOString(), false); | |
console.log(data); | |
}); | |
function sentMessage() { | |
if (messageContainer.val() != "") | |
{ | |
if (pseudo == "") | |
{ | |
$('#modalPseudo').modal('show'); | |
} | |
else | |
{ //Mesajın en son ne zaman yazıldıgını gsteriyor | |
socket.emit('message', messageContainer.val()); | |
addMessage(messageContainer.val(),pseudo, new Date().toISOString(), true); | |
messageContainer.val(''); | |
submitButton.button('loading'); | |
} | |
} | |
} | |
function addMessage(msg, pseudo, date, self) { | |
if(self) var classDiv = "row message self"; | |
else var classDiv = "row message"; | |
$("#chatEntries").append('<div class="'+classDiv+'"><p class="infos"><span class="pseudo">'+pseudo+'</span>, <time class="date" title="'+date+'">'+date+'</time></p><p>' + msg + '</p></div>'); | |
time(); | |
} | |
function bindButton() { | |
submitButton.button('loading'); | |
messageContainer.on('input', function() { | |
if (messageContainer.val() == "") submitButton.button('loading'); | |
else submitButton.button('reset'); | |
}); | |
} | |
function setPseudo() { | |
if ($("#pseudoInput").val() != "") | |
{ | |
socket.emit('setPseudo', $("#pseudoInput").val()); | |
socket.on('pseudoStatus', function(data){ | |
if(data == "ok") | |
{ | |
$('#modalPseudo').modal('hide'); | |
$("#alertPseudo").hide(); | |
pseudo = $("#pseudoInput").val(); | |
} | |
else | |
{ | |
$("#alertPseudo").slideDown(); | |
} | |
}) | |
} | |
} | |
function time() { | |
$("time").each(function(){ | |
$(this).text($.timeago($(this).attr('title'))); | |
}); | |
} | |
function setHeight() { | |
$(".slimScrollDiv").height('603'); | |
$(".slimScrollDiv").css('overflow', 'visible') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment