-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchat.rb
More file actions
executable file
·38 lines (31 loc) · 830 Bytes
/
chat.rb
File metadata and controls
executable file
·38 lines (31 loc) · 830 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
require 'bundler/setup'
Bundler.require
require 'goliath/websocket'
class Chat < Goliath::WebSocket
# render templated files from ./views
include Goliath::Rack::Templates
def on_open(env)
env.logger.info("CHAT OPEN")
env['subscription'] = env.channel.subscribe { |m| env.stream_send(m) }
end
def on_message(env, msg)
env.logger.info("CHAT MESSAGE: #{msg}")
env.channel << msg
end
def on_close(env)
env.logger.info("CHAT CLOSED")
env.channel.unsubscribe(env['subscription'])
end
def on_error(env, error)
env.logger.error error
end
def response(env)
if env['REQUEST_PATH'] == '/chat'
super(env)
elsif env['REQUEST_PATH'] == '/app.js'
[200, {'Content-Type' => 'application/javascript'}, coffee(:app)]
else
[200, {}, haml(:index)]
end
end
end