Created
September 19, 2012 02:25
-
-
Save gkop/3747289 to your computer and use it in GitHub Desktop.
Chat server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
Bundler.require | |
require 'goliath/websocket' | |
class Chat < Goliath::WebSocket | |
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 CLOSE") | |
env.channel.unsubscribe(env['subscription']) | |
end | |
def on_error(env, error) | |
env.logger.error error | |
end | |
def response(env) | |
if env['REQUEST_PATH'] == '/app.js' | |
[200, {}, coffee(:app)] | |
else | |
[200, {}, haml(:index)] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment