File: echo_server.rb

package info (click to toggle)
ruby-faye-websocket 0.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 236 kB
  • sloc: ruby: 1,209; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'puma'
require 'puma/binder'
require 'puma/events'

unless RUBY_PLATFORM =~ /java/
  Faye::WebSocket.load_adapter('thin')
  Thin::Logging.silent = true
end

class EchoServer
  def call(env)
    socket = Faye::WebSocket.new(env, ["echo"])
    socket.onmessage = lambda do |event|
      socket.send(event.data)
    end
    socket.rack_response
  end

  def log(*args)
  end

  def listen(port, backend, tls = false)
    case backend
    when :puma then listen_puma(port, tls)
    when :thin then listen_thin(port, tls)
    end
  end

  def stop
    case @server
    when Puma::Server then @server.stop(true)
    else @server.stop
    end
  end

private

  def listen_puma(port, tls)
    events = Puma::Events.new(StringIO.new, StringIO.new)
    binder = Puma::Binder.new(events)
    binder.parse(["tcp://0.0.0.0:#{ port }"], self)
    @server = Puma::Server.new(self, events)
    @server.binder = binder
    @server.run
  end

  def listen_thin(port, tls)
    Rack::Handler.get('thin').run(self, :Port => port) do |s|
      if tls
        s.ssl = true
        s.ssl_options = {
          :private_key_file => File.expand_path('../server.key', __FILE__),
          :cert_chain_file  => File.expand_path('../server.crt', __FILE__)
        }
      end
      @server = s
    end
  end
end