lesismal / nbio

Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
MIT License
2.11k stars 151 forks source link

How to check that websocket conn is open or closed? can we export the internal variable of isClosed? #396

Closed AnirudhDensityIntern closed 3 months ago

lesismal commented 5 months ago

For opening:

wsc, err := upgrader.Upgrade(...)
if err !=nil {
    ...
    return
}
// on open here

or register open handler by upgrader.OnOpen

For closing:

register close handler by upgrader.OnClose

You don't need to check the state of the ws conn when you use it everytime.

AnirudhDensityIntern commented 5 months ago

I want to make new connection and insure the previous websocket Should be closed, when I renew the older websocket thats why I need to check the webscocket is closed or not.

If you are not exporting isClosed Variable can you please make a function that exports the boolean value for the websocket

lesismal commented 5 months ago

I think your way is not correct, that's useless to export isClosed coz:

  1. before OnClose is called it's healthy
  2. when OnClose is called, you know that it's closed now

That's why I said you don't need to check it's state.

AnirudhDensityIntern commented 5 months ago

I know when we call conn.close it closes the stream but in order to renew the websocket I will again do conn.close on the closed channel but in that case i will get error, So I need to ensure the websocket is closed or not before closing the channel also I dont want to make 2 same stream at any moment.

lesismal commented 5 months ago

You don't need to re-create it when it's healthy! You should re-create a new conn when OnClose.

Why do you want to re-create it when it's healthy? Even you want to re-create a new conn when the old one is still healthy, you just need to close the old one and create the new one, you don't need to care about whether it returns err when you close the old one!

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.