websocket-as-promised icon indicating copy to clipboard operation
websocket-as-promised copied to clipboard

Error: WebSocket closed with reason: (1006).

Open cyruslk opened this issue 3 years ago • 1 comments

Here's how I implement it:

  componentDidMount(){
    document.addEventListener("keydown", this.handleKeyDown);
    (async () => {
      try {
        await this.wsp.open();
        this.wsp.onmessage = (evt) => {

           let data = evt.data;
           let userInput = this.state.userInput;
           let cleanedUserInput = data.replace(userInput, "");
           this.setState({
             dataFromServer: cleanedUserInput
           }, () => {
             this.props.displayPrediction(cleanedUserInput)
           })
        }
      }catch(e) {
        console.error(e);
      }
    })();
  };

and:

  sendDataToServer = () => {
    if(this.state.userInputLength < 7){
        return this.handleErrorMessage();
    }
    let dataToSend = this.state.userInput;

 (async () => {
      try {
        await this.wsp.open();
        this.wsp.send(dataToSend);

      } catch(e) {
        console.error(e);
      }
    })();

    this.props.togglingLoadingSection(this.state.userInput);
    return this.resetPrediction();
  };

When the socket operates, I get the following:

index.js:1 Error: WebSocket closed with reason:  (1006).
    at WebSocketAsPromised._handleClose (index.js:353)
    at WebSocket.listener (index.js:311)

cyruslk avatar Oct 28 '20 02:10 cyruslk

Hi! you should use

wsp.onMessage.addListener(evt => ...)

See: https://github.com/vitalets/websocket-as-promised#WebSocketAsPromised+onMessage

vitalets avatar Oct 30 '20 13:10 vitalets