Permalink
Browse files

cleanup

  • Loading branch information...
1 parent defdec3 commit 6acdb845288a1f8cb6c7157d610ff61d405eabab Marvin Killing committed Jun 9, 2010
@@ -1,9 +0,0 @@
-require.paths.unshift(__dirname + '/vendor/js-oo/lib');
-require('oo');
-
-var sys = require('sys'),
- Listener = require('./socket.io/listener').Listener;
-
-this.listen = function(server, options){
- return new Listener(server, options);
-};
@@ -1,131 +0,0 @@
-var options = require('./util/options').options, urlparse = require('url').parse;
-
-exports.Client = Class({
-
- include: [options],
-
- options: {
- closeTimeout: 0,
- heartbeatInterval: 5000
- },
-
- init: function(listener, req, res, options, head){
- this.listener = listener;
- this.setOptions(options);
- this.connections = 0;
- this.connected = false;
- this.upgradeHead = head;
- this._onConnect(req, res);
- },
-
- send: function(message){
- if (!this.connected || !(this.connection.readyState === 'open' ||
- this.connection.readyState === 'writeOnly')) {
- return this._queue(message);
- }
-
- this._write(JSON.stringify({messages: [message]}));
- return this;
- },
-
- broadcast: function(message){
- if (!('sessionId' in this)) {
- return this;
- }
- this.listener.broadcast(message, this.sessionId);
- return this;
- },
-
- _onMessage: function(data){
- try {
- var messages = JSON.parse(data);
- } catch(e){
- return this.listener.options.log('Bad message received from client ' + this.sessionId);
- }
- for (var i = 0, l = messages.length; i < l; i++){
- this.listener._onClientMessage(messages[i], this);
- }
- },
-
- _onConnect: function(req, res){
- var self = this;
- this.request = req;
- this.response = res;
- this.connection = this.request.connection;
- if (this._disconnectTimeout) {
- clearTimeout(this._disconnectTimeout);
- }
- },
-
- _payload: function(){
- var payload = [];
-
- this.connections++;
- this.connected = true;
-
- if (!this.handshaked){
- this._generateSessionId();
- payload.push(JSON.stringify({
- sessionid: this.sessionId
- }));
- this.handshaked = true;
- }
-
- payload = payload.concat(this._writeQueue || []);
- this._writeQueue = [];
-
- if (payload.length) {
- this._write(JSON.stringify({messages: payload}));
- }
- if (this.connections === 1) {
- this.listener._onClientConnect(this);
- }
- },
-
- _onClose: function(){
- var self = this;
- if (this._heartbeatInterval) {
- clearInterval(this._heartbeatInterval);
- }
- this.connected = false;
- this._disconnectTimeout = setTimeout(function(){
- self._onDisconnect();
- }, this.options.closeTimeout);
- },
-
- _onDisconnect: function(){
- if (!this.finalized){
- this._writeQueue = [];
- this.connected = false;
- this.finalized = true;
- if (this.handshaked) {
- this.listener._onClientDisconnect(this);
- }
- }
- },
-
- _queue: function(message){
- if (!('_writeQueue' in this)) {
- this._writeQueue = [];
- }
- this._writeQueue.push(message);
- return this;
- },
-
- _generateSessionId: function(){
- if (this.sessionId) {
- return this.listener.options.log('This client already has a session id');
- }
- this.sessionId = Math.random().toString().substr(2);
- return this;
- },
-
- _verifyOrigin: function(origin){
- var parts = urlparse(origin), origins = this.listener.options.origins;
- return origins.indexOf('*:*') !== -1 ||
- origins.indexOf(parts.host + ':' + parts.port) !== -1 ||
- origins.indexOf(parts.host + ':*') !== -1 ||
- origins.indexOf('*:' + parts.port) !== -1;
- }
-
-});
@@ -1,119 +0,0 @@
-var url = require('url'),
- options = require('./util/options').options,
- Client = require('./client').Client,
- transports = {};
-
-var Listener = exports.Listener = Class({
-
- include: [process.EventEmitter.prototype, options],
-
- options: {
- origins: '*:*',
- resource: 'socket.io',
- transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
- transportOptions: {},
- log: function(message){
- require('sys').log(message);
- }
- },
-
- init: function(server, options){
- var self = this;
- process.EventEmitter.call(this);
- this.server = server;
- this.setOptions(options);
- this.clients = [];
- this.clientsIndex = {};
-
- var listeners = this.server.listeners('request');
- this.server.removeAllListeners('request');
-
- this.server.addListener('request', function(req, res){
- if (self.check(req, res)) return;
- for (var i = 0; i < listeners.length; i++) {
- listeners[i].call(this, req, res);
- }
- });
-
- this.server.addListener('upgrade', function(req, socket, head){
- if (!self.check(req, socket, true, head)){
- socket.destroy();
- }
- });
-
- this.options.transports.forEach(function(t){
- if (!(t in transports)){
- transports[t] = require('./transports/' + t)[t];
- if (transports[t].init) transports[t].init(this);
- }
- }, this);
-
- this.options.log('socket.io ready - accepting connections');
- },
-
- broadcast: function(message, except){
- for (var i = 0, l = this.clients.length; i < l; i++){
- if (this.clients[i] && (!except || [].concat(except).indexOf(this.clients[i].sessionId) == -1)){
- this.clients[i].send(message);
- }
- }
- return this;
- },
-
- check: function(req, res, httpUpgrade, head){
- var path = url.parse(req.url).pathname, parts, cn;
- if (path.indexOf('/' + this.options.resource) === 0){
- parts = path.substr(1).split('/');
- if (parts[2]){
- cn = this._lookupClient(parts[2]);
- if (cn){
- cn._onConnect(req, res);
- } else {
- req.connection.end();
- this.options.log('Couldnt find client with session id "' + parts[2] + '"');
- }
- } else {
- this._onConnection(parts[1], req, res, httpUpgrade, head);
- }
- return true;
- }
- return false;
- },
-
- _lookupClient: function(sid){
- return this.clientsIndex[sid];
- },
-
- _onClientConnect: function(client){
- if (!(client instanceof Client) || !client.sessionId){
- return this.options.log('Invalid client');
- }
- client.i = this.clients.length;
- this.clients.push(client);
- this.clientsIndex[client.sessionId] = client;
- this.options.log('Client '+ client.sessionId +' connected');
- this.emit('clientConnect', client);
- },
-
- _onClientMessage: function(data, client){
- this.emit('clientMessage', data, client);
- },
-
- _onClientDisconnect: function(client){
- this.clientsIndex[client.sessionId] = null;
- this.clients[client.i] = null;
- this.options.log('Client '+ client.sessionId +' disconnected');
- this.emit('clientDisconnect', client);
- },
-
- // new connections (no session id)
- _onConnection: function(transport, req, res, httpUpgrade, head){
- if (this.options.transports.indexOf(transport) === -1 || (httpUpgrade && !transports[transport].httpUpgrade)){
- httpUpgrade ? res.destroy() : req.connection.destroy();
- return this.options.log('Illegal transport "'+ transport +'"');
- }
- this.options.log('Initializing client with transport "'+ transport +'"');
- new transports[transport](this, req, res, this.options.transportOptions[transport], head);
- }
-
-});
@@ -1,25 +0,0 @@
-var websocket = require('./websocket').websocket,
- net = require('net'),
- listeners = [];
-
-exports.flashsocket = websocket.extend({});
-exports.flashsocket.httpUpgrade = true;
-exports.flashsocket.init = function(listener){
- listeners.push(listener);
-};
-
-net.createServer(function(socket){
- socket.write('<?xml version="1.0"?>\n');
- socket.write('<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n');
- socket.write('<cross-domain-policy>\n');
-
- listeners.forEach(function(l){
- [].concat(l.options.origins).forEach(function(origin){
- var parts = origin.split(':');
- socket.write('<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>\n');
- });
- });
-
- socket.write('</cross-domain-policy>\n');
- socket.end();
-}).listen(843);
@@ -1,50 +0,0 @@
-var Client = require('../client').Client,
- qs = require('querystring');
-
-exports.htmlfile = Client.extend({
-
- _onConnect: function(req, res){
- var self = this, body = '';
- switch (req.method){
- case 'GET':
- this.__super__(req, res);
- this.request.connection.addListener('close', function(){ self._onClose(); });
- this.response.useChunkedEncodingByDefault = true;
- this.response.shouldKeepAlive = true;
- this.response.writeHead(200, {
- 'Content-Type': 'text/html',
- 'Connection': 'keep-alive',
- 'Transfer-Encoding': 'chunked'
- });
- this.response.write('<html><body>' + new Array(244).join(' '));
- this.response.flush();
- this._payload();
- this._heartbeatInterval = setInterval(function(){
- self.response.write('<!-- heartbeat -->');
- self.response.flush();
- }, this.options.heartbeatInterval);
- break;
-
- case 'POST':
- req.addListener('data', function(message){
- body += message;
- });
- req.addListener('end', function(){
- try {
- var msg = qs.parse(body);
- self._onMessage(msg.data);
- } catch(e){}
- res.writeHead(200);
- res.write('ok');
- res.end();
- });
- break;
- }
- },
-
- _write: function(message){
- this.response.write('<script>parent.s._('+ message +', document);</script>');
- this.response.flush();
- }
-
-});
Oops, something went wrong.

0 comments on commit 6acdb84

Please sign in to comment.