Created
March 20, 2014 14:09
-
-
Save k-akimoto/9664550 to your computer and use it in GitHub Desktop.
20140320 blog用(clientサイド)
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
class @ChatClass | |
constructor: (url, useWebsocket) -> | |
# これがソケットのディスパッチャー | |
@dispatcher = new WebSocketRails(url, useWebsocket) | |
# イベントを監視 | |
@bindEvents() | |
bindEvents: () => | |
# 送信ボタンが押されたらサーバへメッセージを送信 | |
$('#send').on 'click', @sendMessage | |
# サーバーからnew_messageを受け取ったらreceiveMessageを実行 | |
@dispatcher.bind 'new_message', @receiveMessage | |
sendMessage: (event) => | |
# サーバ側にsend_messageのイベントを送信 | |
# オブジェクトでデータを指定 | |
user_name = $('#username').val() | |
msg_body = $('#msgbody').val() | |
@dispatcher.trigger 'new_message', { name: user_name , body: msg_body } | |
$('#msgbody').val('') | |
receiveMessage: (message) => | |
# 受け取ったデータをappend | |
$('#chat').append "#{message.name} : #{message.body}<br/>" | |
$ -> | |
window.chatClass = new ChatClass($('#chat').data('uri'), true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment