Skip to content

Commit 4784d99

Browse files
author
Fabrizio Monti
committed
Test with actioncable
1 parent 390c94f commit 4784d99

File tree

11 files changed

+80
-1
lines changed

11 files changed

+80
-1
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ gem "title"
2424
gem "uglifier"
2525
gem "omniauth"
2626
gem "omniauth-github"
27+
gem "actioncable", github: "rails/actioncable"
2728

2829
source "https://rails-assets.org" do
2930
gem "rails-assets-fontawesome", "4.3.0"

Gemfile.lock

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
GIT
2+
remote: git://github.com/rails/actioncable.git
3+
revision: 63cdbf87ddaeb6bbc852de7ca2c504b7f6a22321
4+
specs:
5+
actioncable (0.0.3)
6+
actionpack (>= 4.2.0)
7+
activesupport (>= 4.2.0)
8+
celluloid (~> 0.16.0)
9+
coffee-rails
10+
em-hiredis (~> 0.3.0)
11+
faye-websocket (~> 0.10.0)
12+
redis (~> 3.0)
13+
websocket-driver (~> 0.6.1)
14+
115
GEM
216
remote: https://rubygems.org/
317
remote: https://rails-assets.org/
@@ -81,6 +95,8 @@ GEM
8195
capybara-webkit (1.7.1)
8296
capybara (>= 2.3.0, < 2.6.0)
8397
json
98+
celluloid (0.16.0)
99+
timers (~> 4.0.0)
84100
coderay (1.1.0)
85101
coffee-rails (4.1.0)
86102
coffee-script (>= 2.2.0)
@@ -113,9 +129,13 @@ GEM
113129
json
114130
thread
115131
thread_safe
132+
em-hiredis (0.3.0)
133+
eventmachine (~> 1.0)
134+
hiredis (~> 0.5.0)
116135
email_validator (1.6.0)
117136
activemodel
118137
erubis (2.7.0)
138+
eventmachine (1.0.8)
119139
execjs (2.6.0)
120140
factory_girl (4.5.0)
121141
activesupport (>= 3.0.0)
@@ -127,6 +147,9 @@ GEM
127147
faraday (0.9.2)
128148
multipart-post (>= 1.2, < 3)
129149
fastercsv (1.5.5)
150+
faye-websocket (0.10.1)
151+
eventmachine (>= 0.12.0)
152+
websocket-driver (>= 0.5.1)
130153
flutie (2.0.0)
131154
formulaic (0.3.0)
132155
activesupport
@@ -140,6 +163,8 @@ GEM
140163
hashie (3.4.3)
141164
high_voltage (2.4.0)
142165
highline (1.7.8)
166+
hiredis (0.5.2)
167+
hitimes (1.2.3)
143168
i18n (0.7.0)
144169
i18n-tasks (0.9.1)
145170
activesupport (>= 4.0.2)
@@ -250,6 +275,7 @@ GEM
250275
thor (>= 0.18.1, < 2.0)
251276
rainbow (2.0.0)
252277
rake (10.4.2)
278+
redis (3.2.1)
253279
refills (0.1.0)
254280
request_store (1.2.0)
255281
rspec-core (3.3.2)
@@ -332,6 +358,8 @@ GEM
332358
thread_safe (0.3.5)
333359
tilt (2.0.1)
334360
timecop (0.8.0)
361+
timers (4.0.4)
362+
hitimes
335363
tins (1.6.0)
336364
title (0.0.5)
337365
i18n
@@ -346,13 +374,17 @@ GEM
346374
addressable (>= 2.3.6)
347375
crack (>= 0.3.2)
348376
hashdiff
377+
websocket-driver (0.6.3)
378+
websocket-extensions (>= 0.1.0)
379+
websocket-extensions (0.1.2)
349380
xpath (2.0.0)
350381
nokogiri (~> 1.3)
351382

352383
PLATFORMS
353384
ruby
354385

355386
DEPENDENCIES
387+
actioncable!
356388
airbrake
357389
autoprefixer-rails
358390
awesome_print

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
web: bundle exec puma -p $PORT -C ./config/puma.rb
2-
worker: bundle exec rake jobs:work
2+
/* worker: bundle exec rake jobs:work */

app/assets/javascripts/application.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
//= require jquery
1414
//= require jquery-ujs
1515
//= require lodash
16+
//= require cable
1617
//= require_tree .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@App = {}
2+
App.cable = Cable.createConsumer('ws://' + window.location.host + '/websocket')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$ ->
2+
App.appearance = App.cable.subscriptions.create "AppearanceChannel",
3+
connected: ->
4+
@appear()
5+
6+
appear: ->
7+
console.log "APPEARING"
8+
@perform 'appear'
9+
10+
received: (data) ->
11+
console.log "RICEVO DATI"
12+
console.log data

app/channels/appearance_channel.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class AppearanceChannel < ApplicationCable::Channel
2+
@@total_players_count = 0
3+
4+
def subscribed
5+
stream_from "appearance_count"
6+
end
7+
8+
def appear
9+
@@total_players_count += 1
10+
ActionCable.server.broadcast "appearance_count", total_players_count: @@total_players_count
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ApplicationCable
2+
class Channel < ActionCable::Channel::Base
3+
end
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module ApplicationCable
2+
class Connection < ActionCable::Connection::Base
3+
def connect
4+
end
5+
end
6+
end

config/redis/cable.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
production: &production
2+
:url: <%= ENV['REDIS_URL'] %>
3+
:timeout: 1
4+
development: &development
5+
:url: redis://localhost:6379
6+
:timeout: 1
7+
:inline: true
8+
test: *development

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Rails.application.routes.draw do
22
root to: 'static#home'
3+
match "/websocket", to: ActionCable.server, via: [:get, :post]
34

45
get '/auth/:provider/callback' => 'sessions#create'
56
get '/signin' => 'sessions#new', as: :signin

0 commit comments

Comments
 (0)