gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.1k stars 373 forks source link

Any example for using this library with `gnet`? #201

Closed spy16 closed 3 months ago

spy16 commented 3 months ago

I am trying to use gobwas/ws with https://github.com/panjf2000/gnet library in an attempt to build a high-performance websocket server.

I am running into io.ErrShortBuffer error under two cases:

  1. When client sends a message with size larger than the max-read-buffer set on the gnet server.
  2. When client sends a PING message.

I don't have deep understanding of the websocket protocol internals, but I believe this happens due to the continuation & fragmented frames? I found this reference but it's bit unclear ..

Are there any other examples to use gobwas/ws to use with a model like gnet? I believe, what I need here is to use a buffered reader in between instead of directly passing the gnet.Conn to the wsutil / ws functions.

cristaloleg commented 3 months ago

Hey, no idea about gnet, maybe @panjf2000 has some ideas.

ehsannm commented 3 months ago

I have created a very fast and modular framework and is used in production in our company. You can check how i used gnet and gobwas/ws together. Https://github.com/clubpay/ronykit

The related code is in std/gateways/fastws

spy16 commented 3 months ago

@ehsannm Thank you! This is really helpful.

Have you tried sending a websocket message that is larger than the ReadBufferCap set in gnet though? I think your implementation definitely resolves the first issue for me, since the part of reading all the control frames is in a loop.

But once you get a text/binary payload frame, if the frame size is bigger than the ReadBufferCap set on gnet, I believe, you would also run into the short-buffer error (or you might incorrectly miss a few bytes of the message, which will be read in the next OnTraffic which can corrupt all interactions after that point for that connection ).

For example, if you set ReadBufferCap to 1024, the websocket message can only be upto 1016 bytes (the remaining 8 bytes are used for the frame header)

ehsannm commented 3 months ago

@spy16 thanks for the point, i did write a test in the testenv package and realized that you were right about the issue, i fixed it and pushed. You can check it again.

spy16 commented 3 months ago

Thanks, I will check this out.

I will also close this issue since it's more related to the source reader itself than this package.