Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Heart-Beat configurable #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: standard
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bower_components/
node_modules/
npm-debug.log
.idea/
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"grunt": "^1.0.1",
"grunt-contrib-uglify": "^2.0.0",
"grunt-file-exists": "^0.1.4",
"grunt-standard": "^2.0.0"
"grunt-standard": "^2.0.0",
"eslint": "3.14.0",
"eslint-config-standard": "6.2.1",
"eslint-plugin-promise": "3.4.0",
"eslint-plugin-standard": "2.0.1"
},
"scripts": {
"test": "grunt --verbose"
Expand Down
149 changes: 83 additions & 66 deletions src/ng-stomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,102 @@
* @license MIT
*/

/*global
angular, SockJS, Stomp */
/* global
angular, SockJS, Stomp */

(function () {
angular
.module('ngStomp', [])
.service('$stomp', [
'$rootScope', '$q',
function ($rootScope, $q) {
this.sock = null
this.stomp = null
this.debug = null
.provider('$stomp', [
function () {
var outgoingHeartbeat = 1000
var incomingHeartbeat = 1000

this.setDebug = function (callback) {
this.debug = callback
}
return {
setOutgoingHeartbeat: function (interval) {
outgoingHeartbeat = interval
},
setIncomingHeartbeat: function (interval) {
incomingHeartbeat = interval
},
$get: [
'$rootScope', '$q',
function ($rootScope, $q) {
this.sock = null
this.stomp = null
this.debug = null

this.connect = function (endpoint, headers, errorCallback, sockjsOpts) {
headers = headers || {}
sockjsOpts = sockjsOpts || {}
this.setDebug = function (callback) {
this.debug = callback
}

var dfd = $q.defer()
this.connect = function (endpoint, headers, errorCallback, sockjsOpts) {
headers = headers || {}
sockjsOpts = sockjsOpts || {}

this.sock = new SockJS(endpoint, null, sockjsOpts)
this.sock.onclose = function () {
if (angular.isFunction(errorCallback)) {
errorCallback(new Error('Connection broken'))
}
}
var dfd = $q.defer()

this.stomp = Stomp.over(this.sock)
this.stomp.debug = this.debug
this.stomp.connect(headers, function (frame) {
dfd.resolve(frame)
}, function (err) {
dfd.reject(err)
if (angular.isFunction(errorCallback)) {
errorCallback(err)
}
})
this.sock = new SockJS(endpoint, null, sockjsOpts)
this.sock.onclose = function () {
if (angular.isFunction(errorCallback)) {
errorCallback(new Error('Connection broken'))
}
}

return dfd.promise
}
this.stomp = Stomp.over(this.sock)
this.stomp.heartbeat.outgoing = outgoingHeartbeat
this.stomp.heartbeat.incoming = incomingHeartbeat
this.stomp.debug = this.debug
this.stomp.connect(headers, function (frame) {
dfd.resolve(frame)
}, function (err) {
dfd.reject(err)
if (angular.isFunction(errorCallback)) {
errorCallback(err)
}
})

this.disconnect = function () {
var dfd = $q.defer()
this.stomp.disconnect(dfd.resolve)
return dfd.promise
}
return dfd.promise
}

this.subscribe = this.on = function (destination, callback, headers) {
headers = headers || {}
return this.stomp.subscribe(destination, function (res) {
var payload = null
try {
payload = JSON.parse(res.body)
} finally {
if (callback) {
callback(payload, res.headers, res)
this.disconnect = function () {
var dfd = $q.defer()
this.stomp.disconnect(dfd.resolve)
return dfd.promise
}
}
}, headers)
}

this.unsubscribe = this.off = function (subscription) {
subscription.unsubscribe()
}
this.subscribe = this.on = function (destination, callback, headers) {
headers = headers || {}
return this.stomp.subscribe(destination, function (res) {
var payload = null
try {
payload = JSON.parse(res.body)
} finally {
if (callback) {
callback(payload, res.headers, res)
}
}
}, headers)
}

this.send = function (destination, body, headers) {
var dfd = $q.defer()
try {
var payloadJson = JSON.stringify(body)
headers = headers || {}
this.stomp.send(destination, headers, payloadJson)
dfd.resolve()
} catch (e) {
dfd.reject(e)
}
return dfd.promise
this.unsubscribe = this.off = function (subscription) {
subscription.unsubscribe()
}

this.send = function (destination, body, headers) {
var dfd = $q.defer()
try {
var payloadJson = JSON.stringify(body)
headers = headers || {}
this.stomp.send(destination, headers, payloadJson)
dfd.resolve()
} catch (e) {
dfd.reject(e)
}
return dfd.promise
}
} ]
}
}]
)
} ]
)
})()