antoniodipinto / ikisocket

🧬 WebSocket wrapper with event management for Fiber https://github.com/gofiber/fiber. Based on Fiber WebSocket and inspired by Socket.io
MIT License
123 stars 21 forks source link

Handle Error on Unexpected/Force Disconnection #7

Closed whizdummy closed 3 years ago

whizdummy commented 3 years ago

This commit fixes the error handling of forced/unexpected disconnection on client side to avoid server panic.

The sample error message when having forced/unexpected disconnection:

Error event - Details: read tcp4 127.0.0.1:3000->127.0.0.1:58903: wsarecv: An existing connection was forcibly closed by the remote host. panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0xbb976b]

goroutine 44 [running]: github.com/fasthttp/websocket.(*Conn).Close(...) C:/Users/GoLangDirectory/pkg/mod/github.com/fasthttp/websocket@v1.4.3/conn.go:344 github.com/antoniodipinto/ikisocket.(*Websocket).disconnected(0xc00039e150, 0xded7e0, 0xc0002b4000) C:/Users/GoLangDirectory/pkg/mod/github.com/antoniodipinto/ikisocket@v0.0.0-20201101094602-d230a69e97b8/ikisocket.go:316 +0x8b github.com/antoniodipinto/ikisocket.(*Websocket).read(0xc00039e150) C:/Users/GoLangDirectory/pkg/mod/github.com/antoniodipinto/ikisocket@v0.0.0-20201101094602-d230a69e97b8/ikisocket.go:290 +0x1a5 created by github.com/antoniodipinto/ikisocket.(*Websocket).run C:/Users/GoLangDirectory/pkg/mod/github.com/antoniodipinto/ikisocket@v0.0.0-20201101094602-d230a69e97b8/ikisocket.go:244 +0x85

exit status 2

antoniodipinto commented 3 years ago

Will be merged in the master branch.

Thank you @whizdummy