Created
August 15, 2014 04:31
-
-
Save tigawa/36c3a9db698f41634560 to your computer and use it in GitHub Desktop.
WebsocketRailsのチュートリアル
This file contains hidden or 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
# ■クライアントにブロードキャストで送る方法 | |
# サーバ側 | |
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