yavsc/Yavsc/Views/Home/Chat.cshtml

93 lines
3.3 KiB
Text
Raw Normal View History

2016-05-17 18:22:18 +02:00
@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<style>
.discussion {
color: black;
font-family: monospace;
}
.notif {
color: black;
font-family: monospace;
font-style: italic;
}
.pv {
color: black;
font-family: monospace;
font-style: bold;
}
</style>
2016-05-17 18:22:18 +02:00
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
2016-09-23 12:30:47 +02:00
2016-05-17 18:22:18 +02:00
<input type="hidden" id="displayname" />
2016-10-08 19:20:00 +02:00
@if (ViewBag.Contacts!=null) {
2016-09-23 12:30:47 +02:00
<select id="to" >
2016-10-08 19:20:00 +02:00
2016-09-23 12:30:47 +02:00
@foreach (var contact in ViewBag.Contacts) {
<option>@contact.UserName</option>
}
</select>
2016-10-08 19:20:00 +02:00
}
2016-05-17 18:22:18 +02:00
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
2016-10-08 19:20:00 +02:00
<script src="~/js/jquery.signalR-2.2.1.min.js"></script>
2016-05-17 18:22:18 +02:00
<!--Reference the autogenerated SignalR hub script. -->
2016-10-23 01:31:09 +02:00
<script src="~/api/signalr/hubs"></script>
2016-05-17 18:22:18 +02:00
<!--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 class="discussion"><strong>' + htmlEncode(name)
2016-05-17 18:22:18 +02:00
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
chat.client.addPV = function (name, message) {
2016-09-23 12:30:47 +02:00
// Add the pv to the page.
$('#discussion').append('<li class="pv"><strong>' + htmlEncode(name)
2016-09-23 12:30:47 +02:00
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
chat.client.notify = function (tag, message, data) {
// Add the pv to the page.
$('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(message) +' '+ htmlEncode(data) +'</li>');
};
2016-05-17 18:22:18 +02:00
// 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();
});
2016-09-23 12:30:47 +02:00
$('#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();
});
2016-05-17 18:22:18 +02:00
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}