Created
August 14, 2012 18:17
-
-
Save collin/3351425 to your computer and use it in GitHub Desktop.
rack/websocket + rails
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
# 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