Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################# SERWER ###############
- public class NotificationHub : Hub
- {
- private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
- public void Hello()
- {
- Clients.All.hello();
- }
- public static void SayHello()
- {
- hubContext.Clients.All.hello();
- }
- }
- public partial class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- ConfigureAuth(app);
- app.MapSignalR();
- }
- }
- [AllowAnonymous]
- [RoutePrefix("api/values")]
- public class ValuesController : ApiController
- {
- // GET api/values
- [Route("get")]
- [HttpGet]
- public IEnumerable<string> Get()
- {
- NotificationHub.SayHello();
- return new string[] { "value1", "value2" };
- }
- }
- ################# KLIENT ###############
- export class ChatComponent implements OnInit {
- private connection: any;
- private proxy: any;
- private url: 'http://localhost:51287/api/values/get/signalr';
- constructor() { }
- public startConnection(): void {
- this.connection = $.hubConnection(this.url, { useDefaultPath: false });
- this.proxy = this.connection.createHubProxy('ProcessingHub');
- this.connection.start().done((data: any) => {
- console.log('Connected to Processing Hub');
- this.sendMessage();
- }).catch((error: any) => {
- console.log('Hub error -> ' + error);
- });
- }
- public sendMessage(): void {
- this.proxy.invoke('SendMessage', 'test')
- .catch((error: any) => {
- console.log('SendMessage error -> ' + error);
- });
- }
- ngOnInit(): void {
- this.startConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment