Guest User

Untitled

a guest
Oct 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1.    ################# SERWER ###############
  2.  
  3.     public class NotificationHub : Hub
  4.     {
  5.         private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
  6.  
  7.         public void Hello()
  8.         {
  9.             Clients.All.hello();
  10.         }
  11.  
  12.         public static void SayHello()
  13.         {
  14.             hubContext.Clients.All.hello();
  15.         }
  16.     }
  17.  
  18.  
  19.     public partial class Startup
  20.     {
  21.         public void Configuration(IAppBuilder app)
  22.         {
  23.             ConfigureAuth(app);
  24.             app.MapSignalR();
  25.         }
  26.     }
  27.  
  28.     [AllowAnonymous]
  29.     [RoutePrefix("api/values")]
  30.     public class ValuesController : ApiController
  31.     {
  32.         // GET api/values
  33.         [Route("get")]
  34.         [HttpGet]
  35.         public IEnumerable<string> Get()
  36.         {
  37.             NotificationHub.SayHello();
  38.             return new string[] { "value1", "value2" };
  39.         }
  40.     }
  41.  
  42.    ################# KLIENT ###############
  43.  
  44. export class ChatComponent implements OnInit {
  45.  
  46.   private connection: any;
  47.   private proxy: any;
  48.   private url: 'http://localhost:51287/api/values/get/signalr';
  49.  
  50.   constructor() { }
  51.  
  52.   public startConnection(): void {
  53.     this.connection = $.hubConnection(this.url, { useDefaultPath: false });
  54.     this.proxy = this.connection.createHubProxy('ProcessingHub');
  55.  
  56.     this.connection.start().done((data: any) => {
  57.       console.log('Connected to Processing Hub');
  58.       this.sendMessage();
  59.     }).catch((error: any) => {
  60.       console.log('Hub error -> ' + error);
  61.     });
  62.   }
  63.  
  64.   public sendMessage(): void {
  65.     this.proxy.invoke('SendMessage', 'test')
  66.       .catch((error: any) => {
  67.         console.log('SendMessage error -> ' + error);
  68.       });
  69.   }
  70.  
  71.   ngOnInit(): void {
  72.     this.startConnection();
  73.   }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment