
/**
 * Module dependencies.
 */

var Transport = require('../transport')
  , parser = require('engine.io-parser')
  , debug = require('debug')('engine:ws')

/**
 * Export the constructor.
 */

module.exports = WebSocket;

/**
 * WebSocket transport 
 *
 * @param {http.ServerRequest}
 * @api public
 */

function WebSocket (req) {
  Transport.call(this, req);
  var self = this;
  this.socket = req.websocket;
  this.socket.on('message', this.onData.bind(this));
  this.socket.once('close', this.onClose.bind(this));
  this.socket.on('error', this.onError.bind(this));
  this.socket.on('headers', function (headers) {
    self.emit('headers', headers);
  });
  this.writable = true;
};

/**
 * Inherits from Transport.
 */

WebSocket.prototype.__proto__ = Transport.prototype;

/**
 * Transport name
 *
 * @api public
 */

WebSocket.prototype.name = 'websocket';

/**
 * Advertise upgrade support.
 *
 * @api public
 */

WebSocket.prototype.handlesUpgrades = true;

/**
 * Advertise framing support.
 *
 * @api public
 */

WebSocket.prototype.supportsFraming = true;

/**
 * Processes the incoming data.
 *
 * @param {String} encoded packet
 * @api private
 */

WebSocket.prototype.onData = function (data) {
  debug('received "%s"', data);
  Transport.prototype.onData.call(this, data);
};

/**
 * Writes a packet payload.
 *
 * @param {Array} packets
 * @api private
 */
var isIOS;
WebSocket.prototype.send = function (data){
  function send(packets){
    for (var i = 0, l = packets.length; i < l; i++) {
      var data = parser.encodePacket(packets[i]);
      debug('writing "%s"', data);
      this.writable = false;
      var self = this;
      this.socket.send(data, function (err){
        if (err) return self.onError('write error', err.stack);
        self.writable = true;
        self.emit('drain');
      });
    }
  }
  // Due to a bug in the current iOS browser, we need to wrap the send in a
  // setTimeout, when they resume from sleeping the browser will crash if
  // we don't allow the browser time to detect the socket has been closed
  isIOS = (isIOS !== undefined) ? isIOS : 'undefined' != typeof navigator
    && /iPad|iPhone|iPod/i.test(navigator.userAgent)
  if (isIOS) {
    var self = this;
    setTimeout(function() {
      send.call(self, data);
    }, 0);
  } else {
    send.call(this, data);
  }
}

/**
 * Closes the transport.
 *
 * @api private
 */

WebSocket.prototype.doClose = function (fn) {
  debug('closing');
  this.socket.close();
  fn && fn();
};

@@ -1371,18 +1371,8 @@
  */%0A
-var isIOS;
 %0AWeb
@@ -1408,31 +1408,8 @@
 ion 
-(data)%7B%0A  function send
 (pac
@@ -1413,22 +1413,21 @@
 packets)
+ 
 %7B%0A  
- 
- 
 for (var
@@ -1468,26 +1468,24 @@
  i++) %7B%0A    
-  
 var data = p
@@ -1516,18 +1516,16 @@
 ts%5Bi%5D);%0A
-  
     debu
@@ -1549,26 +1549,24 @@
  data);%0A    
-  
 this.writabl
@@ -1576,18 +1576,16 @@
  false;%0A
-  
     var 
@@ -1601,18 +1601,16 @@
 is;%0A    
-  
 this.soc
@@ -1640,18 +1640,16 @@
  (err)%7B%0A
-  
       if
@@ -1704,26 +1704,24 @@
 ack);%0A      
- 
- 
 self.writabl
@@ -1732,26 +1732,24 @@
 true;%0A      
-  
 self.emit('d
@@ -1764,522 +1764,17 @@
     
- 
- %7D);%0A    %7D%0A  %7D%0A  // Due to a bug in the current iOS browser, we need to wrap the send in a%0A  // setTimeout, when they resume from sleeping the browser will crash if%0A  // we don't allow the browser time to detect the socket has been closed%0A  isIOS = (isIOS !== undefined) ? isIOS : 'undefined' != typeof navigator%0A    && /iPad%7CiPhone%7CiPod/i.test(navigator.userAgent)%0A  if (isIOS) %7B%0A    var self = this;%0A    setTimeout(function() %7B%0A      send.call(self, data);%0A    %7D, 0);%0A  %7D else %7B%0A    send.call(this, data
+%7D
 );%0A  %7D%0A%7D
 %0A%0A/*
@@ -1769,16 +1769,17 @@
 );%0A  %7D%0A%7D
+;
 %0A%0A/**%0A *

Revert "WebSocket setTimeout wrap when on iOS"