Permalink
Browse files

XhrPolling now inherits from WebSocket

  • Loading branch information...
1 parent 5a3f38e commit 4af0d25cdfec2eab14af304bdbe5277c1b6de4d5 @rmehner rmehner committed Oct 7, 2011
Showing with 10 additions and 48 deletions.
  1. +3 −0 transport/websocket.js
  2. +7 −48 transport/xhr_polling.js
View
3 transport/websocket.js
@@ -11,6 +11,7 @@ function WebSocket() {
this.connection = null;
this.port = null;
this.host = null;
+ this.transport = 'websocket';
}
WebSocket.create = function(port, host) {
@@ -24,6 +25,8 @@ WebSocket.create = function(port, host) {
WebSocket.prototype.connect = function() {
var self = this;
+ io.transports = [this.transport];
+
this.connection = io.connect(
'http://' + this.host + ':' + this.port,
{'force new connection': true}
View
55 transport/xhr_polling.js
@@ -1,53 +1,12 @@
-var io = require('socket.io-client');
-var EventEmitter = require('events').EventEmitter;
-var util = require('util');
-
+var util = require('util');
+var WebSocket = require('./websocket');
module.exports = XhrPolling;
-util.inherits(XhrPolling, EventEmitter);
-
-function XhrPolling() {
- EventEmitter.call(this);
-
- this.connection = null;
- this.port = null;
- this.host = null;
-}
-
-XhrPolling.create = function(port, host) {
- var instance = new this();
- instance.port = port;
- instance.host = host;
-
- return instance;
-}
-
-XhrPolling.prototype.connect = function() {
- var self = this;
- io.transports = ['xhr-polling'];
- this.connection = io.connect(
- 'http://' + this.host + ':' + this.port,
- {'force new connection': true}
- );
+util.inherits(XhrPolling, WebSocket);
-
- this.connection
- .on('connection', function() { self.emit('connection'); })
- .on('chat.msg', function(msg) { self.emit('message', {args: [msg]}); })
- .on('error', this.handleError.bind(this));
-
- this.connection.socket.on('error', this.handleError.bind(this));
-}
-
-XhrPolling.prototype.handleError = function(error) {
- var err = typeof error === 'string' ? new Error(error) : error;
- this.emit('error', err);
-}
-
-XhrPolling.prototype.send = function(message) {
- this.connection.emit('chat.send', message);
+function XhrPolling() {
+ WebSocket.call(this);
+ this.transport = 'xhr-polling';
}
-XhrPolling.prototype.disconnect = function() {
- this.connection.disconnect();
-}
+XhrPolling.create = WebSocket.create;

0 comments on commit 4af0d25

Please sign in to comment.