Permalink
Browse files

Adapting JS code to use the new SignalR client

  • Loading branch information...
moozzyk committed Sep 18, 2017
1 parent 7376a0a commit 7fe50ebc4acf67e3abbafd25dd20f1a4af458906
Showing with 11 additions and 10 deletions.
  1. +11 −10 SignalRChat/wwwroot/index.html
@@ -30,26 +30,27 @@
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
var connection = new signalR.HubConnection('/chat');
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
connection.on('broadcastMessage', function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
});
// Start the connection.
$.connection.hub.start().done(function () {
connection.start().then(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
connection.invoke('send', $('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});

0 comments on commit 7fe50eb

Please sign in to comment.