web-socket-pinger v1.0.0
WebSocket Pinger
The WebSocket spec defines a PING-PONG system that allows the side sending the PING to ensure that the other side is still alive.
Crystal Language has proper PONG-response system built-in. But there is no automatic system for sending PING messages at intervals.
I'm sure every project that uses WebSocket doesn't want to deal with dead connections: not closed properly or opened by villians.
So this simple library allows you to enable PING sending for every WebSocket connection and closes it in case of no PONG response within a given timeout period.
Installation
-
Add the dependency to your
shard.yml
:dependencies: web-socket-pinger: github: Xanders/web-socket-pinger
-
Run
shards install
Usage
Just add WebSocketPinger.start(socket)
to the head of your handler:
require "http/web_socket"
require "web-socket-pinger"
my_handler = HTTP::WebSocketHandler.new do |socket, context|
WebSocketPinger.start(socket)
# ...rest of handler code
end
server = HTTP::Server.new [my_handler]
server.bind_tcp "0.0.0.0", 8080
server.listen
Development
I'm using Docker for library development. If you have Docker available, you can use make
command to see the help, powered by make-help project. There are commands for testing, formatting and documentation.
Sadly, I have no idea, how to test this properly. If you have any ideas, please open the Issue or Pull Request.
Contributing
- Fork it (https://github.com/Xanders/web-socket-pinger/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
web-socket-pinger
- 0
- 0
- 0
- 0
- 0
- almost 2 years ago
- January 7, 2023
MIT License
Mon, 18 Nov 2024 05:36:14 GMT