handleEvent method
dynamic
handleEvent
(- dynamic json,
- [dynamic skipTimeout]
)
Implementation
handleEvent(json, [skipTimeout]) {
retries = 0;
if (!this.websockets && this.sessionId != null && skipTimeout != true)
eventHandler();
if (!this.websockets && Janus.isArray(json)) {
// We got an array: it means we passed a maxev > 1, iterate on all objects
for (var i = 0; i < json.length; i++) {
handleEvent(json[i], true);
}
return;
}
if (json["janus"] == "keepalive") {
// Nothing happened
Janus.debug("Got a keepalive on session " + this.sessionId.toString());
return;
} else if (json["janus"] == "ack") {
// Just an ack, we can probably ignore
Janus.debug("Got an ack on session " + this.sessionId.toString());
Janus.debug(json);
String transaction = json["transaction"];
if (transaction != null) {
Function reportSuccess = this.transactions[transaction];
if (reportSuccess is Function) reportSuccess(json);
this.transactions.remove(transaction);
}
return;
} else if (json["janus"] == "success") {
// Success!
if (this.sessionId != null)
Janus.debug("Got a success on session " + this.sessionId.toString());
Janus.debug(json);
String transaction = json["transaction"];
if (transaction != null) {
Function reportSuccess = this.transactions[transaction];
if (reportSuccess is Function) reportSuccess(json);
this.transactions.remove(transaction);
}
return;
} else if (json["janus"] == "trickle") {
// We got a trickle candidate from Janus
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.debug("This handle is not attached to this session");
return;
}
Map<String, dynamic> candidateMap = json["candidate"];
Janus.debug(candidateMap.toString());
RTCIceCandidate candidate = RTCIceCandidate(candidateMap['candidate'],
candidateMap['sdpMid'], candidateMap['sdpMlineIndex']);
Janus.debug(
"Got a trickled candidate on session " + this.sessionId.toString());
Janus.debug(candidate.toMap());
if (pluginHandle.pc != null && pluginHandle.remoteSdp != null) {
// Add candidate right now
Janus.debug("Adding remote candidate:" + candidate.toString());
if (candidate == null ||
candidateMap['candidate']['completed'] == true) {
// end-of-candidates
pluginHandle.pc.addCandidate(Janus.endOfCandidates);
} else {
// New candidate
pluginHandle.pc.addCandidate(candidate);
}
} else {
// We didn't do setRemoteDescription (trickle got here before the offer?)
Janus.debug(
"We didn't do setRemoteDescription (trickle got here before the offer?), caching candidate");
pluginHandle.candidates.add(candidate);
Janus.debug(pluginHandle.candidates.toString());
}
} else if (json["janus"] == "webrtcup") {
// The PeerConnection with the server is up! Notify this
Janus.debug(
"Got a webrtcup event on session " + this.sessionId.toString());
Janus.debug(json);
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.debug("This handle is not attached to this session");
return;
}
pluginHandle.webrtcState(true);
return;
} else if (json["janus"] == "hangup") {
// A plugin asked the core to hangup a PeerConnection on one of our handles
Janus.debug("Got a hangup event on session " + this.sessionId.toString());
Janus.debug(json);
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.debug("This handle is not attached to this session");
return;
}
pluginHandle.webrtcState(false, json["reason"]);
pluginHandle.hangup(true);
} else if (json["janus"] == "detached") {
// A plugin asked the core to detach one of our handles
Janus.debug(
"Got a detached event on session " + this.sessionId.toString());
Janus.debug(json);
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
// Don't warn here because destroyHandle causes this situation.
return;
}
pluginHandle.detached = true;
pluginHandle.onDetached();
pluginHandle.detach(null);
} else if (json["janus"] == "media") {
// Media started/stopped flowing
Janus.debug("Got a media event on session " + this.sessionId.toString());
Janus.debug(json);
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.debug("This handle is not attached to this session");
return;
}
pluginHandle.mediaState(json["type"], json["receiving"]);
} else if (json["janus"] == "slowlink") {
Janus.debug(
"Got a slowlink event on session " + this.sessionId.toString());
Janus.debug(json);
// Trouble uplink or downlink
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.debug("This handle is not attached to this session");
return;
}
pluginHandle.slowLink(json["uplink"], json["lost"]);
} else if (json["janus"] == "error") {
// Oops, something wrong happened
Janus.error("Ooops: " +
json["error"]["code"].toString() +
" " +
json["error"]["reason"]);
Janus.debug(json);
String transaction = json["transaction"];
if (transaction != null) {
Function reportSuccess = this.transactions[transaction];
if (reportSuccess is Function) reportSuccess(json);
this.transactions.remove(transaction);
}
return;
} else if (json["janus"] == "event") {
Janus.debug("Got a plugin event on session " + this.sessionId.toString());
Janus.debug(json);
String sender = json["sender"].toString();
if (sender == null) {
Janus.warn("Missing sender...");
return;
}
var plugindata = json["plugindata"];
if (plugindata == null) {
Janus.warn("Missing plugindata...");
return;
}
Janus.debug(" -- Event is coming from " +
sender.toString() +
" (" +
plugindata["plugin"] +
")");
var data = plugindata["data"];
Janus.debug(data);
Plugin pluginHandle = this.pluginHandles[sender.toString()];
if (pluginHandle == null) {
Janus.warn("This handle is not attached to this session");
return;
}
var jsep = json["jsep"];
if (jsep != null) {
Janus.debug("Handling SDP as well...");
Janus.debug(jsep);
}
var callback = pluginHandle.onMessage;
if (callback is Function) {
Janus.debug("Notifying application...");
// Send to callback specified when attaching plugin handle
Janus.log(data);
callback(data, jsep);
} else {
// Send to generic callback (?)
Janus.debug("No provided notification callback");
}
} else if (json["janus"] == "timeout") {
Janus.error("Timeout on session " + this.sessionId.toString());
Janus.debug(json);
if (this.websockets) {
this.ws.close();
}
return;
} else {
Janus.warn("Unknown message/event '" +
json["janus"] +
"' on session " +
this.sessionId.toString());
Janus.debug(json);
}
}