Skip to content

Instantly share code, notes, and snippets.

@tigawa
Created August 15, 2014 04:31
Show Gist options
  • Save tigawa/36c3a9db698f41634560 to your computer and use it in GitHub Desktop.
Save tigawa/36c3a9db698f41634560 to your computer and use it in GitHub Desktop.
WebsocketRailsのチュートリアル
# ■クライアントにブロードキャストで送る方法
# サーバ側
WebsocketRails[:message_channel].trigger "message", message
# クライアント側
var dispatcher = new WebSocketRails(location.host + '/websocket', true);
var channel = dispatcher.subscribe("message_channel");
// メッセージ受信時の処理
channel.bind("message", function(message){
// ここに受信時の処理を記載 messageにJSON形式で渡される
})
# ■サーバ側に送る方法
# クライアント側
dispatcher.trigger("websocket.chat", comment);
# サーバ側
# events.rb
namespace :websocket do
subscribe :chart, 'chat#create'
end
# chart_controller.rb のcreateメソッドを定義すればOK、messageでメッセージを受け取れる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment