jsonp-polling.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var XHRPolling = require('./xhr-polling')
  2. , util = require(process.binding('natives').util ? 'util' : 'sys');
  3. JSONPPolling = module.exports = function(){
  4. XHRPolling.apply(this, arguments);
  5. };
  6. util.inherits(JSONPPolling, XHRPolling);
  7. JSONPPolling.prototype.getOptions = function(){
  8. return {
  9. timeout: null, // no heartbeats
  10. closeTimeout: 8000,
  11. duration: 20000
  12. };
  13. };
  14. JSONPPolling.prototype._onConnect = function(req, res){
  15. this._index = req.url.match(/\/([0-9]+)\/?$/).pop();
  16. XHRPolling.prototype._onConnect.call(this, req, res);
  17. };
  18. JSONPPolling.prototype._write = function(message){
  19. if (this._open){
  20. if (this.request.headers.origin && !this._verifyOrigin(this.request.headers.origin)){
  21. message = "alert('Cross domain security restrictions not met');";
  22. } else {
  23. message = "io.JSONP["+ this._index +"]._("+ JSON.stringify(message) +");";
  24. }
  25. this.response.writeHead(200, {'Content-Type': 'text/javascript; charset=UTF-8', 'Content-Length': Buffer.byteLength(message)});
  26. this.response.write(message);
  27. this.response.end();
  28. this._onClose();
  29. }
  30. };