 
       # When the WebSocket closes, requests are no longer sent directly
       # but enqueued.
-      socket.addEventListener 'close', ->
-        call = enqueue
+      handleDisconnection = ->
+        disconnect()
+
+        notify.error {
+          message: """
+The connection with XO-Server has been lost.
+
+Attempt to reconnect in #{delay} seconds.
+"""
+        }
+
+        # Tries to reconnect after a small (increasing) delay.
+        console.log delay
+        $timeout connect, delay * 1e3
+        delay *= 2
+      socket.addEventListener 'close', handleDisconnection
+      # socket.addEventListener 'error', handleDisconnection
 
       # When a message is received, we call the corresponding
       # deferred (if any).


    # Identifier of the next request.
    nextId = 0

    # Promises linked to the requests.
    deferreds = {}

    }

  # This service provides an access to the Xen-Orchestra API.
  .service 'xoApi', ($location, $q, $timeout, notify) ->
    url = do ->
      # Note: The path is ignored, the WebSocket must be relative to
      # root.
