New

Untitled clip

reclip of Untitled c...
2117 chars
March 2016
892 views
Reclip Copy RAW
  1. @{
  2. ViewBag.Title = "Chat";
  3. }
  4. <h2>Chat</h2>
  5. <div class="container">
  6. <input type="text" id="message" />
  7. <input type="button" id="sendmessage" value="Send" />
  8. <input type="hidden" id="displayname" />
  9. <ul id="discussion">
  10. </ul>
  11. </div>
  12. @section scripts {
  13. <!--Script references. -->
  14. <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
  15. <!--Reference the SignalR library. -->
  16. <script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
  17. <!--Reference the autogenerated SignalR hub script. -->
  18. <script src="~/signalr/hubs"></script>
  19. <!--SignalR script to update the chat page and send messages.-->
  20. <script>
  21. $(function () {
  22. // Reference the auto-generated proxy for the hub.
  23. var chat = $.connection.chatHub;
  24. // Create a function that the hub can call back to display messages.
  25. chat.client.addNewMessageToPage = function (name, message) {
  26. // Add the message to the page.
  27. $('#discussion').append('<li><strong>' + htmlEncode(name)
  28. + '</strong>: ' + htmlEncode(message) + '</li>');
  29. };
  30. // Get the user name and store it to prepend to messages.
  31. $('#displayname').val(prompt('Enter your name:', ''));
  32. // Set initial focus to message input box.
  33. $('#message').focus();
  34. // Start the connection.
  35. $.connection.hub.start().done(function () {
  36. $('#sendmessage').click(function () {
  37. // Call the Send method on the hub.
  38. chat.server.send($('#displayname').val(), $('#message').val());
  39. // Clear text box and reset focus for next comment.
  40. $('#message').val('').focus();
  41. });
  42. });
  43. });
  44. // This optional function html-encodes messages for display in the page.
  45. function htmlEncode(value) {
  46. var encodedValue = $('<div />').text(value).html();
  47. return encodedValue;
  48. }
  49. </script>
  50. }