-
-
Save v1p/4b8028d45e1d8c98412b to your computer and use it in GitHub Desktop.
WebSockHop as Angular Factory/Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('mymodule').service('WS', ['$timeout', '$rootScope', | |
function ($timeout, $rootScope) { | |
var _api = {}; //REDACTED - Object having the address of ws:// endpoint as a property | |
var _user = {}; //REDACTED - Logged in user object | |
var _identity = {}; //REDACTED - The application context | |
var _isSubscribed = false; | |
var _notifSupported = false; | |
var wsh = null; | |
if (window.Notification) { | |
console.log("Notifications are supported!"); | |
_notifSupported = true; | |
} | |
else { | |
_notifSupported = false; | |
console.log("Notifications are not supported for this Browser/OS version yet."); | |
} | |
this.init = function () { | |
if (_user) { | |
wsh = new WebSockHop(_api.subscribeUrl); | |
wsh.formatter = new WebSockHop.JsonFormatter(); | |
wsh.on('opened', function () { | |
console.log('WS Opened!'); | |
if (!_isSubscribed) { | |
subscribeToPushpin(_identity.name + '-all'); | |
var privateChannel = _identity.name + _user.username; | |
privateChannel = privateChannel.replace(/[\@\.]/g, '_'); | |
subscribeToPushpin(privateChannel); | |
_isSubscribed = true; | |
} | |
}); | |
wsh.on('message', function (response) { | |
console.log(response); | |
console.log(_notifSupported); | |
console.log(Notification.permission); | |
if (_notifSupported && Notification.permission == 'granted') { | |
generateNotification(response); | |
} else if (_notifSupported && Notification.permission !== 'granted') { | |
Notification.requestPermission(); | |
} else { | |
fallbackNotifications(response); | |
} | |
//broadcast event to clients | |
$rootScope.$broadcast('wsUpdate', response); | |
}); | |
wsh.formatter.pingRequest = {type: 'ping'}; | |
wsh.formatter.handlePong = function (message) { | |
console.log(message); | |
return (message == 'pong'); // return true if message was a pong | |
}; | |
if (_notifSupported) { | |
Notification.requestPermission(); | |
} | |
} | |
}; | |
this.subscribe = function (channel) { | |
subscribeToPushpin(channel); | |
}; | |
var subscribeToPushpin = function (channel) { | |
//subscribe to a channel | |
if (wsh) { | |
wsh.request({ | |
type: 'subscribe', | |
channel: channel | |
}, function (reply) { | |
console.log('Subscribed to - ' + channel); // This callback is never called! :( | |
}); | |
console.log('Subscribed to - ' + channel); | |
} | |
}; | |
//global check to close wsh cleanly | |
window.addEventListener('beforeunload', function (event) { | |
console.log('WebSocket connection closed at "unload"'); | |
if (wsh) wsh.close(); | |
}); | |
window.addEventListener('close', function (event) { | |
console.log('WebSocket connection closed at "close"'); | |
if (wsh) wsh.close(); | |
}); | |
var generateNotification = function (entity) { | |
console.log("Spawn Notif"); | |
var options = { | |
title: entity.title, | |
body: entity.text, | |
icon: _identity.settings.icon || null | |
} | |
var notification = new Notification(entity.title, options); | |
//notification.addEventListener('onclick', function(){ | |
// | |
//}); | |
} | |
var fallbackNotifications = function (entity) { | |
//TODO:Implement this | |
} | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment