Support JSONP to allow Apache integration.
Esse commit está contido em:
@@ -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>
|
||||
@@ -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
@@ -1387,20 +1387,22 @@ AjaxIM.request = function(url, type, data, successFunc, failureFunc) {
|
||||
if(typeof failureFunc != 'function')
|
||||
failureFunc = function(){};
|
||||
|
||||
var jsonp = (url.substring(0, 1) !== '/');
|
||||
data['sessionid'] = cookies.get('sessionid');
|
||||
$.ajax({
|
||||
url: url,
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
dataType: jsonp? 'jsonp': 'json',
|
||||
type: type,
|
||||
cache: false,
|
||||
timeout: 299000,
|
||||
success: function(json, textStatus, xhr) {
|
||||
if('status' in xhr && xhr.status == '0') return;
|
||||
if(!jsonp && 'status' in xhr && xhr.status == '0') return;
|
||||
_dbg(json);
|
||||
successFunc(json);
|
||||
},
|
||||
complete: function(xhr, textStatus) {
|
||||
if(~errorTypes.indexOf(textStatus) || xhr.status == '0')
|
||||
if(!jsonp && (~errorTypes.indexOf(textStatus) || xhr.status == '0'))
|
||||
failureFunc(textStatus);
|
||||
}
|
||||
});
|
||||
|
||||
externo
+8
-8
@@ -4855,15 +4855,15 @@ jQuery.extend({
|
||||
success();
|
||||
complete();
|
||||
// Garbage collect
|
||||
window[ jsonp ] = undefined;
|
||||
// window[ jsonp ] = undefined;
|
||||
//
|
||||
// try {
|
||||
// delete window[ jsonp ];
|
||||
// } catch(e) {}
|
||||
|
||||
try {
|
||||
delete window[ jsonp ];
|
||||
} catch(e) {}
|
||||
|
||||
if ( head ) {
|
||||
head.removeChild( script );
|
||||
}
|
||||
// if ( head ) {
|
||||
// head.removeChild( script );
|
||||
// }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,29 @@ module.exports = function setupHub(options) {
|
||||
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.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) {
|
||||
next();
|
||||
|
||||
@@ -56,7 +56,7 @@ Hub.prototype.get = function(req, fn) {
|
||||
} else {
|
||||
this.auth.authenticate(req, o_.bind(function(data) {
|
||||
if(data) {
|
||||
var session = new User(req.sessionID, data);
|
||||
var session = new User(req, data);
|
||||
this.set(req.sessionID, session);
|
||||
|
||||
this.auth.friends(req, data, o_.bind(function(friends) {
|
||||
|
||||
@@ -2,8 +2,9 @@ var events = require('events'),
|
||||
packages = require('../../libs/packages'),
|
||||
o_ = require('../../libs/utils');
|
||||
|
||||
var User = module.exports = function(id, data) {
|
||||
this.id = id;
|
||||
var User = module.exports = function(req, data) {
|
||||
this.req = req;
|
||||
this.id = req.sessionID;
|
||||
this.connection = null;
|
||||
this.listeners = [];
|
||||
this.message_queue = [];
|
||||
@@ -60,6 +61,10 @@ User.prototype.send = function(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) {
|
||||
if(!message && typeof code != 'number') {
|
||||
callback = message;
|
||||
@@ -72,10 +77,11 @@ User.prototype._send = function(type, code, message, callback) {
|
||||
|
||||
if(type == 'connection' && this.connection) {
|
||||
this.connection.writeHead(code || 200, {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': message.length
|
||||
// 'Content-Type': 'application/json',
|
||||
'Content-Type': 'application/javascript',
|
||||
'Content-Length': this.addCallback(message).length
|
||||
});
|
||||
this.connection.end(message);
|
||||
this.connection.end(this.addCallback(message));
|
||||
} else {
|
||||
if(!this.listeners.length)
|
||||
return this.message_queue.push(arguments);
|
||||
@@ -84,10 +90,11 @@ User.prototype._send = function(type, code, message, callback) {
|
||||
this.listeners = [];
|
||||
while(conn = cx.shift()) {
|
||||
conn.writeHead(code || 200, {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': message.length
|
||||
// 'Content-Type': 'application/json',
|
||||
'Content-Type': 'application/javascript',
|
||||
'Content-Length': this.addCallback(message).length
|
||||
});
|
||||
conn.end(message);
|
||||
conn.end(this.addCallback(message));
|
||||
}
|
||||
if(callback) callback();
|
||||
}
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário