73
Ch
Chatroom / files
Chatroom / htdocs / js / script.js
1.6 KB
1/* Author: @xonecas 2 3*/ 4 5function send (ev) { 6 ev.preventDefault(); 7 var message = { text: $('#message').val(), author: window.nick, "color": window.color }; 8 socket.send(JSON.stringify(message)); 9 $('#message').val(""); 10} 11 12function getColor () { 13 var counter = 4, values = [], hex = '0123456789ABCDEF'; 14 while (--counter !== 0) { 15 values.push(hex.charAt(Math.floor(Math.random() * 16))); 16 } 17 return "#"+values.join(""); 18} 19 20var socket = new io.Socket('xonecas.com'); 21socket.connect(); 22//socket.on('connect', function() {}); 23socket.on('message', function(message) { 24 var style = (message.author === 'Raisa')? 25 'class="raisa"': (message.author === 'system')? 26 'style="color:#999;"': 27 'style="color:'+message.color+';"', 28 text = message.text.replace( 29 /(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, 30 '<a href="$&" target="_new">$&</a>'); 31 32 $('#chat li:first-child').before( 33 '<li><span '+style+'>'+message.author+"</span>: "+text+'</li>'); 34}); 35socket.on('disconnect', function(){ 36 socket.send(JSON.stringify({"leaving": window.nick})); 37}); 38 39$(document).ready(function () { 40 $('#go').click(function (ev) { 41 ev.preventDefault(); 42 43 window.nick = $('#nick').val() || "guest"+new Date().getTime(); 44 window.color = getColor(); 45 socket.send(JSON.stringify({"joined": nick})); 46 47 $('#user').fadeOut('fast').remove(); 48 $('#box').slideDown('fast'); 49 $('#chat').show(); 50 }); 51 52 $('#send').click(send); 53 $('#message').keydown(function (ev) { 54 if (ev.which === 13) send(ev); 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