createHandle method

dynamic createHandle (
  1. {Callbacks callbacks}
)

Implementation

createHandle({Callbacks callbacks}) {
  if (!this.connected) {
    Janus.warn("Is the server down? (connected=false)");
    this.gatewayCallbacks.error("Is the server down? (connected=false)");
    return;
  }

  String plugin = callbacks.plugin;
  if (plugin == null) {
    Janus.error("Invalid plugin");
    callbacks.error("Invalid plugin");
    return;
  }

  String opaqueId = callbacks.opaqueId;
  String handleToken = callbacks.token != null ? callbacks.token : this.token;
  String transaction = Janus.randomString(12);
  Map<String, dynamic> request = {
    "janus": "attach",
    "plugin": plugin,
    "opaque_id": opaqueId,
    "transaction": transaction
  };
  if (handleToken != null) request["token"] = handleToken;
  if (this.apiSecret != null) request["apisecret"] = this.apiSecret;

  if (this.websockets) {
    this.transactions[transaction] = (json) {
      Janus.debug(json);
      if (json["janus"] != "success") {
        Janus.error("Ooops: " +
            json["error"]["code"].toString() +
            " " +
            json["error"]["reason"]);
        callbacks.error("Ooops: " +
            json["error"]["code"] +
            " " +
            json["error"]["reason"]);
        return;
      }
      int handleId = json["data"]["id"];
      Janus.log("Created handle: " + handleId.toString());
      // Initialise plugin
      Plugin pluginHandle = Plugin(
          session: this,
          plugin: plugin,
          handleId: handleId,
          handleToken: handleToken,
          callbacks: callbacks);

      this.pluginHandles[handleId.toString()] = pluginHandle;
      callbacks.success(pluginHandle);
    };

    request["session_id"] = this.sessionId;
    this.ws.send(jsonEncode(request));
    return;
  }

  GatewayCallbacks httpCallbacks = GatewayCallbacks();
  httpCallbacks.success = (json) {
    Janus.debug(json);
    if (json["janus"] != "success") {
      Janus.error("Ooops: " +
          json["error"]["code"].toString() +
          " " +
          json["error"]["reason"]);
      callbacks.error("Ooops: " +
          json["error"]["code"].toString() +
          " " +
          json["error"]["reason"]);
      return;
    }
    int handleId = json["data"]["id"];
    Janus.log("Created handle: " + handleId.toString());

    // Initialise plugin
    Plugin pluginHandle = Plugin(
        session: this,
        plugin: plugin,
        handleId: handleId,
        handleToken: handleToken,
        callbacks: callbacks);

    this.pluginHandles[handleId.toString()] = pluginHandle;
    callbacks.success(pluginHandle);
  };

  httpCallbacks.error = (textStatus, errorThrown) {
    Janus.error(textStatus + ":" + errorThrown); // FIXME
    if (errorThrown == "")
      callbacks.error(textStatus + ": Is the server down?");
    else
      callbacks.error(textStatus + ": " + errorThrown);
  };

  Janus.httpAPICall(
      this.server + "/" + this.sessionId.toString(),
      {
        'verb': 'POST',
        'withCredentials': this.withCredentials,
        'body': request
      },
      httpCallbacks);
}