websocket-rails 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,14 +4,14 @@ module WebsocketRails
4
4
  class Dispatcher
5
5
 
6
6
  include Logging
7
-
7
+
8
8
  attr_reader :event_map, :connection_manager
9
-
9
+
10
10
  def initialize(connection_manager)
11
11
  @connection_manager = connection_manager
12
12
  @event_map = EventMap.new( self )
13
13
  end
14
-
14
+
15
15
  def receive_encoded(encoded_data,connection)
16
16
  event = Event.new_from_json( encoded_data, connection )
17
17
  dispatch( event )
@@ -21,20 +21,23 @@ module WebsocketRails
21
21
  event = Event.new event_name, data, connection
22
22
  dispatch( event )
23
23
  end
24
-
24
+
25
25
  def dispatch(event)
26
+ return if event.is_invalid?
27
+
26
28
  log "Event received: #{event.name}"
29
+
27
30
  if event.is_channel?
28
31
  WebsocketRails[event.channel].trigger_event event
29
32
  else
30
33
  route event
31
34
  end
32
35
  end
33
-
36
+
34
37
  def send_message(event)
35
38
  event.connection.trigger event
36
39
  end
37
-
40
+
38
41
  def broadcast_message(event)
39
42
  connection_manager.connections.map do |connection|
40
43
  connection.trigger event
@@ -49,12 +52,12 @@ module WebsocketRails
49
52
 
50
53
  def route(event)
51
54
  actions = []
52
- event_map.routes_for event do |controller,method|
55
+ event_map.routes_for event do |controller, method|
53
56
  actions << Fiber.new do
54
57
  begin
55
58
  controller.instance_variable_set(:@_event,event)
56
- controller.send :execute_observers, event.name if controller.respond_to?(:execute_observers)
57
- result = controller.send method if controller.respond_to?(method)
59
+ controller.send(:execute_observers, event.name) if controller.respond_to?(:execute_observers)
60
+ result = controller.send(method) if controller.respond_to?(method)
58
61
  rescue Exception => ex
59
62
  puts ex.backtrace
60
63
  puts "Application Exception: #{ex}"
@@ -22,6 +22,10 @@ module WebsocketRails
22
22
  Event.new :ping, :data => {}, :connection => connection, :namespace => :websocket_rails
23
23
  end
24
24
 
25
+ def new_on_invalid_event_received(connection,data=nil)
26
+ Event.new :invalid_event, :data => data, :connection => connection
27
+ end
28
+
25
29
  end
26
30
 
27
31
  # Contains all of the relevant information for incoming and outgoing events.
@@ -56,11 +60,13 @@ module WebsocketRails
56
60
  class Event
57
61
 
58
62
  def self.new_from_json(encoded_data,connection)
63
+ log "Event Data: #{encoded_data}"
59
64
  event_name, data = JSON.parse encoded_data
60
65
  data = data.merge(:connection => connection).with_indifferent_access
61
66
  Event.new event_name, data
62
67
  rescue JSON::ParserError => ex
63
68
  warn "Invalid Event Received: #{ex}"
69
+ Event.new_on_invalid_event_received(connection, nil)
64
70
  end
65
71
 
66
72
  include Logging
@@ -68,22 +74,23 @@ module WebsocketRails
68
74
 
69
75
  attr_reader :id, :name, :connection, :namespace, :channel
70
76
 
71
- attr_accessor :data, :result, :success
77
+ attr_accessor :data, :result, :success, :server_token
72
78
 
73
79
  def initialize(event_name,options={})
74
80
  case event_name
75
81
  when String
76
- namespace = event_name.split('.')
77
- @name = namespace.pop.to_sym
82
+ namespace = event_name.split('.')
83
+ @name = namespace.pop.to_sym
78
84
  when Symbol
79
- @name = event_name
80
- namespace = [:global]
85
+ @name = event_name
86
+ namespace = [:global]
81
87
  end
82
- @id = options[:id]
83
- @data = options[:data].is_a?(Hash) ? options[:data].with_indifferent_access : options[:data]
84
- @channel = options[:channel].to_sym if options[:channel]
85
- @connection = options[:connection]
86
- @namespace = validate_namespace( options[:namespace] || namespace )
88
+ @id = options[:id]
89
+ @data = options[:data].is_a?(Hash) ? options[:data].with_indifferent_access : options[:data]
90
+ @channel = options[:channel].to_sym if options[:channel]
91
+ @connection = options[:connection]
92
+ @server_token = options[:server_token]
93
+ @namespace = validate_namespace( options[:namespace] || namespace )
87
94
  end
88
95
 
89
96
  def serialize
@@ -94,7 +101,8 @@ module WebsocketRails
94
101
  :channel => channel,
95
102
  :data => data,
96
103
  :success => success,
97
- :result => result
104
+ :result => result,
105
+ :server_token => server_token
98
106
  }
99
107
  ].to_json
100
108
  end
@@ -103,6 +111,10 @@ module WebsocketRails
103
111
  !@channel.nil?
104
112
  end
105
113
 
114
+ def is_invalid?
115
+ name == :invalid_event
116
+ end
117
+
106
118
  def trigger
107
119
  connection.trigger self if connection
108
120
  end
@@ -18,19 +18,19 @@ module WebsocketRails
18
18
  # subscribe :new, :to => ProductController, :with_method => :new
19
19
  # end
20
20
  class EventMap
21
-
21
+
22
22
  def self.describe(&block)
23
23
  WebsocketRails.route_block = block
24
24
  end
25
25
 
26
26
  attr_reader :namespace
27
-
27
+
28
28
  def initialize(dispatcher)
29
29
  @dispatcher = dispatcher
30
30
  @namespace = DSL.new(dispatcher).evaluate WebsocketRails.route_block
31
31
  @namespace = DSL.new(dispatcher,@namespace).evaluate InternalEvents.events
32
32
  end
33
-
33
+
34
34
  def routes_for(event, &block)
35
35
  @namespace.routes_for event, &block
36
36
  end
@@ -39,7 +39,7 @@ module WebsocketRails
39
39
  def reload_controllers!
40
40
  @namespace.reload_controllers!
41
41
  end
42
-
42
+
43
43
  # Provides the DSL methods available to the Event routes file
44
44
  class DSL
45
45
 
@@ -50,7 +50,7 @@ module WebsocketRails
50
50
  @namespace = Namespace.new :global, dispatcher
51
51
  end
52
52
  end
53
-
53
+
54
54
  def evaluate(route_block)
55
55
  instance_eval &route_block unless route_block.nil?
56
56
  @namespace
@@ -72,7 +72,7 @@ module WebsocketRails
72
72
 
73
73
  end
74
74
 
75
- # Stores route map for nested namespaces
75
+ # Stores route map for nested namespaces
76
76
  class Namespace
77
77
 
78
78
  include Logging
@@ -171,8 +171,8 @@ module WebsocketRails
171
171
  namespace = event.namespace.dup if namespace.nil?
172
172
  namespace
173
173
  end
174
-
174
+
175
175
  end
176
-
176
+
177
177
  end
178
178
  end
@@ -1,7 +1,11 @@
1
1
  module WebsocketRails
2
2
  # Need to replace this module with a real logger
3
3
  module Logging
4
-
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
5
9
  def log(msg)
6
10
  puts msg if debug?
7
11
  end
@@ -11,7 +15,19 @@ module WebsocketRails
11
15
  end
12
16
 
13
17
  def debug?
14
- LOG_LEVEL == :debug
18
+ WebsocketRails.log_level == :debug
19
+ end
20
+
21
+ module ClassMethods
22
+
23
+ def log(msg)
24
+ puts msg if debug?
25
+ end
26
+
27
+ def debug?
28
+ WebsocketRails.log_level == :debug
29
+ end
30
+
15
31
  end
16
32
 
17
33
  end
@@ -0,0 +1,92 @@
1
+ require "redis"
2
+ require "redis/connection/synchrony"
3
+
4
+ module WebsocketRails
5
+ class Synchronization
6
+
7
+ include Logging
8
+
9
+ def self.redis
10
+ @redis ||= Redis.new(WebsocketRails.redis_options)
11
+ end
12
+
13
+ def self.publish(event)
14
+ Fiber.new do
15
+ event.server_token = server_token
16
+ redis.publish "websocket_rails.events", event.serialize
17
+ end.resume
18
+ end
19
+
20
+ def self.server_token
21
+ @server_token
22
+ end
23
+
24
+ def self.synchronize!
25
+ unless @synchronizing
26
+ @server_token = generate_unique_token
27
+ register_server(@server_token)
28
+
29
+ synchro = Fiber.new do
30
+ EM::Synchrony.sleep(0.1)
31
+
32
+ fiber_redis = Redis.connect(WebsocketRails.redis_options)
33
+ fiber_redis.subscribe "websocket_rails.events" do |on|
34
+
35
+ on.message do |channel, encoded_event|
36
+ event = Event.new_from_json(encoded_event, nil)
37
+ next if event.server_token == server_token
38
+
39
+ WebsocketRails[event.channel].trigger_event(event)
40
+ end
41
+ end
42
+
43
+ log "Beginning Synchronization"
44
+ end
45
+
46
+ @synchronizing = true
47
+
48
+ EM.next_tick { synchro.resume }
49
+
50
+ trap('TERM') do
51
+ shutdown!
52
+ end
53
+ trap('INT') do
54
+ shutdown!
55
+ end
56
+ trap('QUIT') do
57
+ shutdown!
58
+ end
59
+ end
60
+ end
61
+
62
+ def self.shutdown!
63
+ remove_server(server_token)
64
+ end
65
+
66
+ private
67
+
68
+ def self.generate_unique_token
69
+ begin
70
+ token = SecureRandom.urlsafe_base64
71
+ end while redis.sismember("websocket_rails.active_servers", token)
72
+
73
+ token
74
+ end
75
+
76
+ def self.register_server(token)
77
+ Fiber.new do
78
+ redis.sadd "websocket_rails.active_servers", token
79
+ log "Server Registered: #{token}"
80
+ end.resume
81
+ end
82
+
83
+ def self.remove_server(token)
84
+ Fiber.new do
85
+ redis.srem "websocket_rails.active_servers", token
86
+ log "Server Removed: #{token}"
87
+ EM.stop
88
+ end.resume
89
+ end
90
+
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module WebsocketRails
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -278,3 +278,303 @@ Connecting to database specified by database.yml
278
278
  Connecting to database specified by database.yml
279
279
  Connecting to database specified by database.yml
280
280
  Connecting to database specified by database.yml
281
+ Connecting to database specified by database.yml
282
+ Connecting to database specified by database.yml
283
+ Connecting to database specified by database.yml
284
+ Connecting to database specified by database.yml
285
+ Connecting to database specified by database.yml
286
+ Connecting to database specified by database.yml
287
+ Connecting to database specified by database.yml
288
+ Connecting to database specified by database.yml
289
+ Connecting to database specified by database.yml
290
+ Connecting to database specified by database.yml
291
+ Connecting to database specified by database.yml
292
+ Connecting to database specified by database.yml
293
+ Connecting to database specified by database.yml
294
+ Connecting to database specified by database.yml
295
+ Connecting to database specified by database.yml
296
+ Connecting to database specified by database.yml
297
+ Connecting to database specified by database.yml
298
+ Connecting to database specified by database.yml
299
+ Connecting to database specified by database.yml
300
+ Connecting to database specified by database.yml
301
+ Connecting to database specified by database.yml
302
+ Connecting to database specified by database.yml
303
+ Connecting to database specified by database.yml
304
+ Connecting to database specified by database.yml
305
+ Connecting to database specified by database.yml
306
+ Connecting to database specified by database.yml
307
+ Connecting to database specified by database.yml
308
+ Connecting to database specified by database.yml
309
+ Connecting to database specified by database.yml
310
+ Connecting to database specified by database.yml
311
+ Connecting to database specified by database.yml
312
+ Connecting to database specified by database.yml
313
+ Connecting to database specified by database.yml
314
+ Connecting to database specified by database.yml
315
+ Connecting to database specified by database.yml
316
+ Connecting to database specified by database.yml
317
+ Connecting to database specified by database.yml
318
+ Connecting to database specified by database.yml
319
+ Connecting to database specified by database.yml
320
+ Connecting to database specified by database.yml
321
+ Connecting to database specified by database.yml
322
+ Connecting to database specified by database.yml
323
+ Connecting to database specified by database.yml
324
+ Connecting to database specified by database.yml
325
+ Connecting to database specified by database.yml
326
+ Connecting to database specified by database.yml
327
+ Connecting to database specified by database.yml
328
+ Connecting to database specified by database.yml
329
+ Connecting to database specified by database.yml
330
+ Connecting to database specified by database.yml
331
+ Connecting to database specified by database.yml
332
+ Connecting to database specified by database.yml
333
+ Connecting to database specified by database.yml
334
+ Connecting to database specified by database.yml
335
+ Connecting to database specified by database.yml
336
+ Connecting to database specified by database.yml
337
+ Connecting to database specified by database.yml
338
+ Connecting to database specified by database.yml
339
+ Connecting to database specified by database.yml
340
+ Connecting to database specified by database.yml
341
+ Connecting to database specified by database.yml
342
+ Connecting to database specified by database.yml
343
+ Connecting to database specified by database.yml
344
+ Connecting to database specified by database.yml
345
+ Connecting to database specified by database.yml
346
+ Connecting to database specified by database.yml
347
+ Connecting to database specified by database.yml
348
+ Connecting to database specified by database.yml
349
+ Connecting to database specified by database.yml
350
+ Connecting to database specified by database.yml
351
+ Connecting to database specified by database.yml
352
+ Connecting to database specified by database.yml
353
+ Connecting to database specified by database.yml
354
+ Connecting to database specified by database.yml
355
+ Connecting to database specified by database.yml
356
+ Connecting to database specified by database.yml
357
+ Connecting to database specified by database.yml
358
+ Connecting to database specified by database.yml
359
+ Connecting to database specified by database.yml
360
+ Connecting to database specified by database.yml
361
+ Connecting to database specified by database.yml
362
+ Connecting to database specified by database.yml
363
+ Connecting to database specified by database.yml
364
+ Connecting to database specified by database.yml
365
+ Connecting to database specified by database.yml
366
+ Connecting to database specified by database.yml
367
+ Connecting to database specified by database.yml
368
+ Connecting to database specified by database.yml
369
+ Connecting to database specified by database.yml
370
+ Connecting to database specified by database.yml
371
+ Connecting to database specified by database.yml
372
+ Connecting to database specified by database.yml
373
+ Connecting to database specified by database.yml
374
+ Connecting to database specified by database.yml
375
+ Connecting to database specified by database.yml
376
+ Connecting to database specified by database.yml
377
+ Connecting to database specified by database.yml
378
+ Connecting to database specified by database.yml
379
+ Connecting to database specified by database.yml
380
+ Connecting to database specified by database.yml
381
+ Connecting to database specified by database.yml
382
+ Connecting to database specified by database.yml
383
+ Connecting to database specified by database.yml
384
+ Connecting to database specified by database.yml
385
+ Connecting to database specified by database.yml
386
+ Connecting to database specified by database.yml
387
+ Connecting to database specified by database.yml
388
+ Connecting to database specified by database.yml
389
+ Connecting to database specified by database.yml
390
+ Connecting to database specified by database.yml
391
+ Connecting to database specified by database.yml
392
+ Connecting to database specified by database.yml
393
+ Connecting to database specified by database.yml
394
+ Connecting to database specified by database.yml
395
+ Connecting to database specified by database.yml
396
+ Connecting to database specified by database.yml
397
+ Connecting to database specified by database.yml
398
+ Connecting to database specified by database.yml
399
+ Connecting to database specified by database.yml
400
+ Connecting to database specified by database.yml
401
+ Connecting to database specified by database.yml
402
+ Connecting to database specified by database.yml
403
+ Connecting to database specified by database.yml
404
+ Connecting to database specified by database.yml
405
+ Connecting to database specified by database.yml
406
+ Connecting to database specified by database.yml
407
+ Connecting to database specified by database.yml
408
+ Connecting to database specified by database.yml
409
+ Connecting to database specified by database.yml
410
+ Connecting to database specified by database.yml
411
+ Connecting to database specified by database.yml
412
+ Connecting to database specified by database.yml
413
+ Connecting to database specified by database.yml
414
+ Connecting to database specified by database.yml
415
+ Connecting to database specified by database.yml
416
+ Connecting to database specified by database.yml
417
+ Connecting to database specified by database.yml
418
+ Connecting to database specified by database.yml
419
+ Connecting to database specified by database.yml
420
+ Connecting to database specified by database.yml
421
+ Connecting to database specified by database.yml
422
+ Connecting to database specified by database.yml
423
+ Connecting to database specified by database.yml
424
+ Connecting to database specified by database.yml
425
+ Connecting to database specified by database.yml
426
+ Connecting to database specified by database.yml
427
+ Connecting to database specified by database.yml
428
+ Connecting to database specified by database.yml
429
+ Connecting to database specified by database.yml
430
+ Connecting to database specified by database.yml
431
+ Connecting to database specified by database.yml
432
+ Connecting to database specified by database.yml
433
+ Connecting to database specified by database.yml
434
+ Connecting to database specified by database.yml
435
+ Connecting to database specified by database.yml
436
+ Connecting to database specified by database.yml
437
+ Connecting to database specified by database.yml
438
+ Connecting to database specified by database.yml
439
+ Connecting to database specified by database.yml
440
+ Connecting to database specified by database.yml
441
+ Connecting to database specified by database.yml
442
+ Connecting to database specified by database.yml
443
+ Connecting to database specified by database.yml
444
+ Connecting to database specified by database.yml
445
+ Connecting to database specified by database.yml
446
+ Connecting to database specified by database.yml
447
+ Connecting to database specified by database.yml
448
+ Connecting to database specified by database.yml
449
+ Connecting to database specified by database.yml
450
+ Connecting to database specified by database.yml
451
+ Connecting to database specified by database.yml
452
+ Connecting to database specified by database.yml
453
+ Connecting to database specified by database.yml
454
+ Connecting to database specified by database.yml
455
+ Connecting to database specified by database.yml
456
+ Connecting to database specified by database.yml
457
+ Connecting to database specified by database.yml
458
+ Connecting to database specified by database.yml
459
+ Connecting to database specified by database.yml
460
+ Connecting to database specified by database.yml
461
+ Connecting to database specified by database.yml
462
+ Connecting to database specified by database.yml
463
+ Connecting to database specified by database.yml
464
+ Connecting to database specified by database.yml
465
+ Connecting to database specified by database.yml
466
+ Connecting to database specified by database.yml
467
+ Connecting to database specified by database.yml
468
+ Connecting to database specified by database.yml
469
+ Connecting to database specified by database.yml
470
+ Connecting to database specified by database.yml
471
+ Connecting to database specified by database.yml
472
+ Connecting to database specified by database.yml
473
+ Connecting to database specified by database.yml
474
+ Connecting to database specified by database.yml
475
+ Connecting to database specified by database.yml
476
+ Connecting to database specified by database.yml
477
+ Connecting to database specified by database.yml
478
+ Connecting to database specified by database.yml
479
+ Connecting to database specified by database.yml
480
+ Connecting to database specified by database.yml
481
+ Connecting to database specified by database.yml
482
+ Connecting to database specified by database.yml
483
+ Connecting to database specified by database.yml
484
+ Connecting to database specified by database.yml
485
+ Connecting to database specified by database.yml
486
+ Connecting to database specified by database.yml
487
+ Connecting to database specified by database.yml
488
+ Connecting to database specified by database.yml
489
+ Connecting to database specified by database.yml
490
+ Connecting to database specified by database.yml
491
+ Connecting to database specified by database.yml
492
+ Connecting to database specified by database.yml
493
+ Connecting to database specified by database.yml
494
+ Connecting to database specified by database.yml
495
+ Connecting to database specified by database.yml
496
+ Connecting to database specified by database.yml
497
+ Connecting to database specified by database.yml
498
+ Connecting to database specified by database.yml
499
+ Connecting to database specified by database.yml
500
+ Connecting to database specified by database.yml
501
+ Connecting to database specified by database.yml
502
+ Connecting to database specified by database.yml
503
+ Connecting to database specified by database.yml
504
+ Connecting to database specified by database.yml
505
+ Connecting to database specified by database.yml
506
+ Connecting to database specified by database.yml
507
+ Connecting to database specified by database.yml
508
+ Connecting to database specified by database.yml
509
+ Connecting to database specified by database.yml
510
+ Connecting to database specified by database.yml
511
+ Connecting to database specified by database.yml
512
+ Connecting to database specified by database.yml
513
+ Connecting to database specified by database.yml
514
+ Connecting to database specified by database.yml
515
+ Connecting to database specified by database.yml
516
+ Connecting to database specified by database.yml
517
+ Connecting to database specified by database.yml
518
+ Connecting to database specified by database.yml
519
+ Connecting to database specified by database.yml
520
+ Connecting to database specified by database.yml
521
+ Connecting to database specified by database.yml
522
+ Connecting to database specified by database.yml
523
+ Connecting to database specified by database.yml
524
+ Connecting to database specified by database.yml
525
+ Connecting to database specified by database.yml
526
+ Connecting to database specified by database.yml
527
+ Connecting to database specified by database.yml
528
+ Connecting to database specified by database.yml
529
+ Connecting to database specified by database.yml
530
+ Connecting to database specified by database.yml
531
+ Connecting to database specified by database.yml
532
+ Connecting to database specified by database.yml
533
+ Connecting to database specified by database.yml
534
+ Connecting to database specified by database.yml
535
+ Connecting to database specified by database.yml
536
+ Connecting to database specified by database.yml
537
+ Connecting to database specified by database.yml
538
+ Connecting to database specified by database.yml
539
+ Connecting to database specified by database.yml
540
+ Connecting to database specified by database.yml
541
+ Connecting to database specified by database.yml
542
+ Connecting to database specified by database.yml
543
+ Connecting to database specified by database.yml
544
+ Connecting to database specified by database.yml
545
+ Connecting to database specified by database.yml
546
+ Connecting to database specified by database.yml
547
+ Connecting to database specified by database.yml
548
+ Connecting to database specified by database.yml
549
+ Connecting to database specified by database.yml
550
+ Connecting to database specified by database.yml
551
+ Connecting to database specified by database.yml
552
+ Connecting to database specified by database.yml
553
+ Connecting to database specified by database.yml
554
+ Connecting to database specified by database.yml
555
+ Connecting to database specified by database.yml
556
+ Connecting to database specified by database.yml
557
+ Connecting to database specified by database.yml
558
+ Connecting to database specified by database.yml
559
+ Connecting to database specified by database.yml
560
+ Connecting to database specified by database.yml
561
+ Connecting to database specified by database.yml
562
+ Connecting to database specified by database.yml
563
+ Connecting to database specified by database.yml
564
+ Connecting to database specified by database.yml
565
+ Connecting to database specified by database.yml
566
+ Connecting to database specified by database.yml
567
+ Connecting to database specified by database.yml
568
+ Connecting to database specified by database.yml
569
+ Connecting to database specified by database.yml
570
+ Connecting to database specified by database.yml
571
+ Connecting to database specified by database.yml
572
+ Connecting to database specified by database.yml
573
+ Connecting to database specified by database.yml
574
+ Connecting to database specified by database.yml
575
+ Connecting to database specified by database.yml
576
+ Connecting to database specified by database.yml
577
+ Connecting to database specified by database.yml
578
+ Connecting to database specified by database.yml
579
+ Connecting to database specified by database.yml
580
+ Connecting to database specified by database.yml