Skip to content

Instantly share code, notes, and snippets.

@collin
Created August 14, 2012 18:17
Show Gist options
  • Save collin/3351425 to your computer and use it in GitHub Desktop.
Save collin/3351425 to your computer and use it in GitHub Desktop.
rack/websocket + rails
# gem 'websocket-rack'
# gem 'thin'
# config.threadsafe! == true
# or whatever Rails4 wants me to do.
require "rack/websocket"
class MyApp < Rack::WebSocket::Application
def on_message(env, msg)
send_data("echo: #{msg}")
end
end
module ActionController::WebSocket
def websocket_upgrade(socket_application)
upgrade_response = socket_application.new.call(request.env)
self.status, self.headers, self.response_body = upgrade_response
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
include ActionController::WebSocket
def echo
websocket_upgrade(MyApp)
end
end
# bundle exec thin-websocket start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment