我想将WebSocket客户机集成到我的angular2应用程序中。
我第一次尝试使用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-webSocketNPM包https://github.com/afrad/angular2-websocket,但是当我在应用程序中这样做时:
import {$WebSocket} from 'angular2-websocket/angular2-websocket'
var ws = new $WebSocket("url");我的IDE (Jetbrains WebStorm)告诉我,$WebSocket至少需要2个参数。
由于目前关于WebSockets in angular2的信息很少,所以我决定问是否有人能解释一下在angular2项目中建立WebSocket的好方法。
我使用angular2 + webpack并在这个样板https://github.com/angular/angular2-seed上建立了我的项目
发布于 2016-04-25 21:19:55
如你所见
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)。
发布于 2017-03-08 16:56:53
我还可以为angular2:https://github.com/Sinthor/WSM中的websockets推荐这种实现
https://stackoverflow.com/questions/36812466
复制相似问题