createSession method
Implementation
createSession({GatewayCallbacks callbacks, bool reconnect = false}) {
String transaction = Janus.randomString(12);
Map<String, dynamic> request = {
"janus": "create",
"transaction": transaction
};
if (reconnect) {
// We're reconnecting, claim the session
connected = false;
request["janus"] = "claim";
request["session_id"] = this.sessionId;
// If we were using websockets, ignore the old connection
if (this.ws != null) {
this.ws.onMessage = null;
this.ws.onError = null;
this.ws.onClose = null;
}
}
if (this.token != null) request["token"] = token;
if (this.apiSecret != null) request["apisecret"] = apiSecret;
if (this.server != null && Janus.isArray(this.servers)) {
// We still need to find a working server from the list we were given
this.server = this.servers[this.serversIndex];
if (this.server.indexOf("ws") == 0) {
this.websockets = true;
Janus.log("Server #" +
(this.serversIndex + 1).toString() +
": trying WebSockets to contact Janus (" +
this.server +
")");
} else {
this.websockets = false;
Janus.log("Server #" +
(this.serversIndex + 1).toString() +
": trying REST API to contact Janus (" +
server +
")");
}
}
if (this.websockets) {
this.ws =
WebSocketWrapper(this.server, this.protocols, this.keepAlivePeriod);
// Attach websocket handlers
this.ws.onError = (int code, String reason) {
Janus.error(
"Error connecting to the Janus WebSockets server... " + server);
Janus.error(reason.toString());
if (Janus.isArray(this.servers) && !reconnect) {
this.serversIndex++;
if (this.serversIndex == this.servers.length) {
// We tried all the servers the user gave us and they all failed
callbacks.error(
"Error connecting to any of the provided Janus servers: Is the server down?");
return;
}
// Let's try the next server
this.server = null;
Timer(
Duration(microseconds: 200), createSession(callbacks: callbacks));
return;
}
callbacks.error(
"Error connecting to the Janus WebSockets server: Is the server down?");
};
this.ws.onMessage = (message) {
handleEvent(jsonDecode(message));
};
this.ws.onClose = (int code, String reason) {
if (this.server == null || !this.connected) {
} else {
this.connected = false;
// FIXME What if this is called when the page is closed?
gatewayCallbacks.error("Lost connection to the server (is it down?)");
}
};
// All set, now try to connect websocket
try {
this.ws.connect();
this.transactions[transaction] = (json) {
Janus.debug(json);
if (json["janus"] != "success") {
Janus.error("Ooops: " +
json["error"]["code"].toString() +
" " +
json["error"]["reason"]);
callbacks.error(json["error"]["reason"]);
return;
}
this.connected = true;
if (json["session_id"] != null) {
this.sessionId = json["session_id"];
} else {
this.sessionId = json["data"]["id"];
}
if (reconnect) {
Janus.log("Claimed session: " + this.sessionId.toString());
} else {
Janus.log("Created session: " + this.sessionId.toString());
}
Janus.sessions[this.sessionId.toString()] = this;
keepAlive();
callbacks.success(this.sessionId);
};
Janus.debug(request.toString());
this.ws.send(jsonEncode(request));
} catch (error) {
Janus.error(error.toString());
}
return;
}
GatewayCallbacks httpCallbacks = GatewayCallbacks();
httpCallbacks.success = (json) {
Janus.debug(json.toString());
if (json["janus"] != "success") {
Janus.error("Ooops: " +
json["error"]["code"] +
" " +
json["error"]["reason"]); // FIXME
callbacks.error(json["error"]["reason"]);
return;
}
this.connected = true;
if (json["session_id"] != null) {
this.sessionId = json["session_id"];
} else {
this.sessionId = json["data"]["id"];
}
if (reconnect) {
Janus.log("Claimed session: " + this.sessionId.toString());
} else {
Janus.log("Created session: " + this.sessionId.toString());
}
Janus.sessions[this.sessionId.toString()] = this;
eventHandler();
callbacks
.success(this.sessionId); // return session to the success callback
};
httpCallbacks.error = (textStatus, errorThrown) {
Janus.error(textStatus + ":", errorThrown); // FIXME
if (Janus.isArray(this.servers) && !reconnect) {
this.serversIndex++;
if (this.serversIndex == servers.length) {
// We tried all the servers the user gave us and they all failed
callbacks.error(
"Error connecting to any of the provided Janus servers: Is the server down?");
return;
}
// Let's try the next server
this.apiSecret = null;
Timer(Duration(microseconds: 200), createSession(callbacks: callbacks));
return;
}
if (errorThrown == "")
callbacks.error(textStatus + ": Is the server down?");
else
callbacks.error(textStatus + ": " + errorThrown);
};
Janus.httpAPICall(
server,
{
'verb': 'POST',
'withCredentials': this.withCredentials,
'body': request,
},
httpCallbacks);
}