2014-06-23 16:47:08 +00:00
|
|
|
define [], () ->
|
|
|
|
class ConnectionManager
|
|
|
|
constructor: (@ide, @$scope) ->
|
2014-06-24 15:33:36 +00:00
|
|
|
@connected = false
|
|
|
|
|
|
|
|
@$scope.connection =
|
|
|
|
reconnecting: false
|
|
|
|
# If we need to force everyone to reload the editor
|
|
|
|
forced_disconnect: false
|
|
|
|
|
|
|
|
@$scope.tryReconnectNow = () =>
|
|
|
|
@tryReconnect()
|
|
|
|
|
2014-07-09 19:32:03 +00:00
|
|
|
@$scope.$on "editor:change", () =>
|
|
|
|
@lastUpdated = new Date()
|
|
|
|
|
2014-06-23 16:47:08 +00:00
|
|
|
@ide.socket = io.connect null,
|
|
|
|
reconnect: false
|
|
|
|
"force new connection": true
|
|
|
|
|
|
|
|
@ide.socket.on "connect", () =>
|
2014-06-24 15:33:36 +00:00
|
|
|
@connected = true
|
|
|
|
@ide.pushEvent("connected")
|
|
|
|
|
2014-06-23 16:47:08 +00:00
|
|
|
@$scope.$apply () =>
|
2014-06-24 15:33:36 +00:00
|
|
|
@$scope.connection.reconnecting = false
|
|
|
|
if @$scope.state.loading
|
2014-07-10 12:41:54 +00:00
|
|
|
@$scope.state.load_progress = 70
|
2014-06-24 15:33:36 +00:00
|
|
|
|
|
|
|
setTimeout(() =>
|
|
|
|
@joinProject()
|
|
|
|
, 100)
|
|
|
|
|
|
|
|
@ide.socket.on 'disconnect', () =>
|
|
|
|
@connected = false
|
|
|
|
@ide.pushEvent("disconnected")
|
|
|
|
|
|
|
|
@$scope.$apply () =>
|
|
|
|
@$scope.connection.reconnecting = false
|
|
|
|
|
|
|
|
setTimeout(=>
|
|
|
|
ga('send', 'event', 'editor-interaction', 'disconnect')
|
|
|
|
, 2000)
|
2014-06-23 16:47:08 +00:00
|
|
|
|
2014-06-24 15:33:36 +00:00
|
|
|
if !$scope.connection.forced_disconnect
|
|
|
|
@startAutoReconnectCountdown()
|
2014-06-23 16:47:08 +00:00
|
|
|
|
2014-06-24 15:33:36 +00:00
|
|
|
@ide.socket.on 'forceDisconnect', (message) =>
|
|
|
|
@$scope.$apply () =>
|
|
|
|
@$scope.connection.forced_disconnect = true
|
|
|
|
@socket.disconnect()
|
2014-06-23 16:47:08 +00:00
|
|
|
|
2014-06-24 15:33:36 +00:00
|
|
|
joinProject: () ->
|
|
|
|
@ide.socket.emit 'joinProject', {
|
|
|
|
project_id: @ide.project_id
|
|
|
|
}, (err, project, permissionsLevel, protocolVersion) =>
|
|
|
|
if @$scope.protocolVersion? and @$scope.protocolVersion != protocolVersion
|
|
|
|
location.reload(true)
|
2014-06-23 16:47:08 +00:00
|
|
|
|
2014-06-24 15:33:36 +00:00
|
|
|
@$scope.$apply () =>
|
|
|
|
@$scope.protocolVersion = protocolVersion
|
|
|
|
@$scope.project = project
|
2014-07-03 14:05:35 +00:00
|
|
|
@$scope.permissionsLevel = permissionsLevel
|
2014-06-24 15:33:36 +00:00
|
|
|
@$scope.state.load_progress = 100
|
|
|
|
@$scope.state.loading = false
|
2014-07-08 09:08:38 +00:00
|
|
|
@$scope.$broadcast "project:joined"
|
2014-06-24 14:31:44 +00:00
|
|
|
|
|
|
|
reconnectImmediately: () ->
|
|
|
|
@disconnect()
|
2014-06-24 15:33:36 +00:00
|
|
|
@tryReconnect()
|
2014-06-24 14:31:44 +00:00
|
|
|
|
|
|
|
disconnect: () ->
|
2014-06-24 15:33:36 +00:00
|
|
|
@ide.socket.disconnect()
|
|
|
|
|
|
|
|
startAutoReconnectCountdown: () ->
|
2014-06-24 15:56:31 +00:00
|
|
|
twoMinutes = 2 * 60 * 1000
|
2014-07-09 19:32:03 +00:00
|
|
|
if @lastUpdated? and new Date() - @lastUpdated > twoMinutes
|
2014-06-24 15:56:31 +00:00
|
|
|
# between 1 minute and 3 minutes
|
|
|
|
countdown = 60 + Math.floor(Math.random() * 120)
|
|
|
|
else
|
|
|
|
countdown = 3 + Math.floor(Math.random() * 7)
|
|
|
|
|
2014-06-24 15:33:36 +00:00
|
|
|
@$scope.$apply () =>
|
2014-07-02 15:41:29 +00:00
|
|
|
@$scope.connection.reconnecting = false
|
2014-06-24 15:56:31 +00:00
|
|
|
@$scope.connection.reconnection_countdown = countdown
|
2014-06-24 15:33:36 +00:00
|
|
|
|
|
|
|
setTimeout(=>
|
|
|
|
if !@connected
|
|
|
|
@timeoutId = setTimeout (=> @decreaseCountdown()), 1000
|
|
|
|
, 200)
|
|
|
|
|
|
|
|
cancelReconnect: () ->
|
|
|
|
clearTimeout @timeoutId if @timeoutId?
|
|
|
|
|
|
|
|
decreaseCountdown: () ->
|
2014-06-24 16:44:46 +00:00
|
|
|
return if !@$scope.connection.reconnection_countdown?
|
2014-06-24 15:33:36 +00:00
|
|
|
@$scope.$apply () =>
|
|
|
|
@$scope.connection.reconnection_countdown--
|
|
|
|
|
|
|
|
if @$scope.connection.reconnection_countdown <= 0
|
|
|
|
@$scope.$apply () =>
|
|
|
|
@tryReconnect()
|
|
|
|
else
|
|
|
|
@timeoutId = setTimeout (=> @decreaseCountdown()), 1000
|
|
|
|
|
|
|
|
tryReconnect: () ->
|
|
|
|
@cancelReconnect()
|
|
|
|
@$scope.connection.reconnecting = true
|
|
|
|
delete @$scope.connection.reconnection_countdown
|
|
|
|
@ide.socket.socket.reconnect()
|
2014-06-24 16:44:46 +00:00
|
|
|
setTimeout (=> @startAutoReconnectCountdown() if !@connected), 2000
|
2014-06-24 15:33:36 +00:00
|
|
|
|