1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00
OpenSTF/res/app/components/stf/socket/socket-service.js

76 lines
1.5 KiB
JavaScript

var io = require('socket.io')
module.exports = function SocketFactory($rootScope) {
var socket = io.connect()
socket.scoped = function($scope) {
var listeners = []
$scope.$on('$destroy', function() {
listeners.forEach(function(listener) {
socket.removeListener(listener.event, listener.handler)
})
})
return {
on: function(event, handler) {
listeners.push({
event: event
, handler: handler
})
socket.on(event, handler)
return this
}
}
}
socket.on('connect', function() {
$rootScope.$apply(function() {
socket.state = 'connect'
})
})
socket.on('connecting', function() {
$rootScope.$apply(function() {
socket.state = 'connecting'
})
})
socket.on('disconnect', function() {
$rootScope.$apply(function() {
socket.state = 'disconnect'
})
})
socket.on('connect_failed', function() {
$rootScope.$apply(function() {
socket.state = 'connect_failed'
})
})
socket.on('error', function() {
$rootScope.$apply(function() {
socket.state = 'error'
})
})
socket.on('reconnect_failed', function() {
$rootScope.$apply(function() {
socket.state = 'reconnect_failed'
})
})
socket.on('reconnect', function() {
$rootScope.$apply(function() {
socket.state = 'reconnect'
})
})
socket.on('reconnecting', function() {
$rootScope.$apply(function() {
socket.state = 'reconnecting'
})
})
return socket
}