yavsc/Yavsc/Views/Home/Chat.cshtml

80 lines
3.1 KiB
Plaintext

@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
8 years ago
<input type="hidden" id="displayname" />
8 years ago
@if (ViewBag.Contacts!=null) {
8 years ago
<select id="to" >
8 years ago
8 years ago
@foreach (var contact in ViewBag.Contacts) {
<option>@contact.UserName</option>
}
</select>
8 years ago
}
<ul id="discussion">
</ul>
8 years ago
<ul id="private">
</ul>
<ul id="chatnotifs">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
8 years ago
<script src="~/js/jquery.signalR-2.2.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
8 years ago
<script src="~/api/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages.
chat.client.addMessage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
8 years ago
chat.client.PV = function (name, message) {
// Add the pv to the page.
$('#private').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
chat.client.notify = function (tag, message) {
// Add the pv to the page.
$('#chatnotifs').append('<li><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(message) + '</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 () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
8 years ago
$('#sendpv').click(function () {
// Call the Send method on the hub.
chat.server.sendPV($('#to').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}