Use Socket.IO instead of HTTP when possible.
Esse commit está contido em:
+51
-39
@@ -58,6 +58,12 @@ AjaxIM = function(options, actions) {
|
||||
self.socket = null;
|
||||
$.getScript(this.settings.pollServer+'/socket.io/socket.io.js', function(){
|
||||
self.socket = io(self.settings.pollServer);
|
||||
self.socket.on('client', function(event) {
|
||||
event = $.extend(true, {}, event);
|
||||
self.dispatchEvent(event);
|
||||
});
|
||||
var event = {type: 'hello', from: this.username, sessionID: cookies.get('sessionid')};
|
||||
self.sendEvent(event);
|
||||
});
|
||||
|
||||
// We load the theme dynamically based on the passed
|
||||
@@ -280,6 +286,7 @@ AjaxIM = function(options, actions) {
|
||||
$.extend(AjaxIM.prototype, {
|
||||
// == Main ==
|
||||
setup: function() {
|
||||
var self = this;
|
||||
$(this).trigger('loadComplete');
|
||||
|
||||
this.initTabBar();
|
||||
@@ -291,7 +298,6 @@ $.extend(AjaxIM.prototype, {
|
||||
if(this.username && store.get(this.username + '-offline') == true) {
|
||||
this.offline = true;
|
||||
|
||||
var self = this;
|
||||
setTimeout(function() { self._showReconnect(); }, 0);
|
||||
return;
|
||||
}
|
||||
@@ -299,7 +305,7 @@ $.extend(AjaxIM.prototype, {
|
||||
if(this.username)
|
||||
this.storage();
|
||||
|
||||
this.listen();
|
||||
setTimeout(function() { if (!self.socket) self.listen(); }, 2000);
|
||||
},
|
||||
|
||||
// === {{{AjaxIM.}}}**{{{storage()}}}** ===
|
||||
@@ -405,7 +411,9 @@ $.extend(AjaxIM.prototype, {
|
||||
self._parseMessage(response);
|
||||
}
|
||||
|
||||
setTimeout(function() { self.listen(); }, 0);
|
||||
if (!self.socket) {
|
||||
setTimeout(function() { self.listen(); }, 0);
|
||||
}
|
||||
},
|
||||
function(error) {
|
||||
self._notConnected();
|
||||
@@ -416,7 +424,9 @@ $.extend(AjaxIM.prototype, {
|
||||
? 1000
|
||||
: Math.min(self._reconnectIn * 2, 16000);
|
||||
self._lastReconnect = new Date();
|
||||
setTimeout(function() { self.listen(); }, self._reconnectIn);
|
||||
if (!self.socket) {
|
||||
setTimeout(function() { self.listen(); }, self._reconnectIn);
|
||||
}
|
||||
},
|
||||
this.actions.noop
|
||||
);
|
||||
@@ -934,9 +944,9 @@ $.extend(AjaxIM.prototype, {
|
||||
|
||||
$(this).trigger('sendingMessage', [username, body]);
|
||||
|
||||
var event = {type: 'message', to: username, body: body};
|
||||
var event = {type: 'message', from: this.username, to: username, body: body};
|
||||
this.sendEvent(event, function(result) {
|
||||
if(result._status.send) {
|
||||
if(result._status.sent) {
|
||||
$(self).trigger('sendMessageSuccessful', [username, body]);
|
||||
} else if(result.type == 'error') {
|
||||
if(result.error == 'not online')
|
||||
@@ -1351,46 +1361,48 @@ $.extend(AjaxIM.prototype, {
|
||||
failureFunc: failureFunc
|
||||
};
|
||||
this.unconfirmedEvents[event.id] = evt;
|
||||
if (this.socket) {
|
||||
this.socket.emit('server', event);
|
||||
} else {
|
||||
var self = this;
|
||||
var url = null;
|
||||
switch (event.type) {
|
||||
case 'message':
|
||||
url = this.actions.send;
|
||||
break;
|
||||
case 'status':
|
||||
url = this.actions.status;
|
||||
break;
|
||||
case 'signoff':
|
||||
url = this.actions.signoff;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
var url = null;
|
||||
switch (event.type) {
|
||||
case 'message':
|
||||
url = this.actions.send;
|
||||
break;
|
||||
case 'status':
|
||||
url = this.actions.status;
|
||||
break;
|
||||
case 'signoff':
|
||||
url = this.actions.signoff;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
AjaxIM.post(url, event,
|
||||
function(result) {
|
||||
if (result) {
|
||||
for (var e=0; e < result.length; ++e) {
|
||||
self.dispatchEvent(events[e]);
|
||||
AjaxIM.post(url, event,
|
||||
function(result) {
|
||||
if (result) {
|
||||
for (var e=0; e < result.length; ++e) {
|
||||
self.dispatchEvent(events[e]);
|
||||
}
|
||||
}
|
||||
},
|
||||
function(error) {
|
||||
if (self.unconfirmedEvents[event.id]) {
|
||||
event = self.unconfirmedEvents[event.id];
|
||||
event['_status']['sent'] = false;
|
||||
self.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
},
|
||||
function(error) {
|
||||
if (self.unconfirmedEvents[event.id]) {
|
||||
event = self.unconfirmedEvents[event.id];
|
||||
event['_status']['sent'] = false;
|
||||
self.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
dispatchEvent: function(event) {
|
||||
if (this.unconfirmedEvents[event.id]) {
|
||||
$.extend(event, this.unconfirmedEvents[event.id]);
|
||||
if (event.id && this.unconfirmedEvents[event.id]) {
|
||||
event['_status'] = $.extend({}, this.unconfirmedEvents[event.id]['_status'], event['_status']);
|
||||
delete this.unconfirmedEvents[event.id];
|
||||
console.log(JSON.stringify(event));
|
||||
if (event['_status']['sent']) {
|
||||
event['_status']['successFunc'](event);
|
||||
} else {
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@
|
||||
"express-error-handler": ">= 0.5.0",
|
||||
"morgan": ">= 1.0.0",
|
||||
"cookie": ">= 0.1.0",
|
||||
"jade": ">= 1.3.0"
|
||||
"jade": ">= 1.3.0",
|
||||
"socket.io": "^1.0.6"
|
||||
}
|
||||
}
|
||||
|
||||
+23
-3
@@ -2,6 +2,7 @@
|
||||
var express = require('express'),
|
||||
app = express(),
|
||||
http = require('http').Server(app),
|
||||
io = require('socket.io')(http),
|
||||
sys = require('sys'),
|
||||
packages = require('./libs/packages'),
|
||||
o_ = require('./libs/utils');
|
||||
@@ -13,11 +14,13 @@ try { o_.merge(global, require('./settings.local')); } catch(e) {}
|
||||
app.use(require('method-override')());
|
||||
app.use(require('cookie-parser')());;
|
||||
app.use(require('body-parser')());;
|
||||
app.use(require('./middleware/im')({
|
||||
maxAge: 15 * 60 * 1000,
|
||||
var mw = require('./middleware/im')({
|
||||
maxAge: 60 * 1000,
|
||||
reapInterval: 60 * 1000,
|
||||
authentication: require('./libs/authentication/' + AUTH_LIBRARY)
|
||||
}));
|
||||
});
|
||||
app.use(mw.session);
|
||||
var hub = mw.hub;
|
||||
|
||||
app.set('root', __dirname);
|
||||
|
||||
@@ -32,6 +35,23 @@ if ('development' == app.get('env')) {
|
||||
app.use(require('express-error-handler')({dumpExceptions: true, showStack: true}));
|
||||
}
|
||||
|
||||
io.on('connection', function(socket){
|
||||
socket.on('server', function(event) {
|
||||
if (event.type == 'hello') {
|
||||
event.socketio = socket;
|
||||
hub.get(event, function(err, sess) {
|
||||
sess.touch();
|
||||
store.set(event.sessionID, sess);
|
||||
});
|
||||
} else {
|
||||
hub.find(event.from, function(from) {
|
||||
from.socketio = socket;
|
||||
from.dispatch(hub, event);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
http.listen(APP_PORT, APP_HOST, function(){
|
||||
console.log('Ajax IM server started...');
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ module.exports = function setupHub(options) {
|
||||
|
||||
store = new Hub(options);
|
||||
|
||||
return function session(req, res, next) {
|
||||
return {hub: store, session: function session(req, res, next) {
|
||||
req.sessionStore = store;
|
||||
|
||||
if(!req.cookies) {
|
||||
@@ -89,5 +89,5 @@ module.exports = function setupHub(options) {
|
||||
type: 'error',
|
||||
error: 'not authenticated'})));
|
||||
}
|
||||
};
|
||||
}};
|
||||
};
|
||||
|
||||
@@ -63,6 +63,9 @@ Hub.prototype.get = function(req, fn) {
|
||||
this.auth.authenticate(req, o_.bind(function(data) {
|
||||
if(data) {
|
||||
var session = new User(req, data);
|
||||
if (req.socketio) {
|
||||
session.socketio = req.socketio;
|
||||
}
|
||||
this.set(req.sessionID, session);
|
||||
|
||||
this.auth.friends(req, data, o_.bind(function(friends) {
|
||||
@@ -115,7 +118,13 @@ Hub.prototype.message = function(res, to, event) {
|
||||
try {
|
||||
to.send(event);
|
||||
event._status = {sent: true};
|
||||
res.jsonp(event);
|
||||
if (res) {
|
||||
res.jsonp(event);
|
||||
} else {
|
||||
this.find(event.from, function(from) {
|
||||
from.socketio.emit('client', event);
|
||||
});
|
||||
}
|
||||
} catch(e) {
|
||||
event._status = {sent: false, e: e.description};
|
||||
res.jsonp(event);
|
||||
|
||||
@@ -76,7 +76,16 @@ User.prototype.send = function(event) {
|
||||
};
|
||||
|
||||
User.prototype._send = function(type, event, res) {
|
||||
if(type == 'connection') {
|
||||
if (this.socketio) {
|
||||
var id = event.id;
|
||||
if (event.id) {
|
||||
delete event.id;
|
||||
}
|
||||
this.socketio.emit('client', event);
|
||||
if (id) {
|
||||
event.id = id;
|
||||
}
|
||||
} else if(type == 'connection') {
|
||||
// end a regular connection with a response
|
||||
res.jsonp(event);
|
||||
} else {
|
||||
@@ -115,3 +124,12 @@ User.prototype.status = function(res, event) {
|
||||
res.jsonp(event);
|
||||
}
|
||||
};
|
||||
|
||||
User.prototype.dispatch = function(hub, event) {
|
||||
if (event.type == 'message') {
|
||||
hub.find(event.to, function(to) {
|
||||
hub.message(null, to, event);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário