Skip to content

Instantly share code, notes, and snippets.

@k-akimoto
Created March 20, 2014 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-akimoto/9664550 to your computer and use it in GitHub Desktop.
Save k-akimoto/9664550 to your computer and use it in GitHub Desktop.
20140320 blog用(clientサイド)
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