Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

536
Visualizações
websocket interrupted while angular2 project is loading on firefox

I've just started angular 2. I've done an angular2 sample as given in the https://angular.io/guide/quickstart

when I run the project in Firefox using

npm start

command in terminal, the connection get disconnected after output showing once.Error showing like

The connection to ws://localhost:3000/browser-sync/socket.io/?EIO=3&transport=websocket&sid=6YFGHWy7oD7T7qioAAAA was interrupted while the page was loading

Any idea about how to fix this issue ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I don't know how you manage your web socket but you could consider using the following code. This idea is to wrap the web socket into an observable.

For this you could use a service like below. The initializeWebSocket will create a shared observable (hot) to wrap a WebSocket object.

export class WebSocketService {
  initializeWebSocket(url) {
    this.wsObservable = Observable.create((observer) => {
      this.ws = new WebSocket(url);

      this.ws.onopen = (e) => {
        (...)
      };

      this.ws.onclose = (e) => {
        if (e.wasClean) {
          observer.complete();
        } else {
          observer.error(e);
        }
      };

      this.ws.onerror = (e) => {
        observer.error(e);
      }

      this.ws.onmessage = (e) => {
        observer.next(JSON.parse(e.data));
      }

      return () => {
        this.ws.close();
      };
    }).share();
  }
}

You could add a sendData to send data on the web socket:

export class WebSocketService {
  (...)

  sendData(message) {
    this.ws.send(JSON.stringify(message));
  }
}

The last point is to make things a bit robust, i.e. filter received messages based on a criteria and implement retry when there is a disconnection. For this, you need to wrap our initial websocket observable into another one. This way we can support retries when the connection is lost and integrate filtering on criteria like the client identifier (in the sample the received data is JSON and contains a sender attribute).

export class WebSocketService {
  (...)

  createClientObservable(clientId) {
    return Observable.create((observer) => {
      let subscription = this.wsObservable
        .filter((data) => data.sender!==clientId)
        .subscribe(observer);

      return () => {
        subscription.unsubscribe();
      };
    }).retryWhen((errors) => {
      return Observable.timer(3000);
    });
  }
}

You can see that deconnections are handled in this code using the retryWhen operator of observable.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda