Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 15th, 2013  |  syntax: None  |  size: 1.14 KB  |  views: 34  |  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 express = require('express');
  2.  var app = express();
  3.  var http = require('http'),
  4.  mysql = require("mysql");
  5.  var server = http.createServer(app);
  6.  var io = require('socket.io').listen(server);
  7.  
  8. server.listen(8080);
  9.  
  10.  
  11. // routing
  12. app.get('/', function (req, res) {
  13.   res.sendfile(__dirname + '/index.html');
  14. });
  15.  
  16. // usernames which are currently connected to the chat
  17. var usernames = {};
  18. var SiteRooms = {};
  19.  
  20.  
  21.  
  22. var parseCookie = require('connect').utils.parseCookie;
  23.  
  24. io.sockets.on('connection', function (socket) {
  25.  
  26.         io.set('authorization', function (data, accept) {
  27.             if (data.headers.cookie) {
  28.                 data.cookie = parseCookie(data.headers.cookie);
  29.                 data.sessionID = data.cookie['express.sid'];
  30.             } else {
  31.                return accept('No cookie transmitted.', false);
  32.             }
  33.             accept(null, true);
  34.         });
  35.  
  36.         socket.on("private", function(data) {
  37.        
  38.                 if(data.agentName.length <= 0) {
  39.                         data.agentName = 'Besökare';
  40.                 }      
  41.                 io.sockets.in('Room_' + data.user_id).emit('updatechat', data.agentName, data.msg);
  42.                 console.log('A socket with sessionID ' + data.handshake.sessionID + ' connected!');
  43.     });
  44.  
  45. });