Angular6: Issue with websocket connection on load
up vote
0
down vote
favorite
I have a websocket connection which on load throws an error occasionally. Although after reloading the page,the connection is restored. But I dont want the reload in the first place.
Below is my web-socket service to open the connection
@Injectable()
export class WebSocketService {
private subject: Subject<MessageEvent>;
constructor() {
}
public connect(url: string): Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
}
return this.subject;
}
private create(url: string): Subject<MessageEvent> {
const ws = new WebSocket(url);
const observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}).pipe(share());
const observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
}
};
return Subject.create(observer, observable);
}
Below I subscribe to the websocket service
private subscrib(): void {
this.$messageQueue = <Subject<QueueDetails>>this.ws
.connect(this.wsurl)
.pipe(
map((response: MessageEvent): QueueDetails => {
const data = JSON.parse(response.data);
})
);
}
websocket connection to 'wss://someurl' failed. Error during websocket handshake: Invalid status line.
Has anyone faced a similar error in angular and knows a fix?
angular websocket connection handshake
add a comment |
up vote
0
down vote
favorite
I have a websocket connection which on load throws an error occasionally. Although after reloading the page,the connection is restored. But I dont want the reload in the first place.
Below is my web-socket service to open the connection
@Injectable()
export class WebSocketService {
private subject: Subject<MessageEvent>;
constructor() {
}
public connect(url: string): Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
}
return this.subject;
}
private create(url: string): Subject<MessageEvent> {
const ws = new WebSocket(url);
const observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}).pipe(share());
const observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
}
};
return Subject.create(observer, observable);
}
Below I subscribe to the websocket service
private subscrib(): void {
this.$messageQueue = <Subject<QueueDetails>>this.ws
.connect(this.wsurl)
.pipe(
map((response: MessageEvent): QueueDetails => {
const data = JSON.parse(response.data);
})
);
}
websocket connection to 'wss://someurl' failed. Error during websocket handshake: Invalid status line.
Has anyone faced a similar error in angular and knows a fix?
angular websocket connection handshake
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a websocket connection which on load throws an error occasionally. Although after reloading the page,the connection is restored. But I dont want the reload in the first place.
Below is my web-socket service to open the connection
@Injectable()
export class WebSocketService {
private subject: Subject<MessageEvent>;
constructor() {
}
public connect(url: string): Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
}
return this.subject;
}
private create(url: string): Subject<MessageEvent> {
const ws = new WebSocket(url);
const observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}).pipe(share());
const observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
}
};
return Subject.create(observer, observable);
}
Below I subscribe to the websocket service
private subscrib(): void {
this.$messageQueue = <Subject<QueueDetails>>this.ws
.connect(this.wsurl)
.pipe(
map((response: MessageEvent): QueueDetails => {
const data = JSON.parse(response.data);
})
);
}
websocket connection to 'wss://someurl' failed. Error during websocket handshake: Invalid status line.
Has anyone faced a similar error in angular and knows a fix?
angular websocket connection handshake
I have a websocket connection which on load throws an error occasionally. Although after reloading the page,the connection is restored. But I dont want the reload in the first place.
Below is my web-socket service to open the connection
@Injectable()
export class WebSocketService {
private subject: Subject<MessageEvent>;
constructor() {
}
public connect(url: string): Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
}
return this.subject;
}
private create(url: string): Subject<MessageEvent> {
const ws = new WebSocket(url);
const observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}).pipe(share());
const observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
}
};
return Subject.create(observer, observable);
}
Below I subscribe to the websocket service
private subscrib(): void {
this.$messageQueue = <Subject<QueueDetails>>this.ws
.connect(this.wsurl)
.pipe(
map((response: MessageEvent): QueueDetails => {
const data = JSON.parse(response.data);
})
);
}
websocket connection to 'wss://someurl' failed. Error during websocket handshake: Invalid status line.
Has anyone faced a similar error in angular and knows a fix?
angular websocket connection handshake
angular websocket connection handshake
asked Nov 8 at 16:54
prabhat gundepalli
11912
11912
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212549%2fangular6-issue-with-websocket-connection-on-load%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown