mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-11 20:13:28 +00:00
Add WS Connection object
This commit is contained in:
17
Gruntfile.js
17
Gruntfile.js
@@ -264,6 +264,21 @@ module.exports = function(grunt) {
|
|||||||
"dest": "<%= path.asset %>/requirejs/",
|
"dest": "<%= path.asset %>/requirejs/",
|
||||||
"flatten": true
|
"flatten": true
|
||||||
},
|
},
|
||||||
|
"sinonjs": {
|
||||||
|
"expand": true,
|
||||||
|
"src": "<%= path.bower %>/sinonjs/sinon.js",
|
||||||
|
"dest": "<%= path.asset %>/sinon",
|
||||||
|
"flatten": true
|
||||||
|
},
|
||||||
|
"sinon-chai": {
|
||||||
|
"expand": true,
|
||||||
|
"src": [
|
||||||
|
"<%= path.bower %>/sinon-chai/lib/sinon-chai.js",
|
||||||
|
"<%= path.bower %>/sinon-chai/LICENSE.txt"
|
||||||
|
],
|
||||||
|
"dest": "<%= path.asset %>/sinon-chai",
|
||||||
|
"flatten": true
|
||||||
|
},
|
||||||
"swfobject": {
|
"swfobject": {
|
||||||
"expand": true,
|
"expand": true,
|
||||||
"src": "<%= path.bower %>/swfobject/swfobject/swfobject.js",
|
"src": "<%= path.bower %>/swfobject/swfobject/swfobject.js",
|
||||||
@@ -393,6 +408,8 @@ module.exports = function(grunt) {
|
|||||||
"copy:modernizr",
|
"copy:modernizr",
|
||||||
"copy:normalize",
|
"copy:normalize",
|
||||||
"copy:requirejs",
|
"copy:requirejs",
|
||||||
|
"copy:sinonjs",
|
||||||
|
"copy:sinon-chai",
|
||||||
"copy:swfobject",
|
"copy:swfobject",
|
||||||
"copy:tinymce",
|
"copy:tinymce",
|
||||||
"copy:underscore",
|
"copy:underscore",
|
||||||
|
@@ -30,7 +30,9 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "latest",
|
"mocha": "latest",
|
||||||
|
"sinonjs": "~1.7.0",
|
||||||
"chai": "~1.6",
|
"chai": "~1.6",
|
||||||
|
"sinon-chai": "~2.5",
|
||||||
"qunit": "https://github.com/jquery/qunit.git#1.11.0",
|
"qunit": "https://github.com/jquery/qunit.git#1.11.0",
|
||||||
"js-fixtures": "https://github.com/badunk/js-fixtures/archive/master.zip"
|
"js-fixtures": "https://github.com/badunk/js-fixtures/archive/master.zip"
|
||||||
}
|
}
|
||||||
|
@@ -1,36 +1,71 @@
|
|||||||
define([
|
define([
|
||||||
"underscore"
|
"underscore",
|
||||||
], function (_) {
|
"backbone"
|
||||||
return function (options) {
|
], function (_, Backbone) {
|
||||||
if (!"url" in options) {
|
var instance;
|
||||||
throw "You must set the websocket 'url'"
|
|
||||||
}
|
|
||||||
if (!"topic" in options) {
|
|
||||||
throw "You must set the websocket 'topic'"
|
|
||||||
}
|
|
||||||
if (!"eventAggregator" in options) {
|
|
||||||
throw "You must set an event aggregator"
|
|
||||||
}
|
|
||||||
|
|
||||||
var eventAggregator = options.eventAggregator;
|
function init(url) {
|
||||||
|
var activeSession = null;
|
||||||
|
|
||||||
return {
|
return _.extend({
|
||||||
run: function() {
|
connect: function() {
|
||||||
|
if (this.hasSession()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var $this = this;
|
||||||
// autobahn js is defined as a global object there is no way to load
|
// autobahn js is defined as a global object there is no way to load
|
||||||
// it as a UMD module
|
// it as a UMD module
|
||||||
ab.connect(options.url, function (session) {
|
ab.connect(url, function (session) {
|
||||||
eventAggregator.trigger("ws:connect", session);
|
$this.setSession(session);
|
||||||
session.subscribe(options.topic, function (topic, msg) {
|
$this.trigger("ws:connect", activeSession);
|
||||||
// double encoded string
|
|
||||||
var msg = JSON.parse(JSON.parse(msg));
|
|
||||||
eventAggregator.trigger("ws:"+msg.event, msg, session);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
function (code, reason) {
|
function (code, reason) {
|
||||||
eventAggregator.trigger("ws:session-gone", code,reason);
|
$this.trigger("ws:session-gone", code, reason);
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
close: function() {
|
||||||
|
if (false === this.hasSession()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.getSession().close();
|
||||||
|
this.setSession(null);
|
||||||
|
this.trigger("ws:session-close");
|
||||||
|
},
|
||||||
|
hasSession: function() {
|
||||||
|
return this.getSession() !== null;
|
||||||
|
},
|
||||||
|
getSession: function() {
|
||||||
|
return activeSession;
|
||||||
|
},
|
||||||
|
setSession: function(session) {
|
||||||
|
activeSession = session;
|
||||||
|
},
|
||||||
|
subscribe: function(topic, callback) {
|
||||||
|
if (false === this.hasSession()) {
|
||||||
|
this.on("ws:connect", function(session) {
|
||||||
|
session.subscribe(topic, callback);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.getSession().subscribe(topic, callback);
|
||||||
|
this.trigger("ws:session-subscribe", topic);
|
||||||
|
},
|
||||||
|
unsubscribe: function(topic, callback) {
|
||||||
|
if (false === this.hasSession()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.getSession().unsubscribe(topic, callback);
|
||||||
|
this.trigger("ws:session-unsubscribe", topic);
|
||||||
}
|
}
|
||||||
}
|
}, Backbone.Events);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
getInstance: function(url) {
|
||||||
|
if (!instance) {
|
||||||
|
instance = init(url);
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="mocha"></div>
|
<div id="mocha"></div>
|
||||||
<script src="./../../../tmp-assets/mocha/mocha.js"></script>
|
<script src="./../../../tmp-assets/mocha/mocha.js"></script>
|
||||||
|
<script src="../../assets/sinon/sinon.js"></script>
|
||||||
<script src="../../assets/requirejs/require.js"></script>
|
<script src="../../assets/requirejs/require.js"></script>
|
||||||
<script src="../../scripts/tests/main.js"></script>
|
<script src="../../scripts/tests/main.js"></script>
|
||||||
<script>
|
<script>
|
||||||
@@ -17,7 +18,8 @@
|
|||||||
'specs/admin/taskmanager',
|
'specs/admin/taskmanager',
|
||||||
'specs/login/home',
|
'specs/login/home',
|
||||||
'specs/models',
|
'specs/models',
|
||||||
'specs/validator'
|
'specs/validator',
|
||||||
|
'specs/websockets/connection'
|
||||||
], runMocha);
|
], runMocha);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
@@ -9,7 +9,8 @@ require.config({
|
|||||||
underscore: "../assets/underscore-amd/underscore",
|
underscore: "../assets/underscore-amd/underscore",
|
||||||
backbone: "../assets/backbone-amd/backbone",
|
backbone: "../assets/backbone-amd/backbone",
|
||||||
i18n: "../assets/i18next/i18next.amd-1.6.3",
|
i18n: "../assets/i18next/i18next.amd-1.6.3",
|
||||||
bootstrap: "../assets/bootstrap/js/bootstrap.min"
|
bootstrap: "../assets/bootstrap/js/bootstrap.min",
|
||||||
|
sinonchai: "../assets/sinon-chai/sinon-chai"
|
||||||
},
|
},
|
||||||
shim: {
|
shim: {
|
||||||
bootstrap: ["jquery"],
|
bootstrap: ["jquery"],
|
||||||
|
74
www/scripts/tests/specs/websockets/connection.js
Normal file
74
www/scripts/tests/specs/websockets/connection.js
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
define([
|
||||||
|
'chai',
|
||||||
|
'sinonchai',
|
||||||
|
'underscore',
|
||||||
|
'common/websockets/connection'
|
||||||
|
], function (chai, sinonchai, _, connection) {
|
||||||
|
var expect = chai.expect;
|
||||||
|
var assert = chai.assert;
|
||||||
|
var should = chai.should();
|
||||||
|
chai.use(sinonchai);
|
||||||
|
|
||||||
|
describe("Connection", function () {
|
||||||
|
describe("Functionnal", function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
this.session = {"hello":"session"};
|
||||||
|
this.session.close = sinon.spy();
|
||||||
|
this.session.subscribe = sinon.spy();
|
||||||
|
this.session.unsubscribe = sinon.spy();
|
||||||
|
|
||||||
|
this.wsConnection = connection.getInstance();
|
||||||
|
var $this = this;
|
||||||
|
var cbSuccess = function (session) {
|
||||||
|
$this.wsConnection.setSession($this.session);
|
||||||
|
};
|
||||||
|
window.ab = {
|
||||||
|
connect: function(url, cbSuccess, cbError) {
|
||||||
|
cbSuccess($this.session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have a session", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
assert.ok(this.wsConnection.hasSession());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should retrieve the session", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
assert.equal(this.wsConnection.getSession().hello, this.session.hello);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should close the session", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
assert.ok(this.wsConnection.hasSession());
|
||||||
|
this.wsConnection.close();
|
||||||
|
assert.ok(!this.wsConnection.hasSession());
|
||||||
|
assert.equal(this.wsConnection.getSession(), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not connect anymore after first connect", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
ab.connect = sinon.spy();
|
||||||
|
this.wsConnection.connect();
|
||||||
|
this.wsConnection.connect();
|
||||||
|
this.wsConnection.connect();
|
||||||
|
expect(ab.connect.should.have.callCount(0)).to.be.ok;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call session subscribe once", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
this.wsConnection.subscribe();
|
||||||
|
expect(this.wsConnection.getSession().subscribe.should.have.callCount(1)).to.be.ok;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call session unsubscribe once", function () {
|
||||||
|
this.wsConnection.connect();
|
||||||
|
this.wsConnection.unsubscribe();
|
||||||
|
expect(this.wsConnection.getSession().unsubscribe.should.have.callCount(1)).to.be.ok;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user