File: server.rb

package info (click to toggle)
ruby-blade 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 208 kB
  • sloc: ruby: 819; makefile: 3; sh: 3
file content (45 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (3)
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
require "faye/websocket"
require "useragent"

module Blade::Server
  extend self
  include Blade::Component

  WEBSOCKET_PATH = "/blade/websocket"

  def start
    Faye::WebSocket.load_adapter("thin")
    Thin::Logging.silent = true
    Thin::Server.start(host, Blade.config.port, app, signals: false)
  end

  def host
    Thin::Server::DEFAULT_HOST
  end

  def websocket_url(path = "")
    Blade.url(WEBSOCKET_PATH + path)
  end

  def client
    @client ||= Faye::Client.new(websocket_url)
  end

  def subscribe(channel)
    client.subscribe(channel) do |message|
      yield message.with_indifferent_access
    end
  end

  def publish(channel, message)
    client.publish(channel, message)
  end

  private
    def app
      Rack::Builder.app do
        use Rack::ShowExceptions
        run Blade::RackAdapter.new
      end
    end
end