forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.rb
More file actions
executable file
·44 lines (34 loc) · 934 Bytes
/
Copy pathwebsocket.rb
File metadata and controls
executable file
·44 lines (34 loc) · 934 Bytes
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
#!/usr/bin/env ruby
$:<< '../lib' << 'lib'
require 'goliath'
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 { |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
end
class WSInfo < Goliath::API
include Goliath::Rack::Templates
def response(env)
[200, {}, erb(:index, :views => Goliath::Application.root_path('ws'))]
end
end
class Websocket < Goliath::API
use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')
get '/', WSInfo
map '/ws', WebsocketEndPoint
end