loading
Generated 2024-05-15T09:39:54+00:00

All Files ( 100.0% covered at 1.05 hits/line )

4 files in total.
39 relevant lines, 39 lines covered and 0 lines missed. ( 100.0% )
4 total branches, 3 branches covered and 1 branches missed. ( 75.0% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line Branch Coverage Branches Covered branches Missed branches
lib/websocket/echo/io.rb 100.00 % 14 7 7 0 1.00 100.00 % 0 0 0
lib/websocket/echo/io/handler.rb 100.00 % 32 16 16 0 1.13 100.00 % 2 2 0
lib/websocket/echo/io/server.rb 100.00 % 45 12 12 0 1.00 50.00 % 2 1 1
lib/websocket/echo/io/version.rb 100.00 % 9 4 4 0 1.00 100.00 % 0 0 0

lib/websocket/echo/io.rb

100.0% lines covered

100.0% branches covered

7 relevant lines. 7 lines covered and 0 lines missed.
0 total branches, 0 branches covered and 0 branches missed.
    
  1. # frozen_string_literal: true
  2. 1 require "iodine"
  3. 1 require_relative "io/version"
  4. 1 require_relative "io/server"
  5. 1 module Websocket
  6. 1 module Echo
  7. 1 module Io
  8. 1 class Error < StandardError; end
  9. end
  10. end
  11. end

lib/websocket/echo/io/handler.rb

100.0% lines covered

100.0% branches covered

16 relevant lines. 16 lines covered and 0 lines missed.
2 total branches, 2 branches covered and 0 branches missed.
    
  1. # frozen_string_literal: true
  2. 1 module Websocket
  3. 1 module Echo
  4. 1 module Io
  5. 1 class Handler
  6. 1 GOODBYE_LINE = "See you in a bit."
  7. 1 SHUTDOWN_LINE = "Server going away\r\n"
  8. 1 def on_open(client)
  9. # Set a connection timeout
  10. 1 client.timeout = 10
  11. # Write a welcome message
  12. 1 client.write "WebSocket-Echo-IO server powered by Iodine #{Iodine::VERSION}.\r\n"
  13. end
  14. 1 def on_message(client, data)
  15. 2 client.write data
  16. 2 else: 1 then: 1 return unless /^Goodbye\b/.match?(data)
  17. 1 client.write GOODBYE_LINE
  18. 1 client.close
  19. end
  20. 1 def on_shutdown(client)
  21. 1 client.write SHUTDOWN_LINE
  22. end
  23. end
  24. end
  25. end
  26. end

lib/websocket/echo/io/server.rb

100.0% lines covered

50.0% branches covered

12 relevant lines. 12 lines covered and 0 lines missed.
2 total branches, 1 branches covered and 1 branches missed.
    
  1. # frozen_string_literal: true
  2. 1 require_relative "handler"
  3. 1 module Websocket
  4. 1 module Echo
  5. 1 module Io
  6. 1 class Server
  7. # support heroku custom port
  8. 1 then: 0 else: 1 PORT = ENV["PORT"]&.to_i || 3003
  9. 1 class << self
  10. 1 def start
  11. 1 handler = Handler.new
  12. 1 app = proc do |env|
  13. skipped # :nocov: for some reason clover can't detect coverage of these lines
  14. skipped if env["rack.upgrade?"] == :websocket
  15. skipped env["rack.upgrade"] = handler
  16. skipped [0, {}, []] # It's possible to set cookies for the response.
  17. skipped elsif env["rack.upgrade?"] == :sse
  18. skipped puts "SSE connections can only receive data from the server, they can't write."
  19. skipped env["rack.upgrade"] = handler
  20. skipped [0, {}, []] # It's possible to set cookies for the response.
  21. skipped else
  22. skipped body = "Upgrade connection to Websocket to enter echo mode."
  23. skipped [418, { "Content-Length" => body.bytesize, "Content-Type" => "text/plain" }, [body]]
  24. skipped end
  25. skipped # :nocov:
  26. end
  27. 1 Iodine.listen service: :ws, address: "::", port: PORT, handler: app # max_msg: 128
  28. 1 Iodine.start
  29. # in case long term running clogs the server, hot restart regularly
  30. # see https://github.com/boazsegev/iodine#hot-restart
  31. # Iodine.run_every(4 * 60 * 60 * 1000) do
  32. # Process.kill("SIGUSR1", Process.pid) unless Iodine.worker?
  33. # end
  34. end
  35. end
  36. end
  37. end
  38. end
  39. end

lib/websocket/echo/io/version.rb

100.0% lines covered

100.0% branches covered

4 relevant lines. 4 lines covered and 0 lines missed.
0 total branches, 0 branches covered and 0 branches missed.
    
  1. # frozen_string_literal: true
  2. 1 module Websocket
  3. 1 module Echo
  4. 1 module Io
  5. 1 VERSION = "0.1.0"
  6. end
  7. end
  8. end