Permalink
Browse files

Moving code to chat.ts

  • Loading branch information...
moozzyk committed Sep 18, 2017
1 parent ba1309e commit 9d44ddc2a9df985566ec5a14c9e41cec39734ab9
Showing with 28 additions and 38 deletions.
  1. +28 −0 SignalRChat/chat.ts
  2. +0 −38 SignalRChat/wwwroot/index.html
View
@@ -0,0 +1,28 @@
import * as signalR from "@aspnet/signalr-client"
import * as $ from "jquery"
// Get the user name and store it to prepend to messages.
$('#displayname').val(<string>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.
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>');
});
// Start the connection.
connection.start().then(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
connection.invoke('send', $('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
@@ -19,43 +19,5 @@
<ul id="discussion">
</ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script
src="http://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<!--Reference the SignalR library. -->
<script src="scripts/signalr/signalr-client-1.0.0-alpha1-final.js"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// 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.
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>');
});
// Start the connection.
connection.start().then(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
connection.invoke('send', $('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>

0 comments on commit 9d44ddc

Please sign in to comment.