Created
January 3, 2019 02:30
-
-
Save dcomartin/f6bb224427f495a004a69dba0c91fc59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var connection = new signalR.HubConnectionBuilder() | |
.withUrl("/messages") | |
.build(); | |
connection.on("ReceiveMessage", function(message) { | |
var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); | |
var div = document.createElement("div"); | |
div.innerHTML = msg + "<hr/>"; | |
document.getElementById("messages").appendChild(div); | |
}); | |
connection.on("UserConnected", function(connectionId) { | |
var groupElement = document.getElementById("group"); | |
var option = document.createElement("option"); | |
option.text = connectionId; | |
option.value = connectionId; | |
groupElement.add(option); | |
}); | |
connection.on("UserDisconnected", function(connectionId) { | |
var groupElement = document.getElementById("group"); | |
for(var i = 0; i < groupElement.length; i++) { | |
if (groupElement.options[i].value == connectionId) { | |
groupElement.remove(i); | |
} | |
} | |
}); | |
connection.start().catch(function(err) { | |
return console.error(err.toString()); | |
}); | |
document.getElementById("sendButton").addEventListener("click", function(event) { | |
var message = document.getElementById("message").value; | |
var groupElement = document.getElementById("group"); | |
var groupValue = groupElement.options[groupElement.selectedIndex].value; | |
if (groupValue === "All" || groupValue === "Myself") { | |
var method = groupValue === "All" ? "SendMessageToAll" : "SendMessageToCaller"; | |
connection.invoke(method, message).catch(function (err) { | |
return console.error(err.toString()); | |
}); | |
} else if (groupValue === "PrivateGroup") { | |
connection.invoke("SendMessageToGroup", "PrivateGroup", message).catch(function (err) { | |
return console.error(err.toString()); | |
}); | |
} else { | |
connection.invoke("SendMessageToUser", groupValue, message).catch(function (err) { | |
return console.error(err.toString()); | |
}); | |
} | |
event.preventDefault(); | |
}); | |
document.getElementById("joinGroup").addEventListener("click", function(event) { | |
connection.invoke("JoinGroup", "PrivateGroup").catch(function (err) { | |
return console.error(err.toString()); | |
}); | |
event.preventDefault(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment