added hub script
This commit is contained in:
parent
37b90af482
commit
5e3d979999
@ -4,7 +4,14 @@
|
|||||||
ViewData["Title"] = "Home page";
|
ViewData["Title"] = "Home page";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@section Scripts
|
||||||
|
{
|
||||||
|
<script src="~/js/signalr/dist/browser/signalr.js"></script>
|
||||||
|
<script src="~/js/hub.js"></script>
|
||||||
|
}
|
||||||
|
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1 class="display-4">Welcome</h1>
|
|
||||||
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
28
BinaryDad.Coding/wwwroot/js/hub.js
Normal file
28
BinaryDad.Coding/wwwroot/js/hub.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
|
||||||
|
|
||||||
|
//Disable send button until connection is established
|
||||||
|
document.getElementById("sendButton").disabled = true;
|
||||||
|
|
||||||
|
connection.on("ReceiveMessage", function (user, message) {
|
||||||
|
var li = document.createElement("li");
|
||||||
|
document.getElementById("messagesList").appendChild(li);
|
||||||
|
// We can assign user-supplied strings to an element's textContent because it
|
||||||
|
// is not interpreted as markup. If you're assigning in any other way, you
|
||||||
|
// should be aware of possible script injection concerns.
|
||||||
|
li.textContent = `${user} says ${message}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.start().then(function () {
|
||||||
|
document.getElementById("sendButton").disabled = false;
|
||||||
|
}).catch(function (err) {
|
||||||
|
return console.error(err.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById("sendButton").addEventListener("click", function (event) {
|
||||||
|
var user = document.getElementById("userInput").value;
|
||||||
|
var message = document.getElementById("messageInput").value;
|
||||||
|
connection.invoke("SendMessage", user, message).catch(function (err) {
|
||||||
|
return console.error(err.toString());
|
||||||
|
});
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user