angular2websocket
Based on https://github.com/AngularClass/angular-websocket and migrated to Angular2
Installation
npm install angular2-websocket
Usage:
;ws.sendevent;
also ws.getDataStream() returns Subject to which you can attach an Observer (https://github.com/Reactive-Extensions/RxJS)
Compilation
npm run typingsnpm run compile
The default value for binary type is 'arrayBuffer'.
example
; // connect;// you can send immediately after connect, // data will cached until connect open and immediately send or connect fail. // when connect fail, websocket will reconnect or not,// you can set {WebSocketConfig.reconnectIfNotNormalClose = true} to enable auto reconnect// all cached data will lost when connect close if not reconnect // set received message callbackws.onMessage, ; // set received message streamws.getDataStream.subscribe,,; // send with default send mode (now default send mode is Observer)ws.send"some thing".subscribe,, ; ws.send"by default, this will never be sent, because Observer is cold.";ws.send"by default, this will be sent, because Observer is hot.".publish.connect; ws.setSendModeWebSocketSendMode.Direct;ws.send"this will be sent Direct, because send mode is set to Direct."; ws.send"this will be sent and return Promise.", WebSocketSendMode.Promise.then, ; ws.send"this will be sent and return Observer.".subscribe,, ; ws.closefalse; // closews.closetrue; // close immediately
Binary type
To set the binary type for the websocket one can provide it as string in the constructor. Allowed types are:
- 'blob' (default)
- 'arraybuffer'
;