Skip to content

Instantly share code, notes, and snippets.

@NiDi
Created November 22, 2013 14:42
Show Gist options
  • Save NiDi/7600937 to your computer and use it in GitHub Desktop.
Save NiDi/7600937 to your computer and use it in GitHub Desktop.
POC: Websockets für Projektseminar
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require websocket_rails/main
//= require_tree .
$ ->
dispatcher = new WebSocketRails('localhost:3000/websocket')
dispatcher.bind('new_message', (data) ->
$('body').append("<p>"+data.message+"</p>")
)
$('#send').on('click', ->
dispatcher.trigger('new_message', {data: $('#my-message').val()})
)
WebsocketRails::EventMap.describe do
# You can use this file to map incoming events to controller actions.
# One event can be mapped to any number of controller actions. The
# actions will be executed in the order they were subscribed.
#
# Uncomment and edit the next line to handle the client connected event:
subscribe :new_message, :to => MessagesController, :with_method => :new_message
#
# Here is an example of mapping namespaced events:
# namespace :product do
# subscribe :new, :to => ProductController, :with_method => :new_product
# end
# The above will handle an event triggered on the client like `product.new`.
end
#....
gem 'websocket-rails'
gem 'thin'
#...
<h1> My Chat</h1>
<input type="text" name="fname" id="my-message">
<a href="javascript:void(0)" id="send"> send </a>
class MessagesController < WebsocketRails::BaseController
def new_message
broadcast_message 'new_message', {message: message}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment