最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSockets in angular2 - Stack Overflow

programmeradmin4浏览0评论

I want to integrate a WebSocket client to my angular2 application.

I frist tried to do this using sock.js and created a SockJS object like this:

export class Core {

  private sock: any;

  constructor (private router: Router) {

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

  }

}

My problem is that angular2 is unable to recognize the SockJS type.

I then tried to use the typings definition provided by .d.ts in my project, but couldn't really figure out where and how to set the typing up.

Alternatively I tried to use the angular2-websocket npm package , but when I do this in my application:

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

My IDE (Jetbrains WebStorm) tells me that $WebSocket expects at least 2 parameters.

Since there is very little information out there about WebSockets in angular2 at the moment I decided to ask if someone could explain me a good and easy way to set up a WebSocket within an angular2 project.

I use angular2 + webpack and built my project on this boilerplate

I want to integrate a WebSocket client to my angular2 application.

I frist tried to do this using sock.js https://github./sockjs/sockjs-client and created a SockJS object like this:

export class Core {

  private sock: any;

  constructor (private router: Router) {

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

  }

}

My problem is that angular2 is unable to recognize the SockJS type.

I then tried to use the typings definition provided by https://github./retyped/sockjs-client-tsd-ambient/blob/master/sockjs-client.d.ts in my project, but couldn't really figure out where and how to set the typing up.

Alternatively I tried to use the angular2-websocket npm package https://github./afrad/angular2-websocket, but when I do this in my application:

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

My IDE (Jetbrains WebStorm) tells me that $WebSocket expects at least 2 parameters.

Since there is very little information out there about WebSockets in angular2 at the moment I decided to ask if someone could explain me a good and easy way to set up a WebSocket within an angular2 project.

I use angular2 + webpack and built my project on this boilerplate https://github./angular/angular2-seed

Share Improve this question asked Apr 23, 2016 at 15:05 Jakob NielsenJakob Nielsen 5,20818 gold badges59 silver badges97 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

As you may see in

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

the other parameters are optional

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

you might also like this it has working socket angular example (using socket.io)

https://github./leonardohjines/auctionApp

i also can remend this implementation for websockets in angular2: https://github./Sinthor/WSM

发布评论

评论列表(0)

  1. 暂无评论