Class: Websocket::Echo::Io::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket/echo/io/server.rb

Constant Summary collapse

PORT =
3003

Class Method Summary collapse

Class Method Details

.startObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/websocket/echo/io/server.rb', line 12

def start
  handler = Handler.new

  app = proc do |env|
    # :nocov: for some reason clover can't detect coverage of these lines
    if env["rack.upgrade?"] == :websocket
      env["rack.upgrade"] = handler
      [0, {}, []] # It's possible to set cookies for the response.
    elsif env["rack.upgrade?"] == :sse
      puts "SSE connections can only receive data from the server, they can't write."
      env["rack.upgrade"] = handler
      [0, {}, []] # It's possible to set cookies for the response.
    else
      body = "Upgrade connection to Websocket to enter echo mode."
      [405, { "Content-Length" => body.bytesize, "Content-Type" => "text/plain" }, [body]]
    end
    # :nocov:
  end

  Iodine.listen service: :ws, address: "::", port: PORT, handler: app
  Iodine.start
end