2016-04-23 52 views
2

我想將WebSocket客戶端集成到我的angular2應用程序中。angular2中的WebSockets

我弗里斯特試圖做到這一點使用sock.js https://github.com/sockjs/sockjs-client 創造了SockJS對象是這樣的:

export class Core { 

    private sock: any; 

    constructor (private router: Router) { 

    this.sock = new SockJS(AppSettings.WEBSOCK_ENDPOINT); 

    } 

} 

我的問題是angular2是無法識別SockJS類型。

然後,我嘗試使用https://github.com/retyped/sockjs-client-tsd-ambient/blob/master/sockjs-client.d.ts在我的項目中提供的打字定義,但無法弄清楚在哪裏以及如何設置打字。

或者我試圖用angular2-WebSocket的NPM包https://github.com/afrad/angular2-websocket,但是當我這樣做在我的應用程序:

import {$WebSocket} from 'angular2-websocket/angular2-websocket' 
var ws = new $WebSocket("url"); 

我的IDE(Jetbrains的WebStorm)告訴我,$的WebSocket預計至少2個參數。

由於目前我很少有關於angular2中的WebSockets的信息,因此我決定詢問是否有人能夠向我解釋一個在angular2項目中設置WebSocket的好方法。

我用angular2 +的WebPack和建立在這個樣板https://github.com/angular/angular2-seed

回答

4

我的項目,你可能在

https://github.com/afrad/angular2-websocket/blob/master/src/angular2-websocket.ts

看到,其他參數可選

constructor(private url:string, private protocols?:Array<string>, private config?: WebSocketConfig ) { 

您可能還像這樣它有工作插口角度示例(使用socket.io)

https://github.com/leonardohjines/auctionApp

+0

感謝我覺得我的IDE與ES6功能的一些問題,這就是爲什麼被證明錯誤。 –

+0

auctionApp確實是一個很好的例子 – Ted