Want more features on Pastebin? Sign Up, it's FREE!
Guest

chat_server.js

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