Advertisement
pro_cessor

SignalR Chat Example Devspace

Oct 19th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. // References
  2.  
  3. Owin
  4. System
  5. System.Core
  6. Microsoft.CSharp
  7. Microsoft.AspNet.SignalR.Core
  8. Microsoft.Owin
  9. Microsoft.Owin.Cors
  10. Microsoft.Owin.Hosting
  11.  
  12. // Server
  13.  
  14. using System;
  15. using Microsoft.AspNet.SignalR;
  16. using Microsoft.Owin.Cors;
  17. using Microsoft.Owin.Hosting;
  18. using Owin;
  19.  
  20. namespace Chat
  21. {
  22.     public class Chat : Hub
  23.     {
  24.         public void Talk(string message)
  25.         {
  26.             Clients.All.Hear(message);
  27.         }
  28.     }
  29.  
  30.     public class Startup
  31.     {
  32.         public void Configuration(IAppBuilder app)
  33.         {
  34.             app.Map("/signalr", map =>
  35.             {
  36.                 map.UseCors(CorsOptions.AllowAll);
  37.                 map.RunSignalR();
  38.             });
  39.         }
  40.     }
  41.  
  42.     public class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.             using (WebApp.Start<Startup>("http://172.27.241.196:8123/"))
  47.             {
  48.                 Console.WriteLine("Server running at http://daheim:8123/");
  49.                 Console.ReadLine();
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. // Client
  56.  
  57. <script src="jquery-1.6.4.min.js"></script>
  58. <script src="jquery.signalR-2.0.0.min.js"></script>
  59. <script src="http://localhost:8123/signalr/hubs"></script>
  60. <script>
  61.     $(function () {
  62.         $.connection.hub.url = "http://localhost:8123/signalr";
  63.  
  64.         var chat = $.connection.chat;
  65.        
  66.         $.connection.hub.start()
  67.         .done(function () {
  68.             chat.server.talk("Yeah connected:D");
  69.         });
  70.         $('#mouth').keypress(function () {
  71.             chat.server.talk($(this).val());
  72.         });
  73.  
  74.         chat.client.hear = function (message) {
  75.             $('#ear').text(message);
  76.         };
  77.     });
  78. </script>
  79. <input id="mouth" type="text">
  80. <div id="ear"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement