I am reading Andrew Lombardi's book "WebSocket", it said that
You can manually trigger calling the onclose event by exe‐ cuting the close() method on a WebSocket object,
page 15
but when I call close() method, it didn't trigger onclose event...
on Chrome:
when call close()
, server stop sending frames, and after a few seconds (6-7 seconds maybe)
, my onclose
handler was invoked.
on iOS 11:
when call close()
, server stop sending frames, but onclose handler never invoked.
So, what's the truth of websocket close event?
my demo app code in React
import React, { Component } from 'react'
class App extends Component {
constructor(props) {
super(props)
this.state = {
log: [],
}
this.ws = null
}
render() {
return (
<div>
<button type="button" onClick={this._open}>
open
</button>
<button type="button" onClick={this._close}>
close
</button>
<div
style={{
padding: 10,
backgroundColor: '#ddd'
}}
>
{this.state.log.map((item, index) => {
return <div key={index}>{item}</div>
})}
</div>
</div>
)
}
_open = () => {
this.ws = new WebSocket('wss://cmb.100jc.net/answer/ws')
this.ws.onopen = () => {
alert('open')
this.setState(prevState => {
return {
log: prevState.log.concat('open')
}
})
}
this.ws.onclose = () => {
alert('close')
this.setState(prevState => {
return {
log: prevState.log.concat('close')
}
})
}
this.ws.onerror = () => {
this.setState(prevState => {
return {
log: prevState.log.concat('error')
}
})
}
}
_close = () => {
this.ws.close()
}
}
export default App
the close() method attempts to perform graceful shutdown of the websocket. It waits closeTimeout
seconds (default is 30 seconds) before actually shutting down the socket, during which the other peer can sand a close frame. After this period, the socket closes and the event is emitted. You can read more on this in this great stack overflow answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With