-3
A
回答
8
這裏我想爲您提供我在項目中使用的SignlR服務。希望這可以幫助你理解。
import { CommonService } from './commonService';
import { AuthSettings } from '../authSettings';
import { Injectable, EventEmitter } from '@angular/core';
declare const $: any;
@Injectable()
export class SignalRService {
private connectionId;
private authData;
private signalRConnectionId;
private proxy: any;
private connection: any;
private tryingToReconnect = false;
public ExpiredBidStatus: EventEmitter<any>;
public ActivatedBid: EventEmitter<any>;
public Notification: EventEmitter<any>;
constructor(private commonSvc: CommonService) {
this.ActivatedBid = new EventEmitter<any>();
this.ExpiredBidStatus = new EventEmitter<any>();
this.Notification = new EventEmitter<any>();
}
public initialize(): void {
this.connection = $.hubConnection(AuthSettings.apiServiceBaseUri);
this.proxy = this.connection.createHubProxy('crowneStockHub');
this.setToken();
this.proxy.on('broadcastExpiredBidStatus', (bidId) => {
this.ExpiredBidStatus.emit(bidId);
});
this.proxy.on('broadcastActivatedBid', (bid) => {
console.log('activated bid');
this.ActivatedBid.emit(bid);
});
this.proxy.on('broadcastNotification', (notification) => {
console.log('notification');
console.log(notification);
this.Notification.emit(notification);
});
this.proxy.on('broadcastTimeOut',() => {
this.initialize();
});
this.stopConnection();
this.connection.start().done((data: any) => {
console.log('Now connected');
this.connectionId = this.connection.id;
this.commonSvc.signalRConnectionId = this.connectionId;
}).fail((error: any) => {
console.log('Could not connect ' + error);
});
this.connection.reconnecting(() => {
this.tryingToReconnect = true;
});
this.connection.reconnected(() => {
this.tryingToReconnect = false;
});
this.connection.error((error) => {
this.initialize();
});
this.connection.disconnected(() => {
if (this.tryingToReconnect) {
setTimeout(() => {
this.initialize();
}, 5000);
}
});
}
setToken() {
this.authData = window.localStorage.getItem('authorizationData');
if (this.authData) {
const token = this.authData.token;
$.signalR.ajaxDefaults.headers = { Authorization: 'Bearer ' + token };
}
};
stopConnection() {
this.connection.stop();
};
getConnection() {
return this.connectionId;
};
}
0
謝謝Jota.Toledo, c-sharpcorner.com/article適合我。 除此之外,我還修改了OwinStartup,如下所示。
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.MapSignalR();
相關問題
- 1. MdDialog與角4
- 2. 角4 - 與ngFor
- 3. 與角4
- 4. Silverlight 4和SignalR
- 5. 角4 RxJS與switchMap
- 6. 麻煩與角4
- 7. 角4與材質
- 8. KSS Styleguide與角4
- 9. 材料角度與角4在生產
- 10. 角4個活動與EventEmitter
- 11. 角4解析HTML與ngmodel
- 12. 解析Django JsonResponse與角4
- 13. 角4與struts2集成
- 14. 角4與注射途徑
- 15. 國際化與角4
- 16. 角4與Docker和Nginx
- 17. ASP.NET MVC 4和SignalR posibilites
- 18. Signalr與Aspx Asp.Net
- 19. SignalR與證書
- 20. Web API與SignalR
- 21. SignalR與Flot
- 22. 與signalR跨域hubConnection
- 23. 的Web API與SignalR
- 24. 使用SignalR與RabbitMQ
- 25. 跨域SignalR與WCF
- 26. 微小的MCE兩路與角2/4
- 27. 角度4路由與無限/#/在url
- 28. (swipeup)未在角4觸發與Hammerjs
- 29. 角度4:* ngIf與多重條件
- 30. 角度4,在primeng大寫與編輯
你有沒有嘗試過嗎? –
https://blog.sstorie.com/integrating-angular2-and-signalr-part-1/或http://www.c-sharpcorner.com/article/asp-net-signalr-angular2-and-typescript-實時時鐘/ 。所以不會做你的研究工作 –
我試過它通過使用c-sharpcorner.com,但我收到了一個錯誤,稱爲「TypeError:無法讀取屬性'hubConnection'未定義的」 –