CHAPTER 6
In the realtimechat project, expand the Pages folder and edit the Index.cshtml file.

Figure 23:The Index.cshtml File
You will see the following default HTML code.
Code Listing 11
@page @model IndexModel @{ ViewData["Title"] = "Home page"; } <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> |
Replace the code in the Index.cshtml file with the code in Code Listing 12.
Code Listing 12
@page <form> <div class="form-group"> <label for="userInput">User</label> <input class="form-control" placeholder="Enter your name" type="text" id="userInput" /> </div> <div class="form-group"> <label for="messageInput">Message</label> <input class="form-control" placeholder="Type your message" type="text" id="messageInput" /> </div> <div class="form-group"> <input class="btn btn-primary" type="button" id="sendButton" value="Send Message" /> </div> </form> <div class="row"> <div class="col-12"> <hr /> </div> </div> <div class="row"> <div class="col-6"> <ul class="list-group" id="messagesList"></ul> </div> </div> <script src="~/js/signalr/dist/browser/signalr.js"></script> <script src="~/js/chat.js"></script> |
The code in Code Listing 12 simply adds text boxes for the user’s name and the message that they want to send. It also includes a send button. There is also a list called messagesList that will display all the messages received from the SignalR hub.
At the bottom of the code, you will see script references to signalr.js and chat.js. You will recall that we added the signalr.js file when we added the client-side libraries to our project (Figure 21). We have not, however, added the chat.js file to our project yet.
To do this, right-click your js folder and add a new JavaScript file, as illustrated in Figure 24.

Figure 24: Add New Item
Call the file chat.js and click Add. Your solution should now look as illustrated in Figure 25.

Figure 25: Added chat.js File
After adding your chat.js file, you should see the blank code editor. Add the following JavaScript code to your chat.js file.
Code Listing 13
|
var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build(); //Disable send button until connection is established. document.getElementById("sendButton").disabled = true; connection.on("MessageReceiver", function (user, message) { var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); var encodedMsg = user + ": " + msg; var li = document.createElement("li"); var currentUser = document.getElementById("userInput").value; if (currentUser === user) { li.className = "list-group-item list-group-item-primary"; } else { li.className = "list-group-item list-group-item-success"; }
li.textContent = encodedMsg; document.getElementById("messagesList").appendChild(li); }); 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("MessageSender", user, message).catch(function (err) { return console.error(err.toString()); }); event.preventDefault(); }); |
Let’s have a closer look at this JavaScript file. Essentially, it does three things:
Note: I will not go into detail regarding the chat.js file’s code. Later on, we will create a SignalR charting app. This will use a similar JavaScript file called charting.js. I will expand on the JavaScript in the file when we discuss the real-time charting application.