Skip to content

Commit

Permalink
feat(webSocket): Add binaryType to config object
Browse files Browse the repository at this point in the history
Add binaryType to config object, so that it is possible
to set that parameter on underlying socket before any
data emits happen.

Closes #2353
  • Loading branch information
mpodlasin committed Feb 10, 2017
1 parent 31dfc73 commit 86acbd1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/observables/dom/webSocket-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ describe('Observable.webSocket', () => {
subject.unsubscribe();
});

it('should take a binaryType and set it properly on the web socket', () => {
const subject = Observable.webSocket({
url: 'ws://mysocket',
binaryType: 'blob'
});

subject.subscribe();

const socket = MockWebSocket.lastSocket;
expect(socket.binaryType).to.equal('blob');

subject.unsubscribe();
});

it('should take a resultSelector', () => {
const results = [];

Expand Down Expand Up @@ -632,6 +646,7 @@ class MockWebSocket {
readyState: number = 0;
closeCode: any;
closeReason: any;
binaryType?: string;

constructor(public url: string, public protocol: string) {
MockWebSocket.sockets.push(this);
Expand Down
5 changes: 5 additions & 0 deletions src/observable/dom/WebSocketSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface WebSocketSubjectConfig {
closeObserver?: NextObserver<CloseEvent>;
closingObserver?: NextObserver<void>;
WebSocketCtor?: { new(url: string, protocol?: string|Array<string>): WebSocket };
binaryType?: 'blob' | 'arraybuffer';
}

/**
Expand All @@ -34,6 +35,7 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
closeObserver: NextObserver<CloseEvent>;
closingObserver: NextObserver<void>;
WebSocketCtor: { new(url: string, protocol?: string|Array<string>): WebSocket };
binaryType?: 'blob' | 'arraybuffer';

private _output: Subject<T>;

Expand Down Expand Up @@ -159,6 +161,9 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
new WebSocketCtor(this.url, this.protocol) :
new WebSocketCtor(this.url);
this.socket = socket;
if (this.binaryType) {
this.socket.binaryType = this.binaryType;
}
} catch (e) {
observer.error(e);
return;
Expand Down

0 comments on commit 86acbd1

Please sign in to comment.