This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

chat_server.js

By: a guest on Jul 8th, 2012  |  syntax: JavaScript  |  size: 3.12 KB  |  views: 378  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var http = require('http').createServer(handler)
  2.   , io   = require('socket.io').listen(http).set('log level', 1);
  3.  
  4. http.listen(8080);
  5. console.log('Chatserver listening on port 8080');
  6.  
  7. var nicknames = {};
  8. var log = {};
  9.  
  10. function handler(req, res) {
  11.   res.writeHead(200);
  12.   res.end();
  13. }
  14.  
  15. function tstamp() {
  16.   var currentTime = new Date();
  17.   var days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat');
  18.   var day = currentTime.getDay();
  19.   var hours = currentTime.getHours();
  20.   var minutes = currentTime.getMinutes();
  21.   if (minutes < 10) {
  22.     minutes = "0" + minutes;
  23.   }
  24.   if (hours > 11) {
  25.     var ap = 'p';
  26.   }
  27.   else {
  28.     var ap = 'a';
  29.   }
  30.   if (hours > 12) {
  31.     hours = hours - 12;
  32.   }
  33.  
  34.   return "["+ days[day] + " " + hours + ":" + minutes + ap + "m] ";
  35. }
  36.  
  37. function linkify(inputText) {
  38.   //URLs starting with http://, https://, or ftp://
  39.   var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
  40.   var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
  41.  
  42.   //URLs starting with www. (without // before it, or it'd re-link the ones done above)
  43.   var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  44.   var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
  45.  
  46.   //Change email addresses to mailto:: links
  47.   var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
  48.   var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
  49.  
  50.   return replacedText
  51. }
  52.  
  53. function updateLog(type, nick, msg) {
  54.   var curTime = new Date();
  55.   if (typeof 'type' != 'undefined') {
  56.     log[curTime.getTime()] = {'type': type, 'nick': nick, 'msg': msg};
  57.   }
  58.   var i;
  59.   for (i in log) {
  60.     // Cull the log, removing entries older than a half hour.
  61.     if (i < (curTime.getTime() - 1800000)) {
  62.       delete log[i];
  63.     }
  64.   }
  65. }
  66.  
  67. io.sockets.on('connection', function (socket) {
  68.   socket.on('user message', function (msg) {
  69.     socket.broadcast.emit('user message', tstamp(), socket.nickname, linkify(msg));
  70.     updateLog('user message', socket.nickname, msg);
  71.   });
  72.  
  73.    socket.on('private message', function(targetUser,msg) {        
  74.         io.sockets.socket(targetUser).emit('private message', tstamp(), socket.nickname, linkify(msg));
  75.         updateLog('private message', socket.nickname, msg);
  76.    });
  77.    
  78.   socket.on('get log', function () {
  79.     updateLog(); // Ensure old entries are cleared out before sending it.
  80.     io.sockets.emit('chat log', log);
  81.   });
  82.  
  83.   socket.on('nickname', function (nick, fn) {
  84.     var i = 1;
  85.     var orignick = nick;
  86.     while (nicknames[nick]) {
  87.       nick = orignick+i;
  88.       i++;
  89.     }
  90.     fn(nick);
  91.     nicknames[nick] = socket.nickname = nick;
  92.     socket.broadcast.emit('announcement', nick + ' connected');
  93.     io.sockets.emit('nicknames', nicknames);
  94.   });
  95.  
  96.   socket.on('disconnect', function () {
  97.     if (!socket.nickname) return;
  98.  
  99.     delete nicknames[socket.nickname];
  100.     socket.broadcast.emit('announcement', socket.nickname + ' disconnected');
  101.     socket.broadcast.emit('nicknames', nicknames);
  102.   });
  103. });
clone this paste RAW Paste Data