Support JSONP to allow Apache integration.

Esse commit está contido em:
Daniel Howard
2013-02-20 12:24:10 -08:00
commit d0afe8590d
7 arquivos alterados com 92 adições e 20 exclusões
+18
Ver Arquivo
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function uid(n){
var chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', out='';
for(var c=0; c < n; c++){
out += chars.substr(0|Math.random() * chars.length, 1);
}
return out;
}
document.cookie = "sessionid="+uid(40)+"; path=/";
</script>
</head>
<body>
cookie set
</body>
</html>
+23
Ver Arquivo
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Ajax IM</title>
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/md5.js" type="text/javascript"></script>
<script src="js/store.js" type="text/javascript"></script>
<script src="js/cookies.js" type="text/javascript"></script>
<script src="js/dateformat.js" type="text/javascript"></script>
<script src="js/im.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var u = window.location.href.split("/");
var p = 8000; // Node.js port
u = u[0]+"//"+u[2]+":"+p;
var im = AjaxIM.init({pollServer: u, theme: "themes/default"});
});
</script>
</head>
<body>
Hello.
</body>
</html>
+5 -3
Ver Arquivo
@@ -1387,20 +1387,22 @@ AjaxIM.request = function(url, type, data, successFunc, failureFunc) {
if(typeof failureFunc != 'function') if(typeof failureFunc != 'function')
failureFunc = function(){}; failureFunc = function(){};
var jsonp = (url.substring(0, 1) !== '/');
data['sessionid'] = cookies.get('sessionid');
$.ajax({ $.ajax({
url: url, url: url,
data: data, data: data,
dataType: 'json', dataType: jsonp? 'jsonp': 'json',
type: type, type: type,
cache: false, cache: false,
timeout: 299000, timeout: 299000,
success: function(json, textStatus, xhr) { success: function(json, textStatus, xhr) {
if('status' in xhr && xhr.status == '0') return; if(!jsonp && 'status' in xhr && xhr.status == '0') return;
_dbg(json); _dbg(json);
successFunc(json); successFunc(json);
}, },
complete: function(xhr, textStatus) { complete: function(xhr, textStatus) {
if(~errorTypes.indexOf(textStatus) || xhr.status == '0') if(!jsonp && (~errorTypes.indexOf(textStatus) || xhr.status == '0'))
failureFunc(textStatus); failureFunc(textStatus);
} }
}); });
+8 -8
Ver Arquivo
@@ -4855,15 +4855,15 @@ jQuery.extend({
success(); success();
complete(); complete();
// Garbage collect // Garbage collect
window[ jsonp ] = undefined; // window[ jsonp ] = undefined;
//
// try {
// delete window[ jsonp ];
// } catch(e) {}
try { // if ( head ) {
delete window[ jsonp ]; // head.removeChild( script );
} catch(e) {} // }
if ( head ) {
head.removeChild( script );
}
}; };
} }
+22
Ver Arquivo
@@ -14,7 +14,29 @@ module.exports = function setupHub(options) {
return; return;
} }
if (!(options.authentication.cookie in req.cookies)) {
if (options.authentication.cookie in req.query) {
req.cookies[options.authentication.cookie] = req.query[options.authentication.cookie];
} else if (options.authentication.cookie in req.body) {
req.cookies[options.authentication.cookie] = req.body[options.authentication.cookie];
}
}
if (!('callback' in req.cookies)) {
if ('callback' in req.query) {
req.cookies.callback = req.query.callback;
} else if ('callback' in req.body) {
req.cookies.callback = req.body.callback;
}
}
req.sessionID = req.cookies[options.authentication.cookie]; req.sessionID = req.cookies[options.authentication.cookie];
req.jsonpCallback = req.cookies.callback;
delete req.query[options.authentication.cookie];
delete req.body[options.authentication.cookie];
delete req.query.callback;
delete req.body.callback;
if(req.dev) { if(req.dev) {
next(); next();
+1 -1
Ver Arquivo
@@ -56,7 +56,7 @@ Hub.prototype.get = function(req, fn) {
} else { } else {
this.auth.authenticate(req, o_.bind(function(data) { this.auth.authenticate(req, o_.bind(function(data) {
if(data) { if(data) {
var session = new User(req.sessionID, data); var session = new User(req, data);
this.set(req.sessionID, session); this.set(req.sessionID, session);
this.auth.friends(req, data, o_.bind(function(friends) { this.auth.friends(req, data, o_.bind(function(friends) {
+15 -8
Ver Arquivo
@@ -2,8 +2,9 @@ var events = require('events'),
packages = require('../../libs/packages'), packages = require('../../libs/packages'),
o_ = require('../../libs/utils'); o_ = require('../../libs/utils');
var User = module.exports = function(id, data) { var User = module.exports = function(req, data) {
this.id = id; this.req = req;
this.id = req.sessionID;
this.connection = null; this.connection = null;
this.listeners = []; this.listeners = [];
this.message_queue = []; this.message_queue = [];
@@ -60,6 +61,10 @@ User.prototype.send = function(code, message, callback) {
this._send('listener', code, message, callback); this._send('listener', code, message, callback);
}; };
User.prototype.addCallback = function(message) {
return ((typeof this.req.jsonpCallback) != 'undefined')? this.req.jsonpCallback+'('+message+');': message;
};
User.prototype._send = function(type, code, message, callback) { User.prototype._send = function(type, code, message, callback) {
if(!message && typeof code != 'number') { if(!message && typeof code != 'number') {
callback = message; callback = message;
@@ -72,10 +77,11 @@ User.prototype._send = function(type, code, message, callback) {
if(type == 'connection' && this.connection) { if(type == 'connection' && this.connection) {
this.connection.writeHead(code || 200, { this.connection.writeHead(code || 200, {
'Content-Type': 'application/json', // 'Content-Type': 'application/json',
'Content-Length': message.length 'Content-Type': 'application/javascript',
'Content-Length': this.addCallback(message).length
}); });
this.connection.end(message); this.connection.end(this.addCallback(message));
} else { } else {
if(!this.listeners.length) if(!this.listeners.length)
return this.message_queue.push(arguments); return this.message_queue.push(arguments);
@@ -84,10 +90,11 @@ User.prototype._send = function(type, code, message, callback) {
this.listeners = []; this.listeners = [];
while(conn = cx.shift()) { while(conn = cx.shift()) {
conn.writeHead(code || 200, { conn.writeHead(code || 200, {
'Content-Type': 'application/json', // 'Content-Type': 'application/json',
'Content-Length': message.length 'Content-Type': 'application/javascript',
'Content-Length': this.addCallback(message).length
}); });
conn.end(message); conn.end(this.addCallback(message));
} }
if(callback) callback(); if(callback) callback();
} }