websocket-as-promised
websocket-as-promised copied to clipboard
Error: WebSocket closed with reason: (1006).
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)
Hi! you should use
wsp.onMessage.addListener(evt => ...)
See: https://github.com/vitalets/websocket-as-promised#WebSocketAsPromised+onMessage