Sha256: 37a581eb5ebd0a41d2ba811aca103bc0135f8904a80b9ffb1a462ad1b0768037

Contents?: true

Size: 866 Bytes

Versions: 9

Compression:

Stored size: 866 Bytes

Contents

#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

require 'goliath'
require 'goliath/websocket'
require 'goliath/rack/templates'

class Websocket < Goliath::WebSocket
  include Goliath::Rack::Templates

  use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')

  def on_open(env)
    env.logger.info("WS OPEN")
    env['subscription'] = env.channel.subscribe { |m| env.stream_send(m) }
  end

  def on_message(env, msg)
    env.logger.info("WS MESSAGE: #{msg}")
    env.channel << msg
  end

  def on_close(env)
    env.logger.info("WS CLOSED")
    env.channel.unsubscribe(env['subscription'])
  end

  def on_error(env, error)
    env.logger.error error
  end

  def response(env)
    if env['REQUEST_PATH'] == '/ws'
      super(env)
    else
      [200, {}, erb(:index, :views => Goliath::Application.root_path('ws'))]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/websocket.rb
goliath-1.0.6 examples/websocket.rb
goliath-1.0.5 examples/websocket.rb
goliath-1.0.4 examples/websocket.rb
goliath-1.0.3 examples/websocket.rb
goliath-1.0.2 examples/websocket.rb
goliath-1.0.1 examples/websocket.rb
goliath-1.0.0 examples/websocket.rb
goliath-1.0.0.beta.1 examples/websocket.rb