Skip to content

Commit

Permalink
feat(url): allow to use a relative url to register
Browse files Browse the repository at this point in the history
  • Loading branch information
davinkevin committed Apr 14, 2018
1 parent 63cf4e0 commit 8610371
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
4 changes: 2 additions & 2 deletions demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {HttpClientModule} from '@angular/common/http';
NgxStompModule.withConfig({
login: 'login',
password: 'password',
url: 'ws://localhost:8080/hello',
debug: false,
url: '/hello',
debug: true,
vhost: 'foo',
headers: {}
})
Expand Down
2 changes: 1 addition & 1 deletion demo/src/lib/ngx-stomp/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface StompConfiguration {
debug: boolean;
vhost: string;
headers: any;
over?: any;
over?: WebSocket;
}

export const STOMP_JS = new InjectionToken('STOMP_JS');
44 changes: 39 additions & 5 deletions demo/src/lib/ngx-stomp/stomp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {Observable} from 'rxjs/Observable';
import {concatMap, map, publishReplay} from 'rxjs/operators';
import {OperatorFunction} from 'rxjs/interfaces';
import {ConnectableObservable} from 'rxjs/observable/ConnectableObservable';
import {DOCUMENT} from '@angular/common';

export interface Stomp {
client: (url: string, protocols?: string | Array<string>) => Client;
over: (ws: WebSocket) => Client;
}


@Injectable()
Expand All @@ -13,9 +19,10 @@ export class StompService {
private readonly connection$: Observable<Client>;

constructor(@Inject(STOMP_CONFIGURATION) private conf: StompConfiguration,
@Inject(STOMP_JS) private Stomp: any) {

this.cl = initClient(Stomp, this.conf.over, this.conf.url);
@Inject(STOMP_JS) private stomp: Stomp,
@Inject(DOCUMENT) document: any
) {
this.cl = initClient(stomp, this.conf.over, this.conf.url, document.location);
this.cl.debug = initDebug(this.conf.debug);

this.connection$ = initConnection(this.cl, this.conf);
Expand Down Expand Up @@ -44,8 +51,14 @@ function toJSON<T>(): OperatorFunction<Message, T> {
return map((m: Message) => JSON.parse(m.body));
}

function initClient(Stomp: any, c: any, url: string): Client {
return c ? Stomp.over(new c(url)) : Stomp.client(url);
function initClient(stomp: Stomp, c: WebSocket, url: string, loc: Location): Client {
if (c) {
return stomp.over(c);
}

const urlNormalized = normalizeUrl(url, loc);
console.log(urlNormalized);
return stomp.client(urlNormalized);
}

function initDebug(isActivated: boolean) {
Expand All @@ -66,3 +79,24 @@ function initConnection(client: Client, config: StompConfiguration): Observable<

return c$;
}

function normalizeUrl(url: string, loc: Location): string {
if (url == null) {
throw new Error('No url found for web-socket');
}

if (url.startsWith('/')) {
return toWsProtocol(loc.protocol) + '//' + loc.hostname + ':' + loc.port + url;
}

try {
return new URL(url).toString();
} catch (e) {
console.error(e);
throw new Error(`Url with format ${url} not handle for now. Should be complete or start with /`);
}
}

function toWsProtocol(protocol: string): string {
return 'http:' === protocol ? 'ws:' : 'wss';
}
44 changes: 39 additions & 5 deletions src/lib/stomp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {Observable} from 'rxjs/Observable';
import {concatMap, map, publishReplay} from 'rxjs/operators';
import {OperatorFunction} from 'rxjs/interfaces';
import {ConnectableObservable} from 'rxjs/observable/ConnectableObservable';
import {DOCUMENT} from '@angular/common';

export interface Stomp {
client: (url: string, protocols?: string | Array<string>) => Client;
over: (ws: WebSocket) => Client;
}


@Injectable()
Expand All @@ -13,9 +19,10 @@ export class StompService {
private readonly connection$: Observable<Client>;

constructor(@Inject(STOMP_CONFIGURATION) private conf: StompConfiguration,
@Inject(STOMP_JS) private Stomp: any) {

this.cl = initClient(Stomp, this.conf.over, this.conf.url);
@Inject(STOMP_JS) private stomp: Stomp,
@Inject(DOCUMENT) document: any
) {
this.cl = initClient(stomp, this.conf.over, this.conf.url, document.location);
this.cl.debug = initDebug(this.conf.debug);

this.connection$ = initConnection(this.cl, this.conf);
Expand Down Expand Up @@ -44,8 +51,14 @@ function toJSON<T>(): OperatorFunction<Message, T> {
return map((m: Message) => JSON.parse(m.body));
}

function initClient(Stomp: any, c: any, url: string): Client {
return c ? Stomp.over(c) : Stomp.client(url);
function initClient(stomp: Stomp, c: WebSocket, url: string, loc: Location): Client {
if (c) {
return stomp.over(c);
}

const urlNormalized = normalizeUrl(url, loc);
console.log(urlNormalized);
return stomp.client(urlNormalized);
}

function initDebug(isActivated: boolean) {
Expand All @@ -66,3 +79,24 @@ function initConnection(client: Client, config: StompConfiguration): Observable<

return c$;
}

function normalizeUrl(url: string, loc: Location): string {
if (url == null) {
throw new Error('No url found for web-socket');
}

if (url.startsWith('/')) {
return toWsProtocol(loc.protocol) + '//' + loc.hostname + ':' + loc.port + url;
}

try {
return new URL(url).toString();
} catch (e) {
console.error(e);
throw new Error(`Url with format ${url} not handle for now. Should be complete or start with /`);
}
}

function toWsProtocol(protocol: string): string {
return 'http:' === protocol ? 'ws:' : 'wss';
}

0 comments on commit 8610371

Please sign in to comment.