Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: WebSocket closed with reason: (1006). #32

Open
cyruslk opened this issue Oct 28, 2020 · 1 comment
Open

Error: WebSocket closed with reason: (1006). #32

cyruslk opened this issue Oct 28, 2020 · 1 comment

Comments

@cyruslk
Copy link

cyruslk commented Oct 28, 2020

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)
@vitalets
Copy link
Owner

Hi!
you should use

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants