Ok this works. The items in bold are rather difficult to glean out of the documentation, examples, or the forum.
// Audio Server Page is a special client of the Signalling Server
easyrtc.setServerListener(function(msgtype, msgdata, targeting) {
if (msgtype === "FailedDbConnect") {
console.log(msgdata.msgText + ": " + msgdata.user);
easyrtc.showError(0, "Client: " + msgdata.user + " failed to Authenticate because MySQL connection failed.");
disconnect();
// user action is to repair MySQL and reconnect Audio Server Page to NodeJS Signalling Server; new Audio clients will
// now be accepted; existing clients are able to reconnect automatically and auto play
}
});
// NodeJS Signalling Server code to send easyrtcMsg to specific client that happens to serve the audio
eventListener.onAuthenticate = function(socket, easyrtcid, appName, username, credential, easyrtcAuthMessage, next) {
// credential.session is the user.sessionId; use this to determine whether to authenticate
const dbfail = "Db_Connect_Failed";
var conObj, socketCb ;
var cred = username.split('.'), msg;
var uName = cred[0], session = cred[1], server = cred[2];
if (server) { // the Audio Server Page tacks an extra argument on to the username to indicate it is the serverRtcId
audioServerId = easyrtcid; // always refresh the server's rtcId when it authenticates
}
if (!credential || credential.session === "" || session !== credential.session) {
next("Error: Invalid Session Credential supplied!");
} else if (!uName || uName === credential.session) {
next("Error: Invalid User supplied! Client page may have expired.");
} else {
try {
verifyUser(credential, function(user) { // connects to MySQL and validates an active user session
if (!user) {
next("Error: Failed User Authentication!");
} else if (user === dbfail) {
msg = { "msgType" : "FailedDbConnect",
"msgData" : {"easyrtcid" : audioServerId,
"msgText" : "User Authentication failed - Check status of MySQL.",
"user" : uName} };
pub.getConnectionWithEasyrtcid(audioServerId, function(err, rcon){
if (err) {
console.log("Connect" + err);
} else {
conObj = rcon;
}
});
if (conObj) {
conObj.socket.on("emitEasyrtcMsg", function(msg, socketCallback) {
socketCb = socketCallback;
});
// send message to specific client, telling operator of a issue with MySQL
pub.events.emit("emitEasyrtcMsg", conObj, msg.msgType, msg, socketCb, function(err) {
if (err) {pub.util.logError("["+appName+"]["+easyrtcid+"] Unhandled easyrtcMsg listener error.", err);}
else {
pub.util.log("Emitted a Msg to Audio Server Page that MySQL connection is BAD.");
}
});
}
next("Error: Failed User Authentication!"); // tells Audio client that Signalling Server is not
// available because MySQL connection failure
}
else {
next(null);
}
});
}
catch (err) {
next(err);
}
}
};