/
ws.rb
57 lines (46 loc) · 1.2 KB
/
ws.rb
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
require 'goliath'
require 'goliath/application'
require 'goliath/websocket'
require 'goliath/rack/templates'
require 'pp'
class WebsocketEndPoint < Goliath::WebSocket
def on_open(env)
env.logger.info("WS OPEN")
env['subscription'] = env.channel.subscribe do |m|
puts m.inspect
env.stream_send(m)
end
env.channel << "thanks for coming"
end
def on_message(env, msg)
env.logger.info("WS MESSAGE: #{msg}")
puts env.channel.inspect
env.channel << msg
end
def on_close(env)
env.logger.info("WS CLOSED")
env.channel.unsubscribe(env['subscription'])
end
def on_response(env)
env['subscription'] = env.channel.subscribe { |m| env.stream_send(m) }
env.channel << "thanks for responding"
end
def on_error(env, error)
env.logger.error error
end
end
class WSInfo < Goliath::API
include Goliath::Rack::Templates
def response(env)
[200, {}, erb(:index, :views => Goliath::Application.root_path('ws'))]
end
end
class Ws< Goliath::API
use( Rack::Static,
:root => Goliath::Application.app_path('public'),
:urls => ['/javascripts'],
:cache_control => 'public, max-age=3600'
)
get '/', WSInfo
map '/ws', WebsocketEndPoint
end