Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. using Microsoft.AspNet.SignalR;
  2. using Microsoft.Owin;
  3. using Owin;
  4.  
  5. [assembly: OwinStartupAttribute(typeof(MyTrainer.Startup))]
  6. namespace MyTrainer
  7. {
  8. public partial class Startup
  9. {
  10. public void Configuration(IAppBuilder app)
  11. {
  12. ConfigureAuth(app);
  13.  
  14. var config = new HubConfiguration();
  15.  
  16. config.EnableDetailedErrors = true;
  17. config.EnableJavaScriptProxies = true;
  18.  
  19. app.MapSignalR("/signalr", config);
  20. }
  21. }
  22. }
  23.  
  24. @{
  25. ViewBag.Title = "ChatRoom";
  26. }
  27.  
  28. <!DOCTYPE html>
  29. <html>
  30. <head>
  31. <title>SignalR Simple Chat</title>
  32. <style type="text/css">
  33. .container {
  34. background-color: #99CCFF;
  35. border: thick solid #808080;
  36. padding: 20px;
  37. margin: 20px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <input type="text" id="message" />
  44. <input type="button" id="sendmessage" value="Send" />
  45. <input type="hidden" id="displayname" />
  46. <ul id="discussion"></ul>
  47. </div>
  48. <!--Script references. -->
  49. <!--Reference the jQuery library. -->
  50. <script src="../Scripts/jquery-1.11.3.min.js"></script>
  51. <!--Reference the SignalR library. -->
  52. <script src="../Scripts/jquery.signalR-2.2.1.min.js"></script>
  53. <!--Reference the autogenerated SignalR hub script. -->
  54. <script src="../signalr/hubs"></script>
  55. <!--Add script to update the page and send messages.-->
  56. <script type="text/javascript">
  57. $(function () {
  58. // Declare a proxy to reference the hub.
  59. var chat = $.connection.chatHub;
  60. // Create a function that the hub can call to broadcast messages.
  61. chat.client.broadcastMessage = function (name, message) {
  62. // Html encode display name and message.
  63. var encodedName = $('<div />').text(name).html();
  64. var encodedMsg = $('<div />').text(message).html();
  65. // Add the message to the page.
  66. $('#discussion').append('<li><strong>' + encodedName
  67. + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
  68. };
  69. // Get the user name and store it to prepend to messages.
  70. $('#displayname').val(prompt('Enter your name:', ''));
  71. // Set initial focus to message input box.
  72. $('#message').focus();
  73. // Start the connection.
  74. $.connection.hub.start().done(function () {
  75. $('#sendmessage').click(function () {
  76. // Call the Send method on the hub.
  77. chat.server.send($('#displayname').val(), $('#message').val());
  78. // Clear text box and reset focus for next comment.
  79. $('#message').val('').focus();
  80. });
  81. });
  82. });
  83. </script>
  84. </body>
  85. </html>
  86.  
  87. using System;
  88. using System.Collections.Generic;
  89. using System.Linq;
  90. using System.Web;
  91. using Microsoft.AspNet.SignalR;
  92.  
  93. namespace MyTrainer
  94. {
  95. public class ChatHub : Hub
  96. {
  97. public void Send(string name, string message)
  98. {
  99. Clients.All.broadcastMessage(name, message);
  100. }
  101. }
  102. }
  103.  
  104. public ActionResult ChatRoom()
  105. {
  106. return View();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement