So I have finally set up the channels code and got that running as a chat app… Code is below for anyone interested.
My question is now… How would I send a message to a specific person? Say I have a situation where player A, B and D who are listening on websockets are in specific channels (all different), but I want them all to get a message without C or E seeing it.
Is there a way I can say, “Take this list and then send this message no matter what channel they are on”?
/** ensure that we have URL.username and set a default channel */
if (!isNull(URL.username))
Session.username = URL.username;
if (isEmpty(URL.username ?: "")){
echo("<p>Username is not defined. Set it using the URL parameter username, e.g. ?Username=Lucy");
abort;
}
if (isEmpty(URL.channel ?: "")){
echo("<p>Channel is not defined. Set it using the URL parameter username, e.g. ?Channel=Galaxy Radio");
abort;
}
param name="channel" default="default";
<script>
var channel = "#url.Channel#";
var endpoint = "/WS/Chat/" + channel;
var protocol = (document.location.protocol == "https:") ? "wss://" : "ws://";
var wschat = new WebSocket(protocol + document.location.host + endpoint);
document.write(`<p>[Lance Chat .2a] connected to "${channel}" as "#URL.username#"`);
function onopen(evt)
{
var msg = evt.data ? JSON.parse(evt.data) : evt;
console.log(msg);
var ChatFrom = msg.FROM;
var ChatMessage = msg.MESSAGE;
if (ChatFrom == '<server>') {
ChatFrom = ChatFrom + "System";
}
document.getElementById("ChatScreen").innerHTML = document.getElementById("ChatScreen").innerHTML + ChatFrom + ': ' + ChatMessage + '<br>';
}
function onmessage(evt)
{
var msg = evt.data ? JSON.parse(evt.data) : evt;
console.log(msg);
var ChatFrom = msg.FROM;
var ChatMessage = msg.MESSAGE;
if (ChatFrom == '<server>') {
ChatFrom = ChatFrom + "System";
}
document.getElementById("ChatScreen").innerHTML = document.getElementById("ChatScreen").innerHTML + ChatFrom + ': ' + ChatMessage + '<br>';
}
function onerror(evt)
{
var msg = evt.data ? JSON.parse(evt.data) : evt;
console.log(msg);
var ChatFrom = msg.FROM;
var ChatMessage = msg.MESSAGE;
if (ChatFrom == '<server>') {
ChatFrom = ChatFrom + "System";
}
document.getElementById("ChatScreen").innerHTML = document.getElementById("ChatScreen").innerHTML + ERROR + ': ' + ChatMessage + '<br>';
}
function onclose(evt)
{
var msg = evt.data ? JSON.parse(evt.data) : evt;
console.log(msg);
var ChatFrom = msg.FROM;
var ChatMessage = msg.MESSAGE;
if (ChatFrom == '<server>') {
ChatFrom = ChatFrom + "System";
}
document.getElementById("ChatScreen").innerHTML = document.getElementById("ChatScreen").innerHTML + ChatFrom + '(Close): ' + ChatMessage + '<br>';
}
wschat.onopen = onopen;
wschat.onmessage = onmessage;
wschat.onerror = onerror;
wschat.onclose = onclose;
function SendMessage()
{
var message = document.getElementById("Message").value;
wschat.send(message);
document.getElementById("Message").value='';
}
</script>
<form onkeypress="return event.keyCode != 13">
<input type="Text" ID="Message" value="">
<input type="Button" name="Submit" value="Submit" onclick="SendMessage();">
</form>
<P>
<select name="Channel" ID="Channel" onchange="window.location.href = 'index.cfm?username=#url.Username#&Channel=' + document.getElementById('Channel').value;">
<option selected>Please select a channel</option>
<option value="Galaxy Radio">Galaxy Radio</option>
<option value="GNN">GNN</option>
<option value="Davul Government">Davul Government</option>
</select>
<HR>
<div ID="ChatScreen">
</div>
------------------------------