mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-17 15:53:28 +02:00
49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
/**
|
|
* Copyright © 2024 contains code contributed by Orange SA, authors: Denis Barbaron - Licensed under the Apache license 2.0
|
|
**/
|
|
|
|
var io = require('socket.io-client').io
|
|
|
|
module.exports = function SocketFactory(
|
|
$rootScope
|
|
, VersionUpdateService
|
|
, AppState
|
|
) {
|
|
var websocketUrl = AppState.config.websocketUrl || ''
|
|
var socket = io(websocketUrl, {
|
|
reconnection: false, transports: ['websocket']
|
|
})
|
|
|
|
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('outdated', function() {
|
|
VersionUpdateService.open()
|
|
})
|
|
|
|
socket.on('socket.ip', function(ip) {
|
|
$rootScope.$apply(function() {
|
|
socket.ip = ip
|
|
})
|
|
})
|
|
|
|
return socket
|
|
}
|