pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.23k stars 199 forks source link

problem with addEventListener on open, do not change external variable value #130

Closed beoArcha closed 4 years ago

beoArcha commented 4 years ago

I am using reconnecting-websocket to connect react app to sever. When I reconnect after losing connection, offen is needed to send different message. I need to do is as fast as possible, but it's not working as expected.

Demo code for problem demonstartion:

value = "oldValue";
webSocket = new ReconnectingWebSocket("ws://localhost/socket");
handler = () => console.log(this.value)
handleDidMount = () => {
    webSocket.addEventListener("open", this.handler);
    this.value - "newValue";
    console.log("reconnect")
    webSocket.reconnect()
}

My expected console result is: oldValue reconnect newValue

However I get: oldValue reconnect oldValue

my problem is how to get expected result?