destroySession method

dynamic destroySession (
  1. {GatewayCallbacks gatewayCallbacks,
  2. bool unload: true,
  3. bool notifyDestroyed: true,
  4. bool cleanupHandles: true}
)

Implementation

destroySession(
    {GatewayCallbacks gatewayCallbacks,
    bool unload = true,
    bool notifyDestroyed = true,
    bool cleanupHandles = true}) {
  // FIXME This method triggers a success even when we fail
  Janus.log("Destroying session " +
      sessionId.toString() +
      " (unload=" +
      unload.toString() +
      ")");
  if (this.sessionId == null) {
    Janus.warn("No session to destroy");
    if (gatewayCallbacks.success is Function) gatewayCallbacks.success();
    if (notifyDestroyed) if (gatewayCallbacks.destroyed is Function)
      gatewayCallbacks.destroyed();
    return;
  }
  if (cleanupHandles) {
    Callbacks callbacks = Callbacks();
    this.pluginHandles.forEach((handleId, handle) {
      callbacks.noRequest = true;
      handle.detach(callbacks);
    });
  }
  if (!this.connected) {
    Janus.warn("Is the server down? (connected=false)");
    this.sessionId = null;
    gatewayCallbacks.success();
    return;
  }
  // No need to destroy all handles first, Janus will do that itself
  Map<String, dynamic> request = {
    "janus": "destroy",
    "transaction": Janus.randomString(12)
  };
  if (this.token != null) request["token"] = token;
  if (this.apiSecret != null) request["apisecret"] = this.apiSecret;
  if (unload) {
    // We're unloading the page: use sendBeacon for HTTP instead,
    // or just close the WebSocket connection if we're using that
    if (this.websockets) {
      this.ws.onClose = null;
      this.ws.close();
      this.ws = null;
    } else {
      // navigator.sendBeacon(this.server + "/" + this.sessionId, jsonEncode(request));
      Janus.httpAPICall(
          this.server + "/" + this.sessionId.toString(),
          {
            'verb': 'POST',
            'withCredentials': this.withCredentials,
            'body': request
          },
          gatewayCallbacks);
    }
    Janus.log("Destroyed session:");
    this.sessionId = null;
    this.connected = false;
    if (gatewayCallbacks.success is Function) gatewayCallbacks.success();
    if (notifyDestroyed) if (gatewayCallbacks.destroyed is Function)
      gatewayCallbacks.destroyed();
    return;
  }
  if (this.websockets) {
    request["session_id"] = this.sessionId;
    var onUnbindMessage;
    var onUnbindError;
    var unbindWebSocket = () {
      // Detach websocket handlers
      this.ws.onError = null;
      this.ws.onOpen = null;
      this.ws.onMessage = null;
      this.ws.onClose = null;

      // TODO connect these calls
      // ws.removeEventListener('message', onUnbindMessage);
      // ws.removeEventListener('error', onUnbindError);

      if (this.wsKeepaliveTimeoutId != null) {
        this.wsKeepaliveTimeoutId.cancel();
      }
      this.ws.close();
    };

    onUnbindMessage = (event) {
      var data = jsonDecode(event.data);
      if (data['session_id'] == request['session_id'] &&
          data['transaction'] == request['transaction']) {
        unbindWebSocket();
        gatewayCallbacks.success();
        if (notifyDestroyed) gatewayCallbacks.destroyed();
      }
    };

    onUnbindError = (event) {
      unbindWebSocket();
      gatewayCallbacks
          .error("Failed to destroy the server: Is the server down?");
      if (notifyDestroyed) gatewayCallbacks.destroyed();
    };

    this.ws.onMessage = onUnbindMessage;
    this.ws.onError = onUnbindError;

    this.ws.send(jsonEncode(request));
    return;
  }

  GatewayCallbacks httpCallbacks = GatewayCallbacks();
  httpCallbacks.success = (json) {
    Janus.log("Destroyed session:");
    Janus.debug(json);
    this.sessionId = null;
    this.connected = false;
    if (json["janus"] != "success") {
      Janus.error("Ooops: " +
          json["error"]["code"].toString() +
          " " +
          json["error"]["reason"]);
    }
    gatewayCallbacks.success();
    if (notifyDestroyed) if (gatewayCallbacks.destroyed is Function)
      gatewayCallbacks.destroyed();
  };
  httpCallbacks.error = (textStatus, errorThrown) {
    Janus.error(textStatus + ":" + errorThrown); // FIXME
    // Reset everything anyway
    this.sessionId = null;
    this.connected = false;
    gatewayCallbacks.success();
    if (notifyDestroyed) if (gatewayCallbacks.destroyed is Function)
      gatewayCallbacks.destroyed();
  };
  Janus.httpAPICall(
      this.server + "/" + this.sessionId.toString(),
      {
        'verb': 'POST',
        'withCredentials': this.withCredentials,
        'body': request
      },
      httpCallbacks);
}