litecable 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +40 -0
  3. data/.rubocop.yml +63 -0
  4. data/.travis.yml +7 -0
  5. data/CHANGELOG.md +7 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +128 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/circle.yml +8 -0
  13. data/examples/sinatra/Gemfile +16 -0
  14. data/examples/sinatra/Procfile +3 -0
  15. data/examples/sinatra/README.md +33 -0
  16. data/examples/sinatra/anycable +18 -0
  17. data/examples/sinatra/app.rb +52 -0
  18. data/examples/sinatra/assets/app.css +169 -0
  19. data/examples/sinatra/assets/cable.js +584 -0
  20. data/examples/sinatra/assets/reset.css +223 -0
  21. data/examples/sinatra/bin/anycable-go +0 -0
  22. data/examples/sinatra/chat.rb +39 -0
  23. data/examples/sinatra/config.ru +28 -0
  24. data/examples/sinatra/views/index.slim +8 -0
  25. data/examples/sinatra/views/layout.slim +15 -0
  26. data/examples/sinatra/views/login.slim +8 -0
  27. data/examples/sinatra/views/resetcss.slim +224 -0
  28. data/examples/sinatra/views/room.slim +68 -0
  29. data/lib/lite_cable.rb +29 -0
  30. data/lib/lite_cable/anycable.rb +62 -0
  31. data/lib/lite_cable/channel.rb +8 -0
  32. data/lib/lite_cable/channel/base.rb +165 -0
  33. data/lib/lite_cable/channel/registry.rb +34 -0
  34. data/lib/lite_cable/channel/streams.rb +56 -0
  35. data/lib/lite_cable/coders.rb +7 -0
  36. data/lib/lite_cable/coders/json.rb +19 -0
  37. data/lib/lite_cable/coders/raw.rb +15 -0
  38. data/lib/lite_cable/config.rb +18 -0
  39. data/lib/lite_cable/connection.rb +10 -0
  40. data/lib/lite_cable/connection/authorization.rb +13 -0
  41. data/lib/lite_cable/connection/base.rb +131 -0
  42. data/lib/lite_cable/connection/identification.rb +88 -0
  43. data/lib/lite_cable/connection/streams.rb +28 -0
  44. data/lib/lite_cable/connection/subscriptions.rb +108 -0
  45. data/lib/lite_cable/internal.rb +13 -0
  46. data/lib/lite_cable/logging.rb +28 -0
  47. data/lib/lite_cable/server.rb +27 -0
  48. data/lib/lite_cable/server/client_socket.rb +9 -0
  49. data/lib/lite_cable/server/client_socket/base.rb +163 -0
  50. data/lib/lite_cable/server/client_socket/subscriptions.rb +23 -0
  51. data/lib/lite_cable/server/heart_beat.rb +50 -0
  52. data/lib/lite_cable/server/middleware.rb +55 -0
  53. data/lib/lite_cable/server/subscribers_map.rb +67 -0
  54. data/lib/lite_cable/server/websocket_ext/protocols.rb +45 -0
  55. data/lib/lite_cable/version.rb +4 -0
  56. data/lib/litecable.rb +2 -0
  57. data/litecable.gemspec +33 -0
  58. metadata +256 -0
@@ -0,0 +1,223 @@
1
+ /* Reset
2
+ ----------------------------------------------------------------------------- */
3
+
4
+ /* stylelint-disable */
5
+
6
+ html, body,
7
+ h1, h2, h3, h4, h5, h6, p, blockquote, pre,
8
+ address, code, img,
9
+ dl, dt, dd, ol, ul, li,
10
+ fieldset, form, label,
11
+ table, th, td,
12
+ article, aside, nav, section, figure, figcaption, footer, header,
13
+ audio, video {
14
+ margin: 0;
15
+ padding: 0;
16
+ }
17
+ blockquote, img, fieldset, form {
18
+ border: 0;
19
+ }
20
+ a, strong, em, b, i, small, sub, sup, img, label, th, td, audio, video {
21
+ vertical-align: baseline;
22
+ }
23
+ applet, object, iframe,
24
+ abbr, acronym, big, cite,
25
+ del, dfn, ins, kbd, q, s, samp,
26
+ strike, tt, var, u, center, legend,
27
+ caption, tbody, tfoot, thead, tr,
28
+ canvas, details, embed,
29
+ menu, output, ruby, summary,
30
+ time, mark {
31
+ margin: 0;
32
+ padding: 0;
33
+ border: 0;
34
+ vertical-align: baseline;
35
+ font: inherit;
36
+ font-size: 100%;
37
+ }
38
+ ul, ol {
39
+ list-style: none;
40
+ }
41
+
42
+ /* Border-box FTW
43
+ https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ */
44
+ *,
45
+ *:before,
46
+ *:after {
47
+ box-sizing: inherit;
48
+ }
49
+ html {
50
+ overflow-y: scroll;
51
+ box-sizing: border-box;
52
+ text-size-adjust: 100%;
53
+ }
54
+ a {
55
+ background-color: transparent; /* Remove the gray background color from active links in IE 10. */
56
+ -webkit-text-decoration-skip: none;
57
+
58
+ &:hover,
59
+ &:active {
60
+ outline: 0;
61
+ }
62
+ }
63
+ img {
64
+ vertical-align: middle;
65
+ }
66
+ strong, b {
67
+ font-weight: bold;
68
+ }
69
+ em, i {
70
+ font-style: italic;
71
+ }
72
+ h1, h2, h3, h4, h5, h6 {
73
+ font-weight: bold;
74
+ }
75
+ table {
76
+ border-spacing: 0;
77
+ border-collapse: collapse;
78
+ }
79
+ th {
80
+ font-weight: bold;
81
+ }
82
+ td {
83
+ vertical-align: top;
84
+ }
85
+ input,
86
+ select,
87
+ textarea,
88
+ button {
89
+ margin: 0;
90
+ vertical-align: middle;
91
+ font-size: 100%;
92
+ font-family: inherit;
93
+ }
94
+
95
+ /**
96
+ * 1. Add the correct box sizing in IE 10-.
97
+ * 2. Remove the padding in IE 10-.
98
+ */
99
+ [type="checkbox"],
100
+ [type="radio"] {
101
+ box-sizing: border-box; /* 1 */
102
+ padding: 0; /* 2 */
103
+ }
104
+
105
+ /**
106
+ * Show the overflow in IE.
107
+ * 1. Show the overflow in Edge.
108
+ * 2. Show the overflow in Edge, Firefox, and IE.
109
+ */
110
+ button,
111
+ input, /* 1 */
112
+ select { /* 2 */
113
+ overflow: visible;
114
+ }
115
+
116
+ /**
117
+ * Address inconsistent `text-transform` inheritance for `button` and `select`.
118
+ * All other form control elements do not inherit `text-transform` values.
119
+ * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera.
120
+ * Correct `select` style inheritance in Firefox.
121
+ */
122
+ button,
123
+ select {
124
+ text-transform: none;
125
+ }
126
+
127
+ /**
128
+ * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
129
+ * controls in Android 4.
130
+ * 2. Correct inability to style clickable `input` types in iOS.
131
+ * 3. Improve usability and consistency of cursor style between image-type
132
+ * `input` and others.
133
+ */
134
+ button,
135
+ html [type="button"], /* 1 */
136
+ [type="reset"],
137
+ [type="submit"] {
138
+ cursor: pointer; /* 3 */
139
+ -webkit-appearance: button; /* 2 */
140
+ }
141
+
142
+ /**
143
+ * Remove the inner border and padding in Firefox.
144
+ */
145
+ button::-moz-focus-inner,
146
+ input::-moz-focus-inner {
147
+ padding: 0;
148
+ border: 0;
149
+ }
150
+ /**
151
+ * 1. Remove the default vertical scrollbar in IE.
152
+ */
153
+ textarea {
154
+ overflow: auto; /* 1 */
155
+ resize: vertical;
156
+ }
157
+ svg:not(:root) {
158
+ overflow: hidden; /* Correct overflow not hidden in IE. */
159
+ }
160
+
161
+ /**
162
+ * Correct the odd appearance of search inputs in Chrome and Safari.
163
+ */
164
+ [type="search"] {
165
+ -webkit-appearance: textfield;
166
+ }
167
+
168
+ /**
169
+ * Remove the inner padding and cancel buttons in Chrome on OS X and
170
+ * Safari on OS X.
171
+ */
172
+ [type="search"]::-webkit-search-cancel-button,
173
+ [type="search"]::-webkit-search-decoration {
174
+ -webkit-appearance: none;
175
+ }
176
+ /* stylelint-enable */
177
+
178
+ [role="button"],
179
+ input[type="submit"],
180
+ input[type="reset"],
181
+ input[type="button"],
182
+ button {
183
+ -webkit-box-sizing: content-box;
184
+ -moz-box-sizing: content-box;
185
+ box-sizing: content-box;
186
+ }
187
+
188
+ /* Reset `button` and button-style `input` default styles */
189
+ input[type="submit"],
190
+ input[type="reset"],
191
+ input[type="button"],
192
+ button {
193
+ background: none;
194
+ border: 0;
195
+ color: inherit;
196
+ /* cursor: default; */
197
+ font: inherit;
198
+ line-height: normal;
199
+ overflow: visible;
200
+ padding: 0;
201
+ -webkit-appearance: button; /* for input */
202
+ -webkit-user-select: none; /* for button */
203
+ -moz-user-select: none;
204
+ -ms-user-select: none;
205
+ }
206
+ input::-moz-focus-inner,
207
+ button::-moz-focus-inner {
208
+ border: 0;
209
+ padding: 0;
210
+ }
211
+
212
+ /* Make `a` like a button */
213
+ [role="button"] {
214
+ color: inherit;
215
+ cursor: default;
216
+ display: inline-block;
217
+ text-align: center;
218
+ text-decoration: none;
219
+ white-space: pre;
220
+ -webkit-user-select: none;
221
+ -moz-user-select: none;
222
+ -ms-user-select: none;
223
+ }
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+ require "litecable"
3
+
4
+ # Sample chat application
5
+ module Chat
6
+ class Connection < LiteCable::Connection::Base # :nodoc:
7
+ identified_by :user, :sid
8
+
9
+ def connect
10
+ @user = cookies["user"]
11
+ @sid = request.params["sid"]
12
+ reject_unauthorized_connection unless @user
13
+ $stdout.puts "#{@user} connected"
14
+ end
15
+
16
+ def disconnect
17
+ $stdout.puts "#{@user} disconnected"
18
+ end
19
+ end
20
+
21
+ class Channel < LiteCable::Channel::Base # :nodoc:
22
+ identifier :chat
23
+
24
+ def subscribed
25
+ reject unless chat_id
26
+ stream_from "chat_#{chat_id}"
27
+ end
28
+
29
+ def speak(data)
30
+ LiteCable.broadcast "chat_#{chat_id}", user: user, message: data["message"], sid: sid
31
+ end
32
+
33
+ private
34
+
35
+ def chat_id
36
+ params.fetch("id")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ lib = File.expand_path("../../../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require './app'
6
+ require './chat'
7
+
8
+ LiteCable.config.log_level = Logger::DEBUG
9
+
10
+ app = Rack::Builder.new do
11
+ map '/' do
12
+ run App
13
+ end
14
+ end
15
+
16
+ if ENV['ANYCABLE']
17
+ # Turn AnyCable compatibility mode
18
+ LiteCable.anycable!
19
+ else
20
+ require "lite_cable/server"
21
+
22
+ app.map '/cable' do
23
+ use LiteCable::Server::Middleware, connection_class: Chat::Connection
24
+ run proc { |_| [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
25
+ end
26
+ end
27
+
28
+ run app
@@ -0,0 +1,8 @@
1
+ h2 Room Id
2
+
3
+ form action="/rooms" method="POST"
4
+ .row
5
+ input type="text" required="required" name="id"
6
+ .row
7
+ button.btn type="submit"
8
+ span Go!
@@ -0,0 +1,15 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title LiteCable Sinatra Demo
5
+ meta name="viewport" content="width=device-width"
6
+ meta charset="UTF-8"
7
+ link rel="stylesheet" href="/reset.css"
8
+ link rel="stylesheet" href="/app.css"
9
+ script type="text/javascript" src="/cable.js"
10
+ body
11
+ .header
12
+ h1.title
13
+ a href='/' LiteCable
14
+ .container.main
15
+ == yield
@@ -0,0 +1,8 @@
1
+ h2 Your Name
2
+
3
+ form action="/sign_in" method="POST"
4
+ .row
5
+ input type="text" required="required" name="user"
6
+ .row
7
+ button.btn type="submit"
8
+ span Go!
@@ -0,0 +1,224 @@
1
+ css:
2
+ /* Reset
3
+ ----------------------------------------------------------------------------- */
4
+
5
+ /* stylelint-disable */
6
+
7
+ html, body,
8
+ h1, h2, h3, h4, h5, h6, p, blockquote, pre,
9
+ address, code, img,
10
+ dl, dt, dd, ol, ul, li,
11
+ fieldset, form, label,
12
+ table, th, td,
13
+ article, aside, nav, section, figure, figcaption, footer, header,
14
+ audio, video {
15
+ margin: 0;
16
+ padding: 0;
17
+ }
18
+ blockquote, img, fieldset, form {
19
+ border: 0;
20
+ }
21
+ a, strong, em, b, i, small, sub, sup, img, label, th, td, audio, video {
22
+ vertical-align: baseline;
23
+ }
24
+ applet, object, iframe,
25
+ abbr, acronym, big, cite,
26
+ del, dfn, ins, kbd, q, s, samp,
27
+ strike, tt, var, u, center, legend,
28
+ caption, tbody, tfoot, thead, tr,
29
+ canvas, details, embed,
30
+ menu, output, ruby, summary,
31
+ time, mark {
32
+ margin: 0;
33
+ padding: 0;
34
+ border: 0;
35
+ vertical-align: baseline;
36
+ font: inherit;
37
+ font-size: 100%;
38
+ }
39
+ ul, ol {
40
+ list-style: none;
41
+ }
42
+
43
+ /* Border-box FTW
44
+ https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ */
45
+ *,
46
+ *:before,
47
+ *:after {
48
+ box-sizing: inherit;
49
+ }
50
+ html {
51
+ overflow-y: scroll;
52
+ box-sizing: border-box;
53
+ text-size-adjust: 100%;
54
+ }
55
+ a {
56
+ background-color: transparent; /* Remove the gray background color from active links in IE 10. */
57
+ -webkit-text-decoration-skip: none;
58
+
59
+ &:hover,
60
+ &:active {
61
+ outline: 0;
62
+ }
63
+ }
64
+ img {
65
+ vertical-align: middle;
66
+ }
67
+ strong, b {
68
+ font-weight: bold;
69
+ }
70
+ em, i {
71
+ font-style: italic;
72
+ }
73
+ h1, h2, h3, h4, h5, h6 {
74
+ font-weight: bold;
75
+ }
76
+ table {
77
+ border-spacing: 0;
78
+ border-collapse: collapse;
79
+ }
80
+ th {
81
+ font-weight: bold;
82
+ }
83
+ td {
84
+ vertical-align: top;
85
+ }
86
+ input,
87
+ select,
88
+ textarea,
89
+ button {
90
+ margin: 0;
91
+ vertical-align: middle;
92
+ font-size: 100%;
93
+ font-family: inherit;
94
+ }
95
+
96
+ /**
97
+ * 1. Add the correct box sizing in IE 10-.
98
+ * 2. Remove the padding in IE 10-.
99
+ */
100
+ [type="checkbox"],
101
+ [type="radio"] {
102
+ box-sizing: border-box; /* 1 */
103
+ padding: 0; /* 2 */
104
+ }
105
+
106
+ /**
107
+ * Show the overflow in IE.
108
+ * 1. Show the overflow in Edge.
109
+ * 2. Show the overflow in Edge, Firefox, and IE.
110
+ */
111
+ button,
112
+ input, /* 1 */
113
+ select { /* 2 */
114
+ overflow: visible;
115
+ }
116
+
117
+ /**
118
+ * Address inconsistent `text-transform` inheritance for `button` and `select`.
119
+ * All other form control elements do not inherit `text-transform` values.
120
+ * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera.
121
+ * Correct `select` style inheritance in Firefox.
122
+ */
123
+ button,
124
+ select {
125
+ text-transform: none;
126
+ }
127
+
128
+ /**
129
+ * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
130
+ * controls in Android 4.
131
+ * 2. Correct inability to style clickable `input` types in iOS.
132
+ * 3. Improve usability and consistency of cursor style between image-type
133
+ * `input` and others.
134
+ */
135
+ button,
136
+ html [type="button"], /* 1 */
137
+ [type="reset"],
138
+ [type="submit"] {
139
+ cursor: pointer; /* 3 */
140
+ -webkit-appearance: button; /* 2 */
141
+ }
142
+
143
+ /**
144
+ * Remove the inner border and padding in Firefox.
145
+ */
146
+ button::-moz-focus-inner,
147
+ input::-moz-focus-inner {
148
+ padding: 0;
149
+ border: 0;
150
+ }
151
+ /**
152
+ * 1. Remove the default vertical scrollbar in IE.
153
+ */
154
+ textarea {
155
+ overflow: auto; /* 1 */
156
+ resize: vertical;
157
+ }
158
+ svg:not(:root) {
159
+ overflow: hidden; /* Correct overflow not hidden in IE. */
160
+ }
161
+
162
+ /**
163
+ * Correct the odd appearance of search inputs in Chrome and Safari.
164
+ */
165
+ [type="search"] {
166
+ -webkit-appearance: textfield;
167
+ }
168
+
169
+ /**
170
+ * Remove the inner padding and cancel buttons in Chrome on OS X and
171
+ * Safari on OS X.
172
+ */
173
+ [type="search"]::-webkit-search-cancel-button,
174
+ [type="search"]::-webkit-search-decoration {
175
+ -webkit-appearance: none;
176
+ }
177
+ /* stylelint-enable */
178
+
179
+ [role="button"],
180
+ input[type="submit"],
181
+ input[type="reset"],
182
+ input[type="button"],
183
+ button {
184
+ -webkit-box-sizing: content-box;
185
+ -moz-box-sizing: content-box;
186
+ box-sizing: content-box;
187
+ }
188
+
189
+ /* Reset `button` and button-style `input` default styles */
190
+ input[type="submit"],
191
+ input[type="reset"],
192
+ input[type="button"],
193
+ button {
194
+ background: none;
195
+ border: 0;
196
+ color: inherit;
197
+ /* cursor: default; */
198
+ font: inherit;
199
+ line-height: normal;
200
+ overflow: visible;
201
+ padding: 0;
202
+ -webkit-appearance: button; /* for input */
203
+ -webkit-user-select: none; /* for button */
204
+ -moz-user-select: none;
205
+ -ms-user-select: none;
206
+ }
207
+ input::-moz-focus-inner,
208
+ button::-moz-focus-inner {
209
+ border: 0;
210
+ padding: 0;
211
+ }
212
+
213
+ /* Make `a` like a button */
214
+ [role="button"] {
215
+ color: inherit;
216
+ cursor: default;
217
+ display: inline-block;
218
+ text-align: center;
219
+ text-decoration: none;
220
+ white-space: pre;
221
+ -webkit-user-select: none;
222
+ -moz-user-select: none;
223
+ -ms-user-select: none;
224
+ }