"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