celluloid_pubsub 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4275 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Class: CelluloidPubsub::WebServer
8
+
9
+ &mdash; Documentation by YARD 0.8.7.6
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '../';
20
+ framesUrl = "../frames.html#!CelluloidPubsub/WebServer.html";
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="../_index.html">Index (W)</a> &raquo;
35
+ <span class='title'><span class='object_link'><a href="../CelluloidPubsub.html" title="CelluloidPubsub (module)">CelluloidPubsub</a></span></span>
36
+ &raquo;
37
+ <span class="title">WebServer</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="../class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="../method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="../file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Class: CelluloidPubsub::WebServer
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+ <dt class="r1">Inherits:</dt>
75
+ <dd class="r1">
76
+ <span class="inheritName">Reel::Server::HTTP</span>
77
+
78
+ <ul class="fullTree">
79
+ <li>Object</li>
80
+
81
+ <li class="next">Reel::Server::HTTP</li>
82
+
83
+ <li class="next">CelluloidPubsub::WebServer</li>
84
+
85
+ </ul>
86
+ <a href="#" class="inheritanceTree">show all</a>
87
+
88
+ </dd>
89
+
90
+
91
+
92
+
93
+
94
+
95
+ <dt class="r2">Includes:</dt>
96
+ <dd class="r2">Celluloid::Logger</dd>
97
+
98
+
99
+
100
+
101
+
102
+ <dt class="r1 last">Defined in:</dt>
103
+ <dd class="r1 last">lib/celluloid_pubsub/web_server.rb</dd>
104
+
105
+ </dl>
106
+ <div class="clear"></div>
107
+
108
+ <h2>Overview</h2><div class="docstring">
109
+ <div class="discussion">
110
+
111
+ <p>webserver to which socket connects should connect to . the server will
112
+ dispatch each request into a new Reactor which will handle the action based
113
+ on the message</p>
114
+
115
+
116
+ </div>
117
+ </div>
118
+ <div class="tags">
119
+
120
+
121
+ </div>
122
+ <h2>Constant Summary</h2>
123
+
124
+ <dl class="constants">
125
+
126
+ <dt id="HOST-constant" class="">HOST =
127
+ <div class="docstring">
128
+ <div class="discussion">
129
+
130
+ <p>The hostname on which the webserver runs on by default</p>
131
+
132
+
133
+ </div>
134
+ </div>
135
+ <div class="tags">
136
+
137
+
138
+ </div>
139
+ </dt>
140
+ <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span></pre></dd>
141
+
142
+ <dt id="PORT-constant" class="">PORT =
143
+ <div class="docstring">
144
+ <div class="discussion">
145
+
146
+ <p>The port on which the webserver runs on by default</p>
147
+
148
+
149
+ </div>
150
+ </div>
151
+ <div class="tags">
152
+
153
+
154
+ </div>
155
+ </dt>
156
+ <dd><pre class="code"><span class='int'>1234</span></pre></dd>
157
+
158
+ <dt id="PATH-constant" class="">PATH =
159
+ <div class="docstring">
160
+ <div class="discussion">
161
+
162
+ <p>The request path that the webserver accepts by default</p>
163
+
164
+
165
+ </div>
166
+ </div>
167
+ <div class="tags">
168
+
169
+
170
+ </div>
171
+ </dt>
172
+ <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span></pre></dd>
173
+
174
+ </dl>
175
+
176
+
177
+
178
+
179
+
180
+ <h2>Instance Attribute Summary <small>(<a href="#" class="summary_toggle">collapse</a>)</small></h2>
181
+ <ul class="summary">
182
+
183
+ <li class="public ">
184
+ <span class="summary_signature">
185
+
186
+ <a href="#backlog-instance_method" title="#backlog (instance method)">- (Integer) <strong>backlog</strong> </a>
187
+
188
+
189
+
190
+ </span>
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+ <span class="summary_desc"><div class='inline'>
204
+ <p>Defaults to 1024.</p>
205
+ </div></span>
206
+
207
+ </li>
208
+
209
+
210
+ <li class="public ">
211
+ <span class="summary_signature">
212
+
213
+ <a href="#hostname-instance_method" title="#hostname (instance method)">- (String) <strong>hostname</strong> </a>
214
+
215
+
216
+
217
+ </span>
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+ <span class="summary_desc"><div class='inline'>
231
+ <p>The hostname on which the webserver runs on.</p>
232
+ </div></span>
233
+
234
+ </li>
235
+
236
+
237
+ <li class="public ">
238
+ <span class="summary_signature">
239
+
240
+ <a href="#options-instance_method" title="#options (instance method)">- (Hash) <strong>options</strong> </a>
241
+
242
+
243
+
244
+ </span>
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+ <span class="summary_desc"><div class='inline'>
258
+ <p>Options used to configure the webserver.</p>
259
+ </div></span>
260
+
261
+ </li>
262
+
263
+
264
+ <li class="public ">
265
+ <span class="summary_signature">
266
+
267
+ <a href="#path-instance_method" title="#path (instance method)">- (String) <strong>path</strong> </a>
268
+
269
+
270
+
271
+ </span>
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+ <span class="summary_desc"><div class='inline'>
285
+ <p>The hostname on which the webserver runs on.</p>
286
+ </div></span>
287
+
288
+ </li>
289
+
290
+
291
+ <li class="public ">
292
+ <span class="summary_signature">
293
+
294
+ <a href="#port-instance_method" title="#port (instance method)">- (String) <strong>port</strong> </a>
295
+
296
+
297
+
298
+ </span>
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ <span class="summary_desc"><div class='inline'>
312
+ <p>The port on which the webserver runs on.</p>
313
+ </div></span>
314
+
315
+ </li>
316
+
317
+
318
+ <li class="public ">
319
+ <span class="summary_signature">
320
+
321
+ <a href="#spy-instance_method" title="#spy (instance method)">- (Boolean) <strong>spy</strong> </a>
322
+
323
+
324
+
325
+ </span>
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+ <span class="summary_desc"><div class='inline'>
339
+ <p>Enable this only if you want to enable debugging for the webserver.</p>
340
+ </div></span>
341
+
342
+ </li>
343
+
344
+
345
+ <li class="public ">
346
+ <span class="summary_signature">
347
+
348
+ <a href="#subscribers-instance_method" title="#subscribers (instance method)">- (Hash) <strong>subscribers</strong> </a>
349
+
350
+
351
+
352
+ </span>
353
+
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+ <span class="summary_desc"><div class='inline'>
366
+ <p>The hostname on which the webserver runs on.</p>
367
+ </div></span>
368
+
369
+ </li>
370
+
371
+
372
+ </ul>
373
+
374
+
375
+
376
+
377
+
378
+ <h2>
379
+ Class Method Summary
380
+ <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
381
+ </h2>
382
+
383
+ <ul class="summary">
384
+
385
+ <li class="public ">
386
+ <span class="summary_signature">
387
+
388
+ <a href="#debug_enabled%3F-class_method" title="debug_enabled? (class method)">+ (boolean) <strong>debug_enabled?</strong> </a>
389
+
390
+
391
+
392
+ </span>
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+ <span class="summary_desc"><div class='inline'>
403
+ <p>checks if debug is enabled.</p>
404
+ </div></span>
405
+
406
+ </li>
407
+
408
+
409
+ </ul>
410
+
411
+ <h2>
412
+ Instance Method Summary
413
+ <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
414
+ </h2>
415
+
416
+ <ul class="summary">
417
+
418
+ <li class="public ">
419
+ <span class="summary_signature">
420
+
421
+ <a href="#debug_enabled%3F-instance_method" title="#debug_enabled? (instance method)">- (boolean) <strong>debug_enabled?</strong> </a>
422
+
423
+
424
+
425
+ </span>
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+ <span class="summary_desc"><div class='inline'>
436
+ <p>checks if debug is enabled.</p>
437
+ </div></span>
438
+
439
+ </li>
440
+
441
+
442
+ <li class="public ">
443
+ <span class="summary_signature">
444
+
445
+ <a href="#handle_dispatched_message-instance_method" title="#handle_dispatched_message (instance method)">- (void) <strong>handle_dispatched_message</strong>(reactor, data) </a>
446
+
447
+
448
+
449
+ </span>
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+ <span class="summary_desc"><div class='inline'>
460
+ <p>If the message can be parsed into a Hash it will respond to the
461
+ reactor&#39;s websocket connection with the same message in JSON format
462
+ otherwise will try send the message how it is and escaped into JSON format.</p>
463
+ </div></span>
464
+
465
+ </li>
466
+
467
+
468
+ <li class="public ">
469
+ <span class="summary_signature">
470
+
471
+ <a href="#initialize-instance_method" title="#initialize (instance method)">- (void) <strong>initialize</strong>(options = {}) </a>
472
+
473
+
474
+
475
+ </span>
476
+
477
+
478
+ <span class="note title constructor">constructor</span>
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+ <span class="summary_desc"><div class='inline'>
488
+ <p>receives a list of options that are used to configure the webserver.</p>
489
+ </div></span>
490
+
491
+ </li>
492
+
493
+
494
+ <li class="public ">
495
+ <span class="summary_signature">
496
+
497
+ <a href="#on_connection-instance_method" title="#on_connection (instance method)">- (void) <strong>on_connection</strong>(connection) </a>
498
+
499
+
500
+
501
+ </span>
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+
510
+
511
+ <span class="summary_desc"><div class='inline'>
512
+ <p>callback that will execute when receiving new conections If the connections
513
+ is a websocket will call method <span class='object_link'><a href="#route_websocket-instance_method" title="CelluloidPubsub::WebServer#route_websocket (method)">#route_websocket</a></span> and if the connection is
514
+ HTTP will call method <span class='object_link'><a href="#route_request-instance_method" title="CelluloidPubsub::WebServer#route_request (method)">#route_request</a></span> For websocket connections , the
515
+ connection is detached from the server and dispatched to another actor.</p>
516
+ </div></span>
517
+
518
+ </li>
519
+
520
+
521
+ <li class="public ">
522
+ <span class="summary_signature">
523
+
524
+ <a href="#parse_options-instance_method" title="#parse_options (instance method)">- (void) <strong>parse_options</strong>(options) </a>
525
+
526
+
527
+
528
+ </span>
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+ <span class="summary_desc"><div class='inline'>
539
+ <p>receives a list of options that are used to configure the webserver.</p>
540
+ </div></span>
541
+
542
+ </li>
543
+
544
+
545
+ <li class="public ">
546
+ <span class="summary_signature">
547
+
548
+ <a href="#publish_event-instance_method" title="#publish_event (instance method)">- (void) <strong>publish_event</strong>(current_topic, message) </a>
549
+
550
+
551
+
552
+ </span>
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+ <span class="summary_desc"><div class='inline'>
563
+ <p>checks if debug is enabled.</p>
564
+ </div></span>
565
+
566
+ </li>
567
+
568
+
569
+ <li class="public ">
570
+ <span class="summary_signature">
571
+
572
+ <a href="#route_request-instance_method" title="#route_request (instance method)">- (void) <strong>route_request</strong>(connection, request) </a>
573
+
574
+
575
+
576
+ </span>
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+
585
+
586
+ <span class="summary_desc"><div class='inline'>
587
+ <p>HTTP connections are not accepted so this method will show 404 message “Not
588
+ Found”.</p>
589
+ </div></span>
590
+
591
+ </li>
592
+
593
+
594
+ <li class="public ">
595
+ <span class="summary_signature">
596
+
597
+ <a href="#route_websocket-instance_method" title="#route_websocket (instance method)">- (void) <strong>route_websocket</strong>(socket) </a>
598
+
599
+
600
+
601
+ </span>
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+ <span class="summary_desc"><div class='inline'>
612
+ <p>If the socket url matches with the one accepted by the server, it will
613
+ dispatch the socket connection to a new reactor
614
+ <span class='object_link'><a href="Reactor.html#work-instance_method" title="CelluloidPubsub::Reactor#work (method)">Reactor#work</a></span> The new actor is linked to the webserver.</p>
615
+ </div></span>
616
+
617
+ </li>
618
+
619
+
620
+ </ul>
621
+
622
+
623
+
624
+
625
+ <div id="constructor_details" class="method_details_list">
626
+ <h2>Constructor Details</h2>
627
+
628
+ <div class="method_details first">
629
+ <h3 class="signature first" id="initialize-instance_method">
630
+
631
+ - (<tt>void</tt>) <strong>initialize</strong>(options = {})
632
+
633
+
634
+
635
+
636
+
637
+ </h3><div class="docstring">
638
+ <div class="discussion">
639
+
640
+ <p>receives a list of options that are used to configure the webserver</p>
641
+
642
+
643
+ </div>
644
+ </div>
645
+ <div class="tags">
646
+ <p class="tag_title">Parameters:</p>
647
+ <ul class="param">
648
+
649
+ <li>
650
+
651
+ <span class='name'>options</span>
652
+
653
+
654
+ <span class='type'>(<tt>Hash</tt>)</span>
655
+
656
+
657
+ <em class="default">(defaults to: <tt>{}</tt>)</em>
658
+
659
+
660
+ &mdash;
661
+ <div class='inline'>
662
+ <p>the options that can be used to connect to webser and send additional data</p>
663
+ </div>
664
+
665
+ </li>
666
+
667
+ </ul>
668
+
669
+
670
+
671
+
672
+ <p class="tag_title">Options Hash (<tt>options</tt>):</p>
673
+ <ul class="option">
674
+
675
+ <li>
676
+ <span class="name">:hostname</span>
677
+ <span class="type">(<tt>String</tt>)</span>
678
+ <span class="default">
679
+
680
+ </span>
681
+
682
+ &mdash; <div class='inline'>
683
+ <p>The hostname on which the webserver runs on</p>
684
+ </div>
685
+
686
+ </li>
687
+
688
+ <li>
689
+ <span class="name">:port</span>
690
+ <span class="type">(<tt>Integer</tt>)</span>
691
+ <span class="default">
692
+
693
+ </span>
694
+
695
+ &mdash; <div class='inline'>
696
+ <p>The port on which the webserver runs on</p>
697
+ </div>
698
+
699
+ </li>
700
+
701
+ <li>
702
+ <span class="name">:path</span>
703
+ <span class="type">(<tt>String</tt>)</span>
704
+ <span class="default">
705
+
706
+ </span>
707
+
708
+ &mdash; <div class='inline'>
709
+ <p>The request path that the webserver accepts</p>
710
+ </div>
711
+
712
+ </li>
713
+
714
+ <li>
715
+ <span class="name">:spy</span>
716
+ <span class="type">(<tt>Boolean</tt>)</span>
717
+ <span class="default">
718
+
719
+ </span>
720
+
721
+ &mdash; <div class='inline'>
722
+ <p>Enable this only if you want to enable debugging for the webserver</p>
723
+ </div>
724
+
725
+ </li>
726
+
727
+ </ul>
728
+
729
+
730
+
731
+ </div><table class="source_code">
732
+ <tr>
733
+ <td>
734
+ <pre class="lines">
735
+
736
+
737
+ 54
738
+ 55
739
+ 56
740
+ 57
741
+ 58
742
+ 59</pre>
743
+ </td>
744
+ <td>
745
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 54</span>
746
+
747
+ <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
748
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
749
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
750
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
751
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
752
+ <span class='kw'>end</span></pre>
753
+ </td>
754
+ </tr>
755
+ </table>
756
+ </div>
757
+
758
+ </div>
759
+
760
+ <div id="instance_attr_details" class="attr_details">
761
+ <h2>Instance Attribute Details</h2>
762
+
763
+
764
+ <span id="backlog=-instance_method"></span>
765
+ <div class="method_details first">
766
+ <h3 class="signature first" id="backlog-instance_method">
767
+
768
+ - (<tt>Integer</tt>) <strong>backlog</strong>
769
+
770
+
771
+
772
+
773
+
774
+ </h3><div class="docstring">
775
+ <div class="discussion">
776
+
777
+ <p>Defaults to 1024</p>
778
+
779
+
780
+ </div>
781
+ </div>
782
+ <div class="tags">
783
+
784
+ <p class="tag_title">Returns:</p>
785
+ <ul class="return">
786
+
787
+ <li>
788
+
789
+
790
+ <span class='type'>(<tt>Integer</tt>)</span>
791
+
792
+
793
+
794
+ &mdash;
795
+ <div class='inline'>
796
+ <p>Determines how many connections can be used</p>
797
+ </div>
798
+
799
+ </li>
800
+
801
+ </ul>
802
+
803
+ </div><table class="source_code">
804
+ <tr>
805
+ <td>
806
+ <pre class="lines">
807
+
808
+
809
+ 31
810
+ 32
811
+ 33
812
+ 34
813
+ 35
814
+ 36
815
+ 37
816
+ 38
817
+ 39
818
+ 40
819
+ 41
820
+ 42
821
+ 43
822
+ 44
823
+ 45
824
+ 46
825
+ 47
826
+ 48
827
+ 49
828
+ 50
829
+ 51
830
+ 52
831
+ 53
832
+ 54
833
+ 55
834
+ 56
835
+ 57
836
+ 58
837
+ 59
838
+ 60
839
+ 61
840
+ 62
841
+ 63
842
+ 64
843
+ 65
844
+ 66
845
+ 67
846
+ 68
847
+ 69
848
+ 70
849
+ 71
850
+ 72
851
+ 73
852
+ 74
853
+ 75
854
+ 76
855
+ 77
856
+ 78
857
+ 79
858
+ 80
859
+ 81
860
+ 82
861
+ 83
862
+ 84
863
+ 85
864
+ 86
865
+ 87
866
+ 88
867
+ 89
868
+ 90
869
+ 91
870
+ 92
871
+ 93
872
+ 94
873
+ 95
874
+ 96
875
+ 97
876
+ 98
877
+ 99
878
+ 100
879
+ 101
880
+ 102
881
+ 103
882
+ 104
883
+ 105
884
+ 106
885
+ 107
886
+ 108
887
+ 109
888
+ 110
889
+ 111
890
+ 112
891
+ 113
892
+ 114
893
+ 115
894
+ 116
895
+ 117
896
+ 118
897
+ 119
898
+ 120
899
+ 121
900
+ 122
901
+ 123
902
+ 124
903
+ 125
904
+ 126
905
+ 127
906
+ 128
907
+ 129
908
+ 130
909
+ 131
910
+ 132
911
+ 133
912
+ 134
913
+ 135
914
+ 136
915
+ 137
916
+ 138
917
+ 139
918
+ 140
919
+ 141
920
+ 142
921
+ 143
922
+ 144
923
+ 145
924
+ 146
925
+ 147
926
+ 148
927
+ 149
928
+ 150
929
+ 151
930
+ 152
931
+ 153
932
+ 154
933
+ 155
934
+ 156
935
+ 157
936
+ 158
937
+ 159
938
+ 160
939
+ 161
940
+ 162
941
+ 163
942
+ 164
943
+ 165
944
+ 166
945
+ 167
946
+ 168
947
+ 169
948
+ 170
949
+ 171
950
+ 172
951
+ 173
952
+ 174
953
+ 175
954
+ 176
955
+ 177
956
+ 178
957
+ 179
958
+ 180
959
+ 181
960
+ 182
961
+ 183
962
+ 184
963
+ 185
964
+ 186
965
+ 187
966
+ 188
967
+ 189
968
+ 190
969
+ 191
970
+ 192
971
+ 193
972
+ 194
973
+ 195
974
+ 196
975
+ 197
976
+ 198
977
+ 199
978
+ 200
979
+ 201
980
+ 202
981
+ 203</pre>
982
+ </td>
983
+ <td>
984
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
985
+
986
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
987
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
988
+
989
+ <span class='comment'># The hostname on which the webserver runs on by default
990
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
991
+ <span class='comment'># The port on which the webserver runs on by default
992
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
993
+ <span class='comment'># The request path that the webserver accepts by default
994
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
995
+
996
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
997
+
998
+ <span class='comment'># receives a list of options that are used to configure the webserver
999
+ </span> <span class='comment'>#
1000
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1001
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1002
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1003
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1004
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1005
+ </span> <span class='comment'>#
1006
+ </span> <span class='comment'># @return [void]
1007
+ </span> <span class='comment'>#
1008
+ </span> <span class='comment'># @api public
1009
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
1010
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1011
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
1012
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1013
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
1014
+ <span class='kw'>end</span>
1015
+
1016
+ <span class='comment'># receives a list of options that are used to configure the webserver
1017
+ </span> <span class='comment'>#
1018
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1019
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1020
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1021
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1022
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1023
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
1024
+ </span> <span class='comment'>#
1025
+ </span> <span class='comment'># @return [void]
1026
+ </span> <span class='comment'>#
1027
+ </span> <span class='comment'># @api public
1028
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1029
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1030
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
1031
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
1032
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
1033
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
1034
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
1035
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
1036
+ <span class='kw'>end</span>
1037
+
1038
+ <span class='comment'># checks if debug is enabled
1039
+ </span> <span class='comment'>#
1040
+ </span> <span class='comment'># @return [boolean]
1041
+ </span> <span class='comment'>#
1042
+ </span> <span class='comment'># @api public
1043
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1044
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1045
+ <span class='kw'>end</span>
1046
+
1047
+ <span class='comment'># checks if debug is enabled
1048
+ </span> <span class='comment'>#
1049
+ </span> <span class='comment'># @return [boolean]
1050
+ </span> <span class='comment'>#
1051
+ </span> <span class='comment'># @api public
1052
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1053
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
1054
+ <span class='kw'>end</span>
1055
+
1056
+ <span class='comment'># checks if debug is enabled
1057
+ </span> <span class='comment'>#
1058
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
1059
+ </span> <span class='comment'># @param [Object] message
1060
+ </span> <span class='comment'>#
1061
+ </span> <span class='comment'># @return [void]
1062
+ </span> <span class='comment'>#
1063
+ </span> <span class='comment'># @api public
1064
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
1065
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
1066
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
1067
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
1068
+ <span class='kw'>end</span>
1069
+ <span class='kw'>end</span>
1070
+
1071
+ <span class='comment'># callback that will execute when receiving new conections
1072
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
1073
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
1074
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
1075
+ </span> <span class='comment'>#
1076
+ </span> <span class='comment'># @see #route_websocket
1077
+ </span> <span class='comment'># @see #route_request
1078
+ </span> <span class='comment'>#
1079
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
1080
+ </span> <span class='comment'>#
1081
+ </span> <span class='comment'># @return [void]
1082
+ </span> <span class='comment'>#
1083
+ </span> <span class='comment'># @api public
1084
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
1085
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
1086
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
1087
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1088
+
1089
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
1090
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
1091
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
1092
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
1093
+ </span> <span class='comment'>#
1094
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
1095
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
1096
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
1097
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
1098
+ <span class='kw'>return</span>
1099
+ <span class='kw'>else</span>
1100
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
1101
+ <span class='kw'>end</span>
1102
+ <span class='kw'>end</span>
1103
+ <span class='kw'>end</span>
1104
+
1105
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
1106
+ </span> <span class='comment'>#
1107
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
1108
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
1109
+ </span> <span class='comment'>#
1110
+ </span> <span class='comment'># @return [void]
1111
+ </span> <span class='comment'>#
1112
+ </span> <span class='comment'># @api public
1113
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
1114
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1115
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
1116
+ <span class='kw'>end</span>
1117
+
1118
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
1119
+ </span> <span class='comment'># The new actor is linked to the webserver
1120
+ </span> <span class='comment'>#
1121
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
1122
+ </span> <span class='comment'>#
1123
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
1124
+ </span> <span class='comment'>#
1125
+ </span> <span class='comment'># @return [void]
1126
+ </span> <span class='comment'>#
1127
+ </span> <span class='comment'># @api public
1128
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
1129
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
1130
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1131
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
1132
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
1133
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
1134
+ <span class='kw'>else</span>
1135
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1136
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
1137
+ <span class='kw'>end</span>
1138
+ <span class='kw'>end</span>
1139
+
1140
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
1141
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
1142
+ </span> <span class='comment'>#
1143
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
1144
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
1145
+ </span> <span class='comment'>#
1146
+ </span> <span class='comment'># @return [void]
1147
+ </span> <span class='comment'>#
1148
+ </span> <span class='comment'># @api public
1149
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1150
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1151
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1152
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1153
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1154
+ <span class='kw'>else</span>
1155
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1156
+ <span class='kw'>end</span>
1157
+ <span class='kw'>end</span>
1158
+ <span class='kw'>end</span></pre>
1159
+ </td>
1160
+ </tr>
1161
+ </table>
1162
+ </div>
1163
+
1164
+
1165
+ <span id="hostname=-instance_method"></span>
1166
+ <div class="method_details ">
1167
+ <h3 class="signature " id="hostname-instance_method">
1168
+
1169
+ - (<tt>String</tt>) <strong>hostname</strong>
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+ </h3><div class="docstring">
1176
+ <div class="discussion">
1177
+
1178
+ <p>Returns The hostname on which the webserver runs on</p>
1179
+
1180
+
1181
+ </div>
1182
+ </div>
1183
+ <div class="tags">
1184
+
1185
+ <p class="tag_title">Returns:</p>
1186
+ <ul class="return">
1187
+
1188
+ <li>
1189
+
1190
+
1191
+ <span class='type'>(<tt>String</tt>)</span>
1192
+
1193
+
1194
+
1195
+ &mdash;
1196
+ <div class='inline'>
1197
+ <p>The hostname on which the webserver runs on</p>
1198
+ </div>
1199
+
1200
+ </li>
1201
+
1202
+ </ul>
1203
+
1204
+ </div><table class="source_code">
1205
+ <tr>
1206
+ <td>
1207
+ <pre class="lines">
1208
+
1209
+
1210
+ 31
1211
+ 32
1212
+ 33
1213
+ 34
1214
+ 35
1215
+ 36
1216
+ 37
1217
+ 38
1218
+ 39
1219
+ 40
1220
+ 41
1221
+ 42
1222
+ 43
1223
+ 44
1224
+ 45
1225
+ 46
1226
+ 47
1227
+ 48
1228
+ 49
1229
+ 50
1230
+ 51
1231
+ 52
1232
+ 53
1233
+ 54
1234
+ 55
1235
+ 56
1236
+ 57
1237
+ 58
1238
+ 59
1239
+ 60
1240
+ 61
1241
+ 62
1242
+ 63
1243
+ 64
1244
+ 65
1245
+ 66
1246
+ 67
1247
+ 68
1248
+ 69
1249
+ 70
1250
+ 71
1251
+ 72
1252
+ 73
1253
+ 74
1254
+ 75
1255
+ 76
1256
+ 77
1257
+ 78
1258
+ 79
1259
+ 80
1260
+ 81
1261
+ 82
1262
+ 83
1263
+ 84
1264
+ 85
1265
+ 86
1266
+ 87
1267
+ 88
1268
+ 89
1269
+ 90
1270
+ 91
1271
+ 92
1272
+ 93
1273
+ 94
1274
+ 95
1275
+ 96
1276
+ 97
1277
+ 98
1278
+ 99
1279
+ 100
1280
+ 101
1281
+ 102
1282
+ 103
1283
+ 104
1284
+ 105
1285
+ 106
1286
+ 107
1287
+ 108
1288
+ 109
1289
+ 110
1290
+ 111
1291
+ 112
1292
+ 113
1293
+ 114
1294
+ 115
1295
+ 116
1296
+ 117
1297
+ 118
1298
+ 119
1299
+ 120
1300
+ 121
1301
+ 122
1302
+ 123
1303
+ 124
1304
+ 125
1305
+ 126
1306
+ 127
1307
+ 128
1308
+ 129
1309
+ 130
1310
+ 131
1311
+ 132
1312
+ 133
1313
+ 134
1314
+ 135
1315
+ 136
1316
+ 137
1317
+ 138
1318
+ 139
1319
+ 140
1320
+ 141
1321
+ 142
1322
+ 143
1323
+ 144
1324
+ 145
1325
+ 146
1326
+ 147
1327
+ 148
1328
+ 149
1329
+ 150
1330
+ 151
1331
+ 152
1332
+ 153
1333
+ 154
1334
+ 155
1335
+ 156
1336
+ 157
1337
+ 158
1338
+ 159
1339
+ 160
1340
+ 161
1341
+ 162
1342
+ 163
1343
+ 164
1344
+ 165
1345
+ 166
1346
+ 167
1347
+ 168
1348
+ 169
1349
+ 170
1350
+ 171
1351
+ 172
1352
+ 173
1353
+ 174
1354
+ 175
1355
+ 176
1356
+ 177
1357
+ 178
1358
+ 179
1359
+ 180
1360
+ 181
1361
+ 182
1362
+ 183
1363
+ 184
1364
+ 185
1365
+ 186
1366
+ 187
1367
+ 188
1368
+ 189
1369
+ 190
1370
+ 191
1371
+ 192
1372
+ 193
1373
+ 194
1374
+ 195
1375
+ 196
1376
+ 197
1377
+ 198
1378
+ 199
1379
+ 200
1380
+ 201
1381
+ 202
1382
+ 203</pre>
1383
+ </td>
1384
+ <td>
1385
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
1386
+
1387
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
1388
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
1389
+
1390
+ <span class='comment'># The hostname on which the webserver runs on by default
1391
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
1392
+ <span class='comment'># The port on which the webserver runs on by default
1393
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
1394
+ <span class='comment'># The request path that the webserver accepts by default
1395
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
1396
+
1397
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
1398
+
1399
+ <span class='comment'># receives a list of options that are used to configure the webserver
1400
+ </span> <span class='comment'>#
1401
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1402
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1403
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1404
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1405
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1406
+ </span> <span class='comment'>#
1407
+ </span> <span class='comment'># @return [void]
1408
+ </span> <span class='comment'>#
1409
+ </span> <span class='comment'># @api public
1410
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
1411
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1412
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
1413
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1414
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
1415
+ <span class='kw'>end</span>
1416
+
1417
+ <span class='comment'># receives a list of options that are used to configure the webserver
1418
+ </span> <span class='comment'>#
1419
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1420
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1421
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1422
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1423
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1424
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
1425
+ </span> <span class='comment'>#
1426
+ </span> <span class='comment'># @return [void]
1427
+ </span> <span class='comment'>#
1428
+ </span> <span class='comment'># @api public
1429
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1430
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1431
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
1432
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
1433
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
1434
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
1435
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
1436
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
1437
+ <span class='kw'>end</span>
1438
+
1439
+ <span class='comment'># checks if debug is enabled
1440
+ </span> <span class='comment'>#
1441
+ </span> <span class='comment'># @return [boolean]
1442
+ </span> <span class='comment'>#
1443
+ </span> <span class='comment'># @api public
1444
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1445
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1446
+ <span class='kw'>end</span>
1447
+
1448
+ <span class='comment'># checks if debug is enabled
1449
+ </span> <span class='comment'>#
1450
+ </span> <span class='comment'># @return [boolean]
1451
+ </span> <span class='comment'>#
1452
+ </span> <span class='comment'># @api public
1453
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1454
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
1455
+ <span class='kw'>end</span>
1456
+
1457
+ <span class='comment'># checks if debug is enabled
1458
+ </span> <span class='comment'>#
1459
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
1460
+ </span> <span class='comment'># @param [Object] message
1461
+ </span> <span class='comment'>#
1462
+ </span> <span class='comment'># @return [void]
1463
+ </span> <span class='comment'>#
1464
+ </span> <span class='comment'># @api public
1465
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
1466
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
1467
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
1468
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
1469
+ <span class='kw'>end</span>
1470
+ <span class='kw'>end</span>
1471
+
1472
+ <span class='comment'># callback that will execute when receiving new conections
1473
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
1474
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
1475
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
1476
+ </span> <span class='comment'>#
1477
+ </span> <span class='comment'># @see #route_websocket
1478
+ </span> <span class='comment'># @see #route_request
1479
+ </span> <span class='comment'>#
1480
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
1481
+ </span> <span class='comment'>#
1482
+ </span> <span class='comment'># @return [void]
1483
+ </span> <span class='comment'>#
1484
+ </span> <span class='comment'># @api public
1485
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
1486
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
1487
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
1488
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1489
+
1490
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
1491
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
1492
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
1493
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
1494
+ </span> <span class='comment'>#
1495
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
1496
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
1497
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
1498
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
1499
+ <span class='kw'>return</span>
1500
+ <span class='kw'>else</span>
1501
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
1502
+ <span class='kw'>end</span>
1503
+ <span class='kw'>end</span>
1504
+ <span class='kw'>end</span>
1505
+
1506
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
1507
+ </span> <span class='comment'>#
1508
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
1509
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
1510
+ </span> <span class='comment'>#
1511
+ </span> <span class='comment'># @return [void]
1512
+ </span> <span class='comment'>#
1513
+ </span> <span class='comment'># @api public
1514
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
1515
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1516
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
1517
+ <span class='kw'>end</span>
1518
+
1519
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
1520
+ </span> <span class='comment'># The new actor is linked to the webserver
1521
+ </span> <span class='comment'>#
1522
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
1523
+ </span> <span class='comment'>#
1524
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
1525
+ </span> <span class='comment'>#
1526
+ </span> <span class='comment'># @return [void]
1527
+ </span> <span class='comment'>#
1528
+ </span> <span class='comment'># @api public
1529
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
1530
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
1531
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1532
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
1533
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
1534
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
1535
+ <span class='kw'>else</span>
1536
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1537
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
1538
+ <span class='kw'>end</span>
1539
+ <span class='kw'>end</span>
1540
+
1541
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
1542
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
1543
+ </span> <span class='comment'>#
1544
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
1545
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
1546
+ </span> <span class='comment'>#
1547
+ </span> <span class='comment'># @return [void]
1548
+ </span> <span class='comment'>#
1549
+ </span> <span class='comment'># @api public
1550
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1551
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1552
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1553
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1554
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1555
+ <span class='kw'>else</span>
1556
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1557
+ <span class='kw'>end</span>
1558
+ <span class='kw'>end</span>
1559
+ <span class='kw'>end</span></pre>
1560
+ </td>
1561
+ </tr>
1562
+ </table>
1563
+ </div>
1564
+
1565
+
1566
+ <span id="options=-instance_method"></span>
1567
+ <div class="method_details ">
1568
+ <h3 class="signature " id="options-instance_method">
1569
+
1570
+ - (<tt>Hash</tt>) <strong>options</strong>
1571
+
1572
+
1573
+
1574
+
1575
+
1576
+ </h3><div class="docstring">
1577
+ <div class="discussion">
1578
+
1579
+ <p>Returns options used to configure the webserver</p>
1580
+
1581
+
1582
+ </div>
1583
+ </div>
1584
+ <div class="tags">
1585
+
1586
+
1587
+
1588
+ <p class="tag_title">Returns:</p>
1589
+ <ul class="return">
1590
+
1591
+ <li>
1592
+
1593
+
1594
+ <span class='type'>(<tt>Hash</tt>)</span>
1595
+
1596
+
1597
+
1598
+ &mdash;
1599
+ <div class='inline'>
1600
+ <p>options used to configure the webserver</p>
1601
+ </div>
1602
+
1603
+ </li>
1604
+
1605
+ </ul>
1606
+
1607
+ </div><table class="source_code">
1608
+ <tr>
1609
+ <td>
1610
+ <pre class="lines">
1611
+
1612
+
1613
+ 31
1614
+ 32
1615
+ 33
1616
+ 34
1617
+ 35
1618
+ 36
1619
+ 37
1620
+ 38
1621
+ 39
1622
+ 40
1623
+ 41
1624
+ 42
1625
+ 43
1626
+ 44
1627
+ 45
1628
+ 46
1629
+ 47
1630
+ 48
1631
+ 49
1632
+ 50
1633
+ 51
1634
+ 52
1635
+ 53
1636
+ 54
1637
+ 55
1638
+ 56
1639
+ 57
1640
+ 58
1641
+ 59
1642
+ 60
1643
+ 61
1644
+ 62
1645
+ 63
1646
+ 64
1647
+ 65
1648
+ 66
1649
+ 67
1650
+ 68
1651
+ 69
1652
+ 70
1653
+ 71
1654
+ 72
1655
+ 73
1656
+ 74
1657
+ 75
1658
+ 76
1659
+ 77
1660
+ 78
1661
+ 79
1662
+ 80
1663
+ 81
1664
+ 82
1665
+ 83
1666
+ 84
1667
+ 85
1668
+ 86
1669
+ 87
1670
+ 88
1671
+ 89
1672
+ 90
1673
+ 91
1674
+ 92
1675
+ 93
1676
+ 94
1677
+ 95
1678
+ 96
1679
+ 97
1680
+ 98
1681
+ 99
1682
+ 100
1683
+ 101
1684
+ 102
1685
+ 103
1686
+ 104
1687
+ 105
1688
+ 106
1689
+ 107
1690
+ 108
1691
+ 109
1692
+ 110
1693
+ 111
1694
+ 112
1695
+ 113
1696
+ 114
1697
+ 115
1698
+ 116
1699
+ 117
1700
+ 118
1701
+ 119
1702
+ 120
1703
+ 121
1704
+ 122
1705
+ 123
1706
+ 124
1707
+ 125
1708
+ 126
1709
+ 127
1710
+ 128
1711
+ 129
1712
+ 130
1713
+ 131
1714
+ 132
1715
+ 133
1716
+ 134
1717
+ 135
1718
+ 136
1719
+ 137
1720
+ 138
1721
+ 139
1722
+ 140
1723
+ 141
1724
+ 142
1725
+ 143
1726
+ 144
1727
+ 145
1728
+ 146
1729
+ 147
1730
+ 148
1731
+ 149
1732
+ 150
1733
+ 151
1734
+ 152
1735
+ 153
1736
+ 154
1737
+ 155
1738
+ 156
1739
+ 157
1740
+ 158
1741
+ 159
1742
+ 160
1743
+ 161
1744
+ 162
1745
+ 163
1746
+ 164
1747
+ 165
1748
+ 166
1749
+ 167
1750
+ 168
1751
+ 169
1752
+ 170
1753
+ 171
1754
+ 172
1755
+ 173
1756
+ 174
1757
+ 175
1758
+ 176
1759
+ 177
1760
+ 178
1761
+ 179
1762
+ 180
1763
+ 181
1764
+ 182
1765
+ 183
1766
+ 184
1767
+ 185
1768
+ 186
1769
+ 187
1770
+ 188
1771
+ 189
1772
+ 190
1773
+ 191
1774
+ 192
1775
+ 193
1776
+ 194
1777
+ 195
1778
+ 196
1779
+ 197
1780
+ 198
1781
+ 199
1782
+ 200
1783
+ 201
1784
+ 202
1785
+ 203</pre>
1786
+ </td>
1787
+ <td>
1788
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
1789
+
1790
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
1791
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
1792
+
1793
+ <span class='comment'># The hostname on which the webserver runs on by default
1794
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
1795
+ <span class='comment'># The port on which the webserver runs on by default
1796
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
1797
+ <span class='comment'># The request path that the webserver accepts by default
1798
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
1799
+
1800
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
1801
+
1802
+ <span class='comment'># receives a list of options that are used to configure the webserver
1803
+ </span> <span class='comment'>#
1804
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1805
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1806
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1807
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1808
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1809
+ </span> <span class='comment'>#
1810
+ </span> <span class='comment'># @return [void]
1811
+ </span> <span class='comment'>#
1812
+ </span> <span class='comment'># @api public
1813
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
1814
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1815
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
1816
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1817
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
1818
+ <span class='kw'>end</span>
1819
+
1820
+ <span class='comment'># receives a list of options that are used to configure the webserver
1821
+ </span> <span class='comment'>#
1822
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
1823
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
1824
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
1825
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
1826
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
1827
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
1828
+ </span> <span class='comment'>#
1829
+ </span> <span class='comment'># @return [void]
1830
+ </span> <span class='comment'>#
1831
+ </span> <span class='comment'># @api public
1832
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
1833
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1834
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
1835
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
1836
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
1837
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
1838
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
1839
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
1840
+ <span class='kw'>end</span>
1841
+
1842
+ <span class='comment'># checks if debug is enabled
1843
+ </span> <span class='comment'>#
1844
+ </span> <span class='comment'># @return [boolean]
1845
+ </span> <span class='comment'>#
1846
+ </span> <span class='comment'># @api public
1847
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1848
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1849
+ <span class='kw'>end</span>
1850
+
1851
+ <span class='comment'># checks if debug is enabled
1852
+ </span> <span class='comment'>#
1853
+ </span> <span class='comment'># @return [boolean]
1854
+ </span> <span class='comment'>#
1855
+ </span> <span class='comment'># @api public
1856
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1857
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
1858
+ <span class='kw'>end</span>
1859
+
1860
+ <span class='comment'># checks if debug is enabled
1861
+ </span> <span class='comment'>#
1862
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
1863
+ </span> <span class='comment'># @param [Object] message
1864
+ </span> <span class='comment'>#
1865
+ </span> <span class='comment'># @return [void]
1866
+ </span> <span class='comment'>#
1867
+ </span> <span class='comment'># @api public
1868
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
1869
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
1870
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
1871
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
1872
+ <span class='kw'>end</span>
1873
+ <span class='kw'>end</span>
1874
+
1875
+ <span class='comment'># callback that will execute when receiving new conections
1876
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
1877
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
1878
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
1879
+ </span> <span class='comment'>#
1880
+ </span> <span class='comment'># @see #route_websocket
1881
+ </span> <span class='comment'># @see #route_request
1882
+ </span> <span class='comment'>#
1883
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
1884
+ </span> <span class='comment'>#
1885
+ </span> <span class='comment'># @return [void]
1886
+ </span> <span class='comment'>#
1887
+ </span> <span class='comment'># @api public
1888
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
1889
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
1890
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
1891
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1892
+
1893
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
1894
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
1895
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
1896
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
1897
+ </span> <span class='comment'>#
1898
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
1899
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
1900
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
1901
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
1902
+ <span class='kw'>return</span>
1903
+ <span class='kw'>else</span>
1904
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
1905
+ <span class='kw'>end</span>
1906
+ <span class='kw'>end</span>
1907
+ <span class='kw'>end</span>
1908
+
1909
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
1910
+ </span> <span class='comment'>#
1911
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
1912
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
1913
+ </span> <span class='comment'>#
1914
+ </span> <span class='comment'># @return [void]
1915
+ </span> <span class='comment'>#
1916
+ </span> <span class='comment'># @api public
1917
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
1918
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1919
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
1920
+ <span class='kw'>end</span>
1921
+
1922
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
1923
+ </span> <span class='comment'># The new actor is linked to the webserver
1924
+ </span> <span class='comment'>#
1925
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
1926
+ </span> <span class='comment'>#
1927
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
1928
+ </span> <span class='comment'>#
1929
+ </span> <span class='comment'># @return [void]
1930
+ </span> <span class='comment'>#
1931
+ </span> <span class='comment'># @api public
1932
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
1933
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
1934
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1935
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
1936
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
1937
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
1938
+ <span class='kw'>else</span>
1939
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1940
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
1941
+ <span class='kw'>end</span>
1942
+ <span class='kw'>end</span>
1943
+
1944
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
1945
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
1946
+ </span> <span class='comment'>#
1947
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
1948
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
1949
+ </span> <span class='comment'>#
1950
+ </span> <span class='comment'># @return [void]
1951
+ </span> <span class='comment'>#
1952
+ </span> <span class='comment'># @api public
1953
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1954
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
1955
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
1956
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
1957
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1958
+ <span class='kw'>else</span>
1959
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
1960
+ <span class='kw'>end</span>
1961
+ <span class='kw'>end</span>
1962
+ <span class='kw'>end</span></pre>
1963
+ </td>
1964
+ </tr>
1965
+ </table>
1966
+ </div>
1967
+
1968
+
1969
+ <span id="path=-instance_method"></span>
1970
+ <div class="method_details ">
1971
+ <h3 class="signature " id="path-instance_method">
1972
+
1973
+ - (<tt>String</tt>) <strong>path</strong>
1974
+
1975
+
1976
+
1977
+
1978
+
1979
+ </h3><div class="docstring">
1980
+ <div class="discussion">
1981
+
1982
+ <p>Returns The hostname on which the webserver runs on</p>
1983
+
1984
+
1985
+ </div>
1986
+ </div>
1987
+ <div class="tags">
1988
+
1989
+ <p class="tag_title">Returns:</p>
1990
+ <ul class="return">
1991
+
1992
+ <li>
1993
+
1994
+
1995
+ <span class='type'>(<tt>String</tt>)</span>
1996
+
1997
+
1998
+
1999
+ &mdash;
2000
+ <div class='inline'>
2001
+ <p>The hostname on which the webserver runs on</p>
2002
+ </div>
2003
+
2004
+ </li>
2005
+
2006
+ </ul>
2007
+
2008
+ </div><table class="source_code">
2009
+ <tr>
2010
+ <td>
2011
+ <pre class="lines">
2012
+
2013
+
2014
+ 31
2015
+ 32
2016
+ 33
2017
+ 34
2018
+ 35
2019
+ 36
2020
+ 37
2021
+ 38
2022
+ 39
2023
+ 40
2024
+ 41
2025
+ 42
2026
+ 43
2027
+ 44
2028
+ 45
2029
+ 46
2030
+ 47
2031
+ 48
2032
+ 49
2033
+ 50
2034
+ 51
2035
+ 52
2036
+ 53
2037
+ 54
2038
+ 55
2039
+ 56
2040
+ 57
2041
+ 58
2042
+ 59
2043
+ 60
2044
+ 61
2045
+ 62
2046
+ 63
2047
+ 64
2048
+ 65
2049
+ 66
2050
+ 67
2051
+ 68
2052
+ 69
2053
+ 70
2054
+ 71
2055
+ 72
2056
+ 73
2057
+ 74
2058
+ 75
2059
+ 76
2060
+ 77
2061
+ 78
2062
+ 79
2063
+ 80
2064
+ 81
2065
+ 82
2066
+ 83
2067
+ 84
2068
+ 85
2069
+ 86
2070
+ 87
2071
+ 88
2072
+ 89
2073
+ 90
2074
+ 91
2075
+ 92
2076
+ 93
2077
+ 94
2078
+ 95
2079
+ 96
2080
+ 97
2081
+ 98
2082
+ 99
2083
+ 100
2084
+ 101
2085
+ 102
2086
+ 103
2087
+ 104
2088
+ 105
2089
+ 106
2090
+ 107
2091
+ 108
2092
+ 109
2093
+ 110
2094
+ 111
2095
+ 112
2096
+ 113
2097
+ 114
2098
+ 115
2099
+ 116
2100
+ 117
2101
+ 118
2102
+ 119
2103
+ 120
2104
+ 121
2105
+ 122
2106
+ 123
2107
+ 124
2108
+ 125
2109
+ 126
2110
+ 127
2111
+ 128
2112
+ 129
2113
+ 130
2114
+ 131
2115
+ 132
2116
+ 133
2117
+ 134
2118
+ 135
2119
+ 136
2120
+ 137
2121
+ 138
2122
+ 139
2123
+ 140
2124
+ 141
2125
+ 142
2126
+ 143
2127
+ 144
2128
+ 145
2129
+ 146
2130
+ 147
2131
+ 148
2132
+ 149
2133
+ 150
2134
+ 151
2135
+ 152
2136
+ 153
2137
+ 154
2138
+ 155
2139
+ 156
2140
+ 157
2141
+ 158
2142
+ 159
2143
+ 160
2144
+ 161
2145
+ 162
2146
+ 163
2147
+ 164
2148
+ 165
2149
+ 166
2150
+ 167
2151
+ 168
2152
+ 169
2153
+ 170
2154
+ 171
2155
+ 172
2156
+ 173
2157
+ 174
2158
+ 175
2159
+ 176
2160
+ 177
2161
+ 178
2162
+ 179
2163
+ 180
2164
+ 181
2165
+ 182
2166
+ 183
2167
+ 184
2168
+ 185
2169
+ 186
2170
+ 187
2171
+ 188
2172
+ 189
2173
+ 190
2174
+ 191
2175
+ 192
2176
+ 193
2177
+ 194
2178
+ 195
2179
+ 196
2180
+ 197
2181
+ 198
2182
+ 199
2183
+ 200
2184
+ 201
2185
+ 202
2186
+ 203</pre>
2187
+ </td>
2188
+ <td>
2189
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
2190
+
2191
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
2192
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
2193
+
2194
+ <span class='comment'># The hostname on which the webserver runs on by default
2195
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
2196
+ <span class='comment'># The port on which the webserver runs on by default
2197
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
2198
+ <span class='comment'># The request path that the webserver accepts by default
2199
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
2200
+
2201
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
2202
+
2203
+ <span class='comment'># receives a list of options that are used to configure the webserver
2204
+ </span> <span class='comment'>#
2205
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
2206
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
2207
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
2208
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
2209
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
2210
+ </span> <span class='comment'>#
2211
+ </span> <span class='comment'># @return [void]
2212
+ </span> <span class='comment'>#
2213
+ </span> <span class='comment'># @api public
2214
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
2215
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
2216
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
2217
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2218
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
2219
+ <span class='kw'>end</span>
2220
+
2221
+ <span class='comment'># receives a list of options that are used to configure the webserver
2222
+ </span> <span class='comment'>#
2223
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
2224
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
2225
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
2226
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
2227
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
2228
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
2229
+ </span> <span class='comment'>#
2230
+ </span> <span class='comment'># @return [void]
2231
+ </span> <span class='comment'>#
2232
+ </span> <span class='comment'># @api public
2233
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
2234
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
2235
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
2236
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
2237
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
2238
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
2239
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
2240
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
2241
+ <span class='kw'>end</span>
2242
+
2243
+ <span class='comment'># checks if debug is enabled
2244
+ </span> <span class='comment'>#
2245
+ </span> <span class='comment'># @return [boolean]
2246
+ </span> <span class='comment'>#
2247
+ </span> <span class='comment'># @api public
2248
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2249
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2250
+ <span class='kw'>end</span>
2251
+
2252
+ <span class='comment'># checks if debug is enabled
2253
+ </span> <span class='comment'>#
2254
+ </span> <span class='comment'># @return [boolean]
2255
+ </span> <span class='comment'>#
2256
+ </span> <span class='comment'># @api public
2257
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2258
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
2259
+ <span class='kw'>end</span>
2260
+
2261
+ <span class='comment'># checks if debug is enabled
2262
+ </span> <span class='comment'>#
2263
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
2264
+ </span> <span class='comment'># @param [Object] message
2265
+ </span> <span class='comment'>#
2266
+ </span> <span class='comment'># @return [void]
2267
+ </span> <span class='comment'>#
2268
+ </span> <span class='comment'># @api public
2269
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
2270
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
2271
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
2272
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
2273
+ <span class='kw'>end</span>
2274
+ <span class='kw'>end</span>
2275
+
2276
+ <span class='comment'># callback that will execute when receiving new conections
2277
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
2278
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
2279
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
2280
+ </span> <span class='comment'>#
2281
+ </span> <span class='comment'># @see #route_websocket
2282
+ </span> <span class='comment'># @see #route_request
2283
+ </span> <span class='comment'>#
2284
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
2285
+ </span> <span class='comment'>#
2286
+ </span> <span class='comment'># @return [void]
2287
+ </span> <span class='comment'>#
2288
+ </span> <span class='comment'># @api public
2289
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
2290
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
2291
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
2292
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2293
+
2294
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
2295
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
2296
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
2297
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
2298
+ </span> <span class='comment'>#
2299
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
2300
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
2301
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
2302
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
2303
+ <span class='kw'>return</span>
2304
+ <span class='kw'>else</span>
2305
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
2306
+ <span class='kw'>end</span>
2307
+ <span class='kw'>end</span>
2308
+ <span class='kw'>end</span>
2309
+
2310
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
2311
+ </span> <span class='comment'>#
2312
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
2313
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
2314
+ </span> <span class='comment'>#
2315
+ </span> <span class='comment'># @return [void]
2316
+ </span> <span class='comment'>#
2317
+ </span> <span class='comment'># @api public
2318
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
2319
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2320
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
2321
+ <span class='kw'>end</span>
2322
+
2323
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
2324
+ </span> <span class='comment'># The new actor is linked to the webserver
2325
+ </span> <span class='comment'>#
2326
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
2327
+ </span> <span class='comment'>#
2328
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
2329
+ </span> <span class='comment'>#
2330
+ </span> <span class='comment'># @return [void]
2331
+ </span> <span class='comment'>#
2332
+ </span> <span class='comment'># @api public
2333
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
2334
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
2335
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2336
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
2337
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
2338
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
2339
+ <span class='kw'>else</span>
2340
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2341
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
2342
+ <span class='kw'>end</span>
2343
+ <span class='kw'>end</span>
2344
+
2345
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
2346
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
2347
+ </span> <span class='comment'>#
2348
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
2349
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
2350
+ </span> <span class='comment'>#
2351
+ </span> <span class='comment'># @return [void]
2352
+ </span> <span class='comment'>#
2353
+ </span> <span class='comment'># @api public
2354
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
2355
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2356
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
2357
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
2358
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
2359
+ <span class='kw'>else</span>
2360
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
2361
+ <span class='kw'>end</span>
2362
+ <span class='kw'>end</span>
2363
+ <span class='kw'>end</span></pre>
2364
+ </td>
2365
+ </tr>
2366
+ </table>
2367
+ </div>
2368
+
2369
+
2370
+ <span id="port=-instance_method"></span>
2371
+ <div class="method_details ">
2372
+ <h3 class="signature " id="port-instance_method">
2373
+
2374
+ - (<tt>String</tt>) <strong>port</strong>
2375
+
2376
+
2377
+
2378
+
2379
+
2380
+ </h3><div class="docstring">
2381
+ <div class="discussion">
2382
+
2383
+ <p>Returns The port on which the webserver runs on</p>
2384
+
2385
+
2386
+ </div>
2387
+ </div>
2388
+ <div class="tags">
2389
+
2390
+ <p class="tag_title">Returns:</p>
2391
+ <ul class="return">
2392
+
2393
+ <li>
2394
+
2395
+
2396
+ <span class='type'>(<tt>String</tt>)</span>
2397
+
2398
+
2399
+
2400
+ &mdash;
2401
+ <div class='inline'>
2402
+ <p>The port on which the webserver runs on</p>
2403
+ </div>
2404
+
2405
+ </li>
2406
+
2407
+ </ul>
2408
+
2409
+ </div><table class="source_code">
2410
+ <tr>
2411
+ <td>
2412
+ <pre class="lines">
2413
+
2414
+
2415
+ 31
2416
+ 32
2417
+ 33
2418
+ 34
2419
+ 35
2420
+ 36
2421
+ 37
2422
+ 38
2423
+ 39
2424
+ 40
2425
+ 41
2426
+ 42
2427
+ 43
2428
+ 44
2429
+ 45
2430
+ 46
2431
+ 47
2432
+ 48
2433
+ 49
2434
+ 50
2435
+ 51
2436
+ 52
2437
+ 53
2438
+ 54
2439
+ 55
2440
+ 56
2441
+ 57
2442
+ 58
2443
+ 59
2444
+ 60
2445
+ 61
2446
+ 62
2447
+ 63
2448
+ 64
2449
+ 65
2450
+ 66
2451
+ 67
2452
+ 68
2453
+ 69
2454
+ 70
2455
+ 71
2456
+ 72
2457
+ 73
2458
+ 74
2459
+ 75
2460
+ 76
2461
+ 77
2462
+ 78
2463
+ 79
2464
+ 80
2465
+ 81
2466
+ 82
2467
+ 83
2468
+ 84
2469
+ 85
2470
+ 86
2471
+ 87
2472
+ 88
2473
+ 89
2474
+ 90
2475
+ 91
2476
+ 92
2477
+ 93
2478
+ 94
2479
+ 95
2480
+ 96
2481
+ 97
2482
+ 98
2483
+ 99
2484
+ 100
2485
+ 101
2486
+ 102
2487
+ 103
2488
+ 104
2489
+ 105
2490
+ 106
2491
+ 107
2492
+ 108
2493
+ 109
2494
+ 110
2495
+ 111
2496
+ 112
2497
+ 113
2498
+ 114
2499
+ 115
2500
+ 116
2501
+ 117
2502
+ 118
2503
+ 119
2504
+ 120
2505
+ 121
2506
+ 122
2507
+ 123
2508
+ 124
2509
+ 125
2510
+ 126
2511
+ 127
2512
+ 128
2513
+ 129
2514
+ 130
2515
+ 131
2516
+ 132
2517
+ 133
2518
+ 134
2519
+ 135
2520
+ 136
2521
+ 137
2522
+ 138
2523
+ 139
2524
+ 140
2525
+ 141
2526
+ 142
2527
+ 143
2528
+ 144
2529
+ 145
2530
+ 146
2531
+ 147
2532
+ 148
2533
+ 149
2534
+ 150
2535
+ 151
2536
+ 152
2537
+ 153
2538
+ 154
2539
+ 155
2540
+ 156
2541
+ 157
2542
+ 158
2543
+ 159
2544
+ 160
2545
+ 161
2546
+ 162
2547
+ 163
2548
+ 164
2549
+ 165
2550
+ 166
2551
+ 167
2552
+ 168
2553
+ 169
2554
+ 170
2555
+ 171
2556
+ 172
2557
+ 173
2558
+ 174
2559
+ 175
2560
+ 176
2561
+ 177
2562
+ 178
2563
+ 179
2564
+ 180
2565
+ 181
2566
+ 182
2567
+ 183
2568
+ 184
2569
+ 185
2570
+ 186
2571
+ 187
2572
+ 188
2573
+ 189
2574
+ 190
2575
+ 191
2576
+ 192
2577
+ 193
2578
+ 194
2579
+ 195
2580
+ 196
2581
+ 197
2582
+ 198
2583
+ 199
2584
+ 200
2585
+ 201
2586
+ 202
2587
+ 203</pre>
2588
+ </td>
2589
+ <td>
2590
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
2591
+
2592
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
2593
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
2594
+
2595
+ <span class='comment'># The hostname on which the webserver runs on by default
2596
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
2597
+ <span class='comment'># The port on which the webserver runs on by default
2598
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
2599
+ <span class='comment'># The request path that the webserver accepts by default
2600
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
2601
+
2602
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
2603
+
2604
+ <span class='comment'># receives a list of options that are used to configure the webserver
2605
+ </span> <span class='comment'>#
2606
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
2607
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
2608
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
2609
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
2610
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
2611
+ </span> <span class='comment'>#
2612
+ </span> <span class='comment'># @return [void]
2613
+ </span> <span class='comment'>#
2614
+ </span> <span class='comment'># @api public
2615
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
2616
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
2617
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
2618
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2619
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
2620
+ <span class='kw'>end</span>
2621
+
2622
+ <span class='comment'># receives a list of options that are used to configure the webserver
2623
+ </span> <span class='comment'>#
2624
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
2625
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
2626
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
2627
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
2628
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
2629
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
2630
+ </span> <span class='comment'>#
2631
+ </span> <span class='comment'># @return [void]
2632
+ </span> <span class='comment'>#
2633
+ </span> <span class='comment'># @api public
2634
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
2635
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
2636
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
2637
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
2638
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
2639
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
2640
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
2641
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
2642
+ <span class='kw'>end</span>
2643
+
2644
+ <span class='comment'># checks if debug is enabled
2645
+ </span> <span class='comment'>#
2646
+ </span> <span class='comment'># @return [boolean]
2647
+ </span> <span class='comment'>#
2648
+ </span> <span class='comment'># @api public
2649
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2650
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2651
+ <span class='kw'>end</span>
2652
+
2653
+ <span class='comment'># checks if debug is enabled
2654
+ </span> <span class='comment'>#
2655
+ </span> <span class='comment'># @return [boolean]
2656
+ </span> <span class='comment'>#
2657
+ </span> <span class='comment'># @api public
2658
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2659
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
2660
+ <span class='kw'>end</span>
2661
+
2662
+ <span class='comment'># checks if debug is enabled
2663
+ </span> <span class='comment'>#
2664
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
2665
+ </span> <span class='comment'># @param [Object] message
2666
+ </span> <span class='comment'>#
2667
+ </span> <span class='comment'># @return [void]
2668
+ </span> <span class='comment'>#
2669
+ </span> <span class='comment'># @api public
2670
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
2671
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
2672
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
2673
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
2674
+ <span class='kw'>end</span>
2675
+ <span class='kw'>end</span>
2676
+
2677
+ <span class='comment'># callback that will execute when receiving new conections
2678
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
2679
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
2680
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
2681
+ </span> <span class='comment'>#
2682
+ </span> <span class='comment'># @see #route_websocket
2683
+ </span> <span class='comment'># @see #route_request
2684
+ </span> <span class='comment'>#
2685
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
2686
+ </span> <span class='comment'>#
2687
+ </span> <span class='comment'># @return [void]
2688
+ </span> <span class='comment'>#
2689
+ </span> <span class='comment'># @api public
2690
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
2691
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
2692
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
2693
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2694
+
2695
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
2696
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
2697
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
2698
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
2699
+ </span> <span class='comment'>#
2700
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
2701
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
2702
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
2703
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
2704
+ <span class='kw'>return</span>
2705
+ <span class='kw'>else</span>
2706
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
2707
+ <span class='kw'>end</span>
2708
+ <span class='kw'>end</span>
2709
+ <span class='kw'>end</span>
2710
+
2711
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
2712
+ </span> <span class='comment'>#
2713
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
2714
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
2715
+ </span> <span class='comment'>#
2716
+ </span> <span class='comment'># @return [void]
2717
+ </span> <span class='comment'>#
2718
+ </span> <span class='comment'># @api public
2719
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
2720
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2721
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
2722
+ <span class='kw'>end</span>
2723
+
2724
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
2725
+ </span> <span class='comment'># The new actor is linked to the webserver
2726
+ </span> <span class='comment'>#
2727
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
2728
+ </span> <span class='comment'>#
2729
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
2730
+ </span> <span class='comment'>#
2731
+ </span> <span class='comment'># @return [void]
2732
+ </span> <span class='comment'>#
2733
+ </span> <span class='comment'># @api public
2734
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
2735
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
2736
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2737
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
2738
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
2739
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
2740
+ <span class='kw'>else</span>
2741
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2742
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
2743
+ <span class='kw'>end</span>
2744
+ <span class='kw'>end</span>
2745
+
2746
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
2747
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
2748
+ </span> <span class='comment'>#
2749
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
2750
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
2751
+ </span> <span class='comment'>#
2752
+ </span> <span class='comment'># @return [void]
2753
+ </span> <span class='comment'>#
2754
+ </span> <span class='comment'># @api public
2755
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
2756
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
2757
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
2758
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
2759
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
2760
+ <span class='kw'>else</span>
2761
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
2762
+ <span class='kw'>end</span>
2763
+ <span class='kw'>end</span>
2764
+ <span class='kw'>end</span></pre>
2765
+ </td>
2766
+ </tr>
2767
+ </table>
2768
+ </div>
2769
+
2770
+
2771
+ <span id="spy=-instance_method"></span>
2772
+ <div class="method_details ">
2773
+ <h3 class="signature " id="spy-instance_method">
2774
+
2775
+ - (<tt>Boolean</tt>) <strong>spy</strong>
2776
+
2777
+
2778
+
2779
+
2780
+
2781
+ </h3><div class="docstring">
2782
+ <div class="discussion">
2783
+
2784
+ <p>Returns Enable this only if you want to enable debugging for the webserver</p>
2785
+
2786
+
2787
+ </div>
2788
+ </div>
2789
+ <div class="tags">
2790
+
2791
+ <p class="tag_title">Returns:</p>
2792
+ <ul class="return">
2793
+
2794
+ <li>
2795
+
2796
+
2797
+ <span class='type'>(<tt>Boolean</tt>)</span>
2798
+
2799
+
2800
+
2801
+ &mdash;
2802
+ <div class='inline'>
2803
+ <p>Enable this only if you want to enable debugging for the webserver</p>
2804
+ </div>
2805
+
2806
+ </li>
2807
+
2808
+ </ul>
2809
+
2810
+ </div><table class="source_code">
2811
+ <tr>
2812
+ <td>
2813
+ <pre class="lines">
2814
+
2815
+
2816
+ 31
2817
+ 32
2818
+ 33
2819
+ 34
2820
+ 35
2821
+ 36
2822
+ 37
2823
+ 38
2824
+ 39
2825
+ 40
2826
+ 41
2827
+ 42
2828
+ 43
2829
+ 44
2830
+ 45
2831
+ 46
2832
+ 47
2833
+ 48
2834
+ 49
2835
+ 50
2836
+ 51
2837
+ 52
2838
+ 53
2839
+ 54
2840
+ 55
2841
+ 56
2842
+ 57
2843
+ 58
2844
+ 59
2845
+ 60
2846
+ 61
2847
+ 62
2848
+ 63
2849
+ 64
2850
+ 65
2851
+ 66
2852
+ 67
2853
+ 68
2854
+ 69
2855
+ 70
2856
+ 71
2857
+ 72
2858
+ 73
2859
+ 74
2860
+ 75
2861
+ 76
2862
+ 77
2863
+ 78
2864
+ 79
2865
+ 80
2866
+ 81
2867
+ 82
2868
+ 83
2869
+ 84
2870
+ 85
2871
+ 86
2872
+ 87
2873
+ 88
2874
+ 89
2875
+ 90
2876
+ 91
2877
+ 92
2878
+ 93
2879
+ 94
2880
+ 95
2881
+ 96
2882
+ 97
2883
+ 98
2884
+ 99
2885
+ 100
2886
+ 101
2887
+ 102
2888
+ 103
2889
+ 104
2890
+ 105
2891
+ 106
2892
+ 107
2893
+ 108
2894
+ 109
2895
+ 110
2896
+ 111
2897
+ 112
2898
+ 113
2899
+ 114
2900
+ 115
2901
+ 116
2902
+ 117
2903
+ 118
2904
+ 119
2905
+ 120
2906
+ 121
2907
+ 122
2908
+ 123
2909
+ 124
2910
+ 125
2911
+ 126
2912
+ 127
2913
+ 128
2914
+ 129
2915
+ 130
2916
+ 131
2917
+ 132
2918
+ 133
2919
+ 134
2920
+ 135
2921
+ 136
2922
+ 137
2923
+ 138
2924
+ 139
2925
+ 140
2926
+ 141
2927
+ 142
2928
+ 143
2929
+ 144
2930
+ 145
2931
+ 146
2932
+ 147
2933
+ 148
2934
+ 149
2935
+ 150
2936
+ 151
2937
+ 152
2938
+ 153
2939
+ 154
2940
+ 155
2941
+ 156
2942
+ 157
2943
+ 158
2944
+ 159
2945
+ 160
2946
+ 161
2947
+ 162
2948
+ 163
2949
+ 164
2950
+ 165
2951
+ 166
2952
+ 167
2953
+ 168
2954
+ 169
2955
+ 170
2956
+ 171
2957
+ 172
2958
+ 173
2959
+ 174
2960
+ 175
2961
+ 176
2962
+ 177
2963
+ 178
2964
+ 179
2965
+ 180
2966
+ 181
2967
+ 182
2968
+ 183
2969
+ 184
2970
+ 185
2971
+ 186
2972
+ 187
2973
+ 188
2974
+ 189
2975
+ 190
2976
+ 191
2977
+ 192
2978
+ 193
2979
+ 194
2980
+ 195
2981
+ 196
2982
+ 197
2983
+ 198
2984
+ 199
2985
+ 200
2986
+ 201
2987
+ 202
2988
+ 203</pre>
2989
+ </td>
2990
+ <td>
2991
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
2992
+
2993
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
2994
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
2995
+
2996
+ <span class='comment'># The hostname on which the webserver runs on by default
2997
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
2998
+ <span class='comment'># The port on which the webserver runs on by default
2999
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
3000
+ <span class='comment'># The request path that the webserver accepts by default
3001
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
3002
+
3003
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
3004
+
3005
+ <span class='comment'># receives a list of options that are used to configure the webserver
3006
+ </span> <span class='comment'>#
3007
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
3008
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
3009
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
3010
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
3011
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
3012
+ </span> <span class='comment'>#
3013
+ </span> <span class='comment'># @return [void]
3014
+ </span> <span class='comment'>#
3015
+ </span> <span class='comment'># @api public
3016
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
3017
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
3018
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
3019
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3020
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
3021
+ <span class='kw'>end</span>
3022
+
3023
+ <span class='comment'># receives a list of options that are used to configure the webserver
3024
+ </span> <span class='comment'>#
3025
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
3026
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
3027
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
3028
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
3029
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
3030
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
3031
+ </span> <span class='comment'>#
3032
+ </span> <span class='comment'># @return [void]
3033
+ </span> <span class='comment'>#
3034
+ </span> <span class='comment'># @api public
3035
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
3036
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
3037
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
3038
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
3039
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
3040
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
3041
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
3042
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
3043
+ <span class='kw'>end</span>
3044
+
3045
+ <span class='comment'># checks if debug is enabled
3046
+ </span> <span class='comment'>#
3047
+ </span> <span class='comment'># @return [boolean]
3048
+ </span> <span class='comment'>#
3049
+ </span> <span class='comment'># @api public
3050
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3051
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3052
+ <span class='kw'>end</span>
3053
+
3054
+ <span class='comment'># checks if debug is enabled
3055
+ </span> <span class='comment'>#
3056
+ </span> <span class='comment'># @return [boolean]
3057
+ </span> <span class='comment'>#
3058
+ </span> <span class='comment'># @api public
3059
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3060
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
3061
+ <span class='kw'>end</span>
3062
+
3063
+ <span class='comment'># checks if debug is enabled
3064
+ </span> <span class='comment'>#
3065
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
3066
+ </span> <span class='comment'># @param [Object] message
3067
+ </span> <span class='comment'>#
3068
+ </span> <span class='comment'># @return [void]
3069
+ </span> <span class='comment'>#
3070
+ </span> <span class='comment'># @api public
3071
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
3072
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
3073
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
3074
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
3075
+ <span class='kw'>end</span>
3076
+ <span class='kw'>end</span>
3077
+
3078
+ <span class='comment'># callback that will execute when receiving new conections
3079
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
3080
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
3081
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
3082
+ </span> <span class='comment'>#
3083
+ </span> <span class='comment'># @see #route_websocket
3084
+ </span> <span class='comment'># @see #route_request
3085
+ </span> <span class='comment'>#
3086
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
3087
+ </span> <span class='comment'>#
3088
+ </span> <span class='comment'># @return [void]
3089
+ </span> <span class='comment'>#
3090
+ </span> <span class='comment'># @api public
3091
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
3092
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
3093
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
3094
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3095
+
3096
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
3097
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
3098
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
3099
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
3100
+ </span> <span class='comment'>#
3101
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
3102
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
3103
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
3104
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
3105
+ <span class='kw'>return</span>
3106
+ <span class='kw'>else</span>
3107
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
3108
+ <span class='kw'>end</span>
3109
+ <span class='kw'>end</span>
3110
+ <span class='kw'>end</span>
3111
+
3112
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
3113
+ </span> <span class='comment'>#
3114
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
3115
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
3116
+ </span> <span class='comment'>#
3117
+ </span> <span class='comment'># @return [void]
3118
+ </span> <span class='comment'>#
3119
+ </span> <span class='comment'># @api public
3120
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
3121
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3122
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
3123
+ <span class='kw'>end</span>
3124
+
3125
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
3126
+ </span> <span class='comment'># The new actor is linked to the webserver
3127
+ </span> <span class='comment'>#
3128
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
3129
+ </span> <span class='comment'>#
3130
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
3131
+ </span> <span class='comment'>#
3132
+ </span> <span class='comment'># @return [void]
3133
+ </span> <span class='comment'>#
3134
+ </span> <span class='comment'># @api public
3135
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
3136
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
3137
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3138
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
3139
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
3140
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
3141
+ <span class='kw'>else</span>
3142
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3143
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
3144
+ <span class='kw'>end</span>
3145
+ <span class='kw'>end</span>
3146
+
3147
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
3148
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
3149
+ </span> <span class='comment'>#
3150
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
3151
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
3152
+ </span> <span class='comment'>#
3153
+ </span> <span class='comment'># @return [void]
3154
+ </span> <span class='comment'>#
3155
+ </span> <span class='comment'># @api public
3156
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3157
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3158
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3159
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
3160
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3161
+ <span class='kw'>else</span>
3162
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3163
+ <span class='kw'>end</span>
3164
+ <span class='kw'>end</span>
3165
+ <span class='kw'>end</span></pre>
3166
+ </td>
3167
+ </tr>
3168
+ </table>
3169
+ </div>
3170
+
3171
+
3172
+ <span id="subscribers=-instance_method"></span>
3173
+ <div class="method_details ">
3174
+ <h3 class="signature " id="subscribers-instance_method">
3175
+
3176
+ - (<tt>Hash</tt>) <strong>subscribers</strong>
3177
+
3178
+
3179
+
3180
+
3181
+
3182
+ </h3><div class="docstring">
3183
+ <div class="discussion">
3184
+
3185
+ <p>Returns The hostname on which the webserver runs on</p>
3186
+
3187
+
3188
+ </div>
3189
+ </div>
3190
+ <div class="tags">
3191
+
3192
+ <p class="tag_title">Returns:</p>
3193
+ <ul class="return">
3194
+
3195
+ <li>
3196
+
3197
+
3198
+ <span class='type'>(<tt>Hash</tt>)</span>
3199
+
3200
+
3201
+
3202
+ &mdash;
3203
+ <div class='inline'>
3204
+ <p>The hostname on which the webserver runs on</p>
3205
+ </div>
3206
+
3207
+ </li>
3208
+
3209
+ </ul>
3210
+
3211
+ </div><table class="source_code">
3212
+ <tr>
3213
+ <td>
3214
+ <pre class="lines">
3215
+
3216
+
3217
+ 31
3218
+ 32
3219
+ 33
3220
+ 34
3221
+ 35
3222
+ 36
3223
+ 37
3224
+ 38
3225
+ 39
3226
+ 40
3227
+ 41
3228
+ 42
3229
+ 43
3230
+ 44
3231
+ 45
3232
+ 46
3233
+ 47
3234
+ 48
3235
+ 49
3236
+ 50
3237
+ 51
3238
+ 52
3239
+ 53
3240
+ 54
3241
+ 55
3242
+ 56
3243
+ 57
3244
+ 58
3245
+ 59
3246
+ 60
3247
+ 61
3248
+ 62
3249
+ 63
3250
+ 64
3251
+ 65
3252
+ 66
3253
+ 67
3254
+ 68
3255
+ 69
3256
+ 70
3257
+ 71
3258
+ 72
3259
+ 73
3260
+ 74
3261
+ 75
3262
+ 76
3263
+ 77
3264
+ 78
3265
+ 79
3266
+ 80
3267
+ 81
3268
+ 82
3269
+ 83
3270
+ 84
3271
+ 85
3272
+ 86
3273
+ 87
3274
+ 88
3275
+ 89
3276
+ 90
3277
+ 91
3278
+ 92
3279
+ 93
3280
+ 94
3281
+ 95
3282
+ 96
3283
+ 97
3284
+ 98
3285
+ 99
3286
+ 100
3287
+ 101
3288
+ 102
3289
+ 103
3290
+ 104
3291
+ 105
3292
+ 106
3293
+ 107
3294
+ 108
3295
+ 109
3296
+ 110
3297
+ 111
3298
+ 112
3299
+ 113
3300
+ 114
3301
+ 115
3302
+ 116
3303
+ 117
3304
+ 118
3305
+ 119
3306
+ 120
3307
+ 121
3308
+ 122
3309
+ 123
3310
+ 124
3311
+ 125
3312
+ 126
3313
+ 127
3314
+ 128
3315
+ 129
3316
+ 130
3317
+ 131
3318
+ 132
3319
+ 133
3320
+ 134
3321
+ 135
3322
+ 136
3323
+ 137
3324
+ 138
3325
+ 139
3326
+ 140
3327
+ 141
3328
+ 142
3329
+ 143
3330
+ 144
3331
+ 145
3332
+ 146
3333
+ 147
3334
+ 148
3335
+ 149
3336
+ 150
3337
+ 151
3338
+ 152
3339
+ 153
3340
+ 154
3341
+ 155
3342
+ 156
3343
+ 157
3344
+ 158
3345
+ 159
3346
+ 160
3347
+ 161
3348
+ 162
3349
+ 163
3350
+ 164
3351
+ 165
3352
+ 166
3353
+ 167
3354
+ 168
3355
+ 169
3356
+ 170
3357
+ 171
3358
+ 172
3359
+ 173
3360
+ 174
3361
+ 175
3362
+ 176
3363
+ 177
3364
+ 178
3365
+ 179
3366
+ 180
3367
+ 181
3368
+ 182
3369
+ 183
3370
+ 184
3371
+ 185
3372
+ 186
3373
+ 187
3374
+ 188
3375
+ 189
3376
+ 190
3377
+ 191
3378
+ 192
3379
+ 193
3380
+ 194
3381
+ 195
3382
+ 196
3383
+ 197
3384
+ 198
3385
+ 199
3386
+ 200
3387
+ 201
3388
+ 202
3389
+ 203</pre>
3390
+ </td>
3391
+ <td>
3392
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 31</span>
3393
+
3394
+ <span class='kw'>class</span> <span class='const'>WebServer</span> <span class='op'>&lt;</span> <span class='const'>Reel</span><span class='op'>::</span><span class='const'>Server</span><span class='op'>::</span><span class='const'>HTTP</span>
3395
+ <span class='id identifier rubyid_include'>include</span> <span class='const'>Celluloid</span><span class='op'>::</span><span class='const'>Logger</span>
3396
+
3397
+ <span class='comment'># The hostname on which the webserver runs on by default
3398
+ </span> <span class='const'>HOST</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.0.0.0</span><span class='tstring_end'>&#39;</span></span>
3399
+ <span class='comment'># The port on which the webserver runs on by default
3400
+ </span> <span class='const'>PORT</span> <span class='op'>=</span> <span class='int'>1234</span>
3401
+ <span class='comment'># The request path that the webserver accepts by default
3402
+ </span> <span class='const'>PATH</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>/ws</span><span class='tstring_end'>&#39;</span></span>
3403
+
3404
+ <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:options</span><span class='comma'>,</span> <span class='symbol'>:subscribers</span><span class='comma'>,</span> <span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='symbol'>:port</span><span class='comma'>,</span> <span class='symbol'>:path</span><span class='comma'>,</span> <span class='symbol'>:spy</span>
3405
+
3406
+ <span class='comment'># receives a list of options that are used to configure the webserver
3407
+ </span> <span class='comment'>#
3408
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
3409
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
3410
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
3411
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
3412
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
3413
+ </span> <span class='comment'>#
3414
+ </span> <span class='comment'># @return [void]
3415
+ </span> <span class='comment'>#
3416
+ </span> <span class='comment'># @api public
3417
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
3418
+ <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
3419
+ <span class='ivar'>@subscribers</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
3420
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>CelluloidPubsub::WebServer example starting on </span><span class='embexpr_beg'>#{</span><span class='ivar'>@hostname</span><span class='embexpr_end'>}</span><span class='tstring_content'>:</span><span class='embexpr_beg'>#{</span><span class='ivar'>@port</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3421
+ <span class='kw'>super</span><span class='lparen'>(</span><span class='ivar'>@hostname</span><span class='comma'>,</span> <span class='ivar'>@port</span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>spy:</span> <span class='ivar'>@spy</span><span class='comma'>,</span> <span class='label'>backlog:</span> <span class='ivar'>@backlog</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:on_connection</span><span class='rparen'>)</span><span class='rparen'>)</span>
3422
+ <span class='kw'>end</span>
3423
+
3424
+ <span class='comment'># receives a list of options that are used to configure the webserver
3425
+ </span> <span class='comment'>#
3426
+ </span> <span class='comment'># @param [Hash] options the options that can be used to connect to webser and send additional data
3427
+ </span> <span class='comment'># @option options [String]:hostname The hostname on which the webserver runs on
3428
+ </span> <span class='comment'># @option options [Integer] :port The port on which the webserver runs on
3429
+ </span> <span class='comment'># @option options [String] :path The request path that the webserver accepts
3430
+ </span> <span class='comment'># @option options [Boolean] :spy Enable this only if you want to enable debugging for the webserver
3431
+ </span> <span class='comment'># @option options [Integer]:backlog How many connections the server accepts
3432
+ </span> <span class='comment'>#
3433
+ </span> <span class='comment'># @return [void]
3434
+ </span> <span class='comment'>#
3435
+ </span> <span class='comment'># @api public
3436
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
3437
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
3438
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
3439
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
3440
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
3441
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
3442
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
3443
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
3444
+ <span class='kw'>end</span>
3445
+
3446
+ <span class='comment'># checks if debug is enabled
3447
+ </span> <span class='comment'>#
3448
+ </span> <span class='comment'># @return [boolean]
3449
+ </span> <span class='comment'>#
3450
+ </span> <span class='comment'># @api public
3451
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3452
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3453
+ <span class='kw'>end</span>
3454
+
3455
+ <span class='comment'># checks if debug is enabled
3456
+ </span> <span class='comment'>#
3457
+ </span> <span class='comment'># @return [boolean]
3458
+ </span> <span class='comment'>#
3459
+ </span> <span class='comment'># @api public
3460
+ </span> <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3461
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
3462
+ <span class='kw'>end</span>
3463
+
3464
+ <span class='comment'># checks if debug is enabled
3465
+ </span> <span class='comment'>#
3466
+ </span> <span class='comment'># @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
3467
+ </span> <span class='comment'># @param [Object] message
3468
+ </span> <span class='comment'>#
3469
+ </span> <span class='comment'># @return [void]
3470
+ </span> <span class='comment'>#
3471
+ </span> <span class='comment'># @api public
3472
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
3473
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
3474
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
3475
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
3476
+ <span class='kw'>end</span>
3477
+ <span class='kw'>end</span>
3478
+
3479
+ <span class='comment'># callback that will execute when receiving new conections
3480
+ </span> <span class='comment'># If the connections is a websocket will call method {#route_websocket}
3481
+ </span> <span class='comment'># and if the connection is HTTP will call method {#route_request}
3482
+ </span> <span class='comment'># For websocket connections , the connection is detached from the server and dispatched to another actor
3483
+ </span> <span class='comment'>#
3484
+ </span> <span class='comment'># @see #route_websocket
3485
+ </span> <span class='comment'># @see #route_request
3486
+ </span> <span class='comment'>#
3487
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The connection that was made to the webserver
3488
+ </span> <span class='comment'>#
3489
+ </span> <span class='comment'># @return [void]
3490
+ </span> <span class='comment'>#
3491
+ </span> <span class='comment'># @api public
3492
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
3493
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
3494
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
3495
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3496
+
3497
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
3498
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
3499
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
3500
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
3501
+ </span> <span class='comment'>#
3502
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
3503
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
3504
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
3505
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
3506
+ <span class='kw'>return</span>
3507
+ <span class='kw'>else</span>
3508
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
3509
+ <span class='kw'>end</span>
3510
+ <span class='kw'>end</span>
3511
+ <span class='kw'>end</span>
3512
+
3513
+ <span class='comment'># HTTP connections are not accepted so this method will show 404 message &quot;Not Found&quot;
3514
+ </span> <span class='comment'>#
3515
+ </span> <span class='comment'># @param [Reel::WebSocket] connection The HTTP connection that was received
3516
+ </span> <span class='comment'># @param [Reel::Request] request The request that was made to the webserver and contains the type , the url, and the parameters
3517
+ </span> <span class='comment'>#
3518
+ </span> <span class='comment'># @return [void]
3519
+ </span> <span class='comment'>#
3520
+ </span> <span class='comment'># @api public
3521
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
3522
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3523
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
3524
+ <span class='kw'>end</span>
3525
+
3526
+ <span class='comment'># If the socket url matches with the one accepted by the server, it will dispatch the socket connection to a new reactor {CelluloidPubsub::Reactor#work}
3527
+ </span> <span class='comment'># The new actor is linked to the webserver
3528
+ </span> <span class='comment'>#
3529
+ </span> <span class='comment'># @see CelluloidPubsub::Reactor#work
3530
+ </span> <span class='comment'>#
3531
+ </span> <span class='comment'># @param [Reel::WebSocket] socket The web socket connection that was received
3532
+ </span> <span class='comment'>#
3533
+ </span> <span class='comment'># @return [void]
3534
+ </span> <span class='comment'>#
3535
+ </span> <span class='comment'># @api public
3536
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
3537
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
3538
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3539
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
3540
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
3541
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
3542
+ <span class='kw'>else</span>
3543
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3544
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
3545
+ <span class='kw'>end</span>
3546
+ <span class='kw'>end</span>
3547
+
3548
+ <span class='comment'># If the message can be parsed into a Hash it will respond to the reactor&#39;s websocket connection with the same message in JSON format
3549
+ </span> <span class='comment'># otherwise will try send the message how it is and escaped into JSON format
3550
+ </span> <span class='comment'>#
3551
+ </span> <span class='comment'># @param [CelluloidPubsub::Reactor] reactor The reactor that received an unhandled message
3552
+ </span> <span class='comment'># @param [Object] data The message that the reactor could not handle
3553
+ </span> <span class='comment'>#
3554
+ </span> <span class='comment'># @return [void]
3555
+ </span> <span class='comment'>#
3556
+ </span> <span class='comment'># @api public
3557
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3558
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3559
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3560
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
3561
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3562
+ <span class='kw'>else</span>
3563
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3564
+ <span class='kw'>end</span>
3565
+ <span class='kw'>end</span>
3566
+ <span class='kw'>end</span></pre>
3567
+ </td>
3568
+ </tr>
3569
+ </table>
3570
+ </div>
3571
+
3572
+ </div>
3573
+
3574
+
3575
+ <div id="class_method_details" class="method_details_list">
3576
+ <h2>Class Method Details</h2>
3577
+
3578
+
3579
+ <div class="method_details first">
3580
+ <h3 class="signature first" id="debug_enabled?-class_method">
3581
+
3582
+ + (<tt>boolean</tt>) <strong>debug_enabled?</strong>
3583
+
3584
+
3585
+
3586
+
3587
+
3588
+ </h3><div class="docstring">
3589
+ <div class="discussion">
3590
+
3591
+ <p>checks if debug is enabled</p>
3592
+
3593
+
3594
+ </div>
3595
+ </div>
3596
+ <div class="tags">
3597
+
3598
+ <p class="tag_title">Returns:</p>
3599
+ <ul class="return">
3600
+
3601
+ <li>
3602
+
3603
+
3604
+ <span class='type'>(<tt>boolean</tt>)</span>
3605
+
3606
+
3607
+
3608
+ </li>
3609
+
3610
+ </ul>
3611
+
3612
+ </div><table class="source_code">
3613
+ <tr>
3614
+ <td>
3615
+ <pre class="lines">
3616
+
3617
+
3618
+ 97
3619
+ 98
3620
+ 99</pre>
3621
+ </td>
3622
+ <td>
3623
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 97</span>
3624
+
3625
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3626
+ <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='lparen'>(</span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>true</span><span class='tstring_end'>&#39;</span></span> <span class='op'>||</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>DEBUG_CELLULOID</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='kw'>true</span><span class='rparen'>)</span>
3627
+ <span class='kw'>end</span></pre>
3628
+ </td>
3629
+ </tr>
3630
+ </table>
3631
+ </div>
3632
+
3633
+ </div>
3634
+
3635
+ <div id="instance_method_details" class="method_details_list">
3636
+ <h2>Instance Method Details</h2>
3637
+
3638
+
3639
+ <div class="method_details first">
3640
+ <h3 class="signature first" id="debug_enabled?-instance_method">
3641
+
3642
+ - (<tt>boolean</tt>) <strong>debug_enabled?</strong>
3643
+
3644
+
3645
+
3646
+
3647
+
3648
+ </h3><div class="docstring">
3649
+ <div class="discussion">
3650
+
3651
+ <p>checks if debug is enabled</p>
3652
+
3653
+
3654
+ </div>
3655
+ </div>
3656
+ <div class="tags">
3657
+
3658
+ <p class="tag_title">Returns:</p>
3659
+ <ul class="return">
3660
+
3661
+ <li>
3662
+
3663
+
3664
+ <span class='type'>(<tt>boolean</tt>)</span>
3665
+
3666
+
3667
+
3668
+ </li>
3669
+
3670
+ </ul>
3671
+
3672
+ </div><table class="source_code">
3673
+ <tr>
3674
+ <td>
3675
+ <pre class="lines">
3676
+
3677
+
3678
+ 88
3679
+ 89
3680
+ 90</pre>
3681
+ </td>
3682
+ <td>
3683
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 88</span>
3684
+
3685
+ <span class='kw'>def</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3686
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3687
+ <span class='kw'>end</span></pre>
3688
+ </td>
3689
+ </tr>
3690
+ </table>
3691
+ </div>
3692
+
3693
+ <div class="method_details ">
3694
+ <h3 class="signature " id="handle_dispatched_message-instance_method">
3695
+
3696
+ - (<tt>void</tt>) <strong>handle_dispatched_message</strong>(reactor, data)
3697
+
3698
+
3699
+
3700
+
3701
+
3702
+ </h3><div class="docstring">
3703
+ <div class="discussion">
3704
+ <p class="note returns_void">This method returns an undefined value.</p>
3705
+ <p>If the message can be parsed into a Hash it will respond to the
3706
+ reactor&#39;s websocket connection with the same message in JSON format
3707
+ otherwise will try send the message how it is and escaped into JSON format</p>
3708
+
3709
+
3710
+ </div>
3711
+ </div>
3712
+ <div class="tags">
3713
+ <p class="tag_title">Parameters:</p>
3714
+ <ul class="param">
3715
+
3716
+ <li>
3717
+
3718
+ <span class='name'>reactor</span>
3719
+
3720
+
3721
+ <span class='type'>(<tt><span class='object_link'><a href="Reactor.html" title="CelluloidPubsub::Reactor (class)">CelluloidPubsub::Reactor</a></span></tt>)</span>
3722
+
3723
+
3724
+
3725
+ &mdash;
3726
+ <div class='inline'>
3727
+ <p>The reactor that received an unhandled message</p>
3728
+ </div>
3729
+
3730
+ </li>
3731
+
3732
+ <li>
3733
+
3734
+ <span class='name'>data</span>
3735
+
3736
+
3737
+ <span class='type'>(<tt>Object</tt>)</span>
3738
+
3739
+
3740
+
3741
+ &mdash;
3742
+ <div class='inline'>
3743
+ <p>The message that the reactor could not handle</p>
3744
+ </div>
3745
+
3746
+ </li>
3747
+
3748
+ </ul>
3749
+
3750
+
3751
+ </div><table class="source_code">
3752
+ <tr>
3753
+ <td>
3754
+ <pre class="lines">
3755
+
3756
+
3757
+ 194
3758
+ 195
3759
+ 196
3760
+ 197
3761
+ 198
3762
+ 199
3763
+ 200
3764
+ 201
3765
+ 202</pre>
3766
+ </td>
3767
+ <td>
3768
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 194</span>
3769
+
3770
+ <span class='kw'>def</span> <span class='id identifier rubyid_handle_dispatched_message'>handle_dispatched_message</span><span class='lparen'>(</span><span class='id identifier rubyid_reactor'>reactor</span><span class='comma'>,</span> <span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3771
+ <span class='id identifier rubyid_debug'>debug</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Webserver trying to dispatch message </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3772
+ <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_parse_json_data'>parse_json_data</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='rparen'>)</span>
3773
+ <span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
3774
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3775
+ <span class='kw'>else</span>
3776
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span>
3777
+ <span class='kw'>end</span>
3778
+ <span class='kw'>end</span></pre>
3779
+ </td>
3780
+ </tr>
3781
+ </table>
3782
+ </div>
3783
+
3784
+ <div class="method_details ">
3785
+ <h3 class="signature " id="on_connection-instance_method">
3786
+
3787
+ - (<tt>void</tt>) <strong>on_connection</strong>(connection)
3788
+
3789
+
3790
+
3791
+
3792
+
3793
+ </h3><div class="docstring">
3794
+ <div class="discussion">
3795
+ <p class="note returns_void">This method returns an undefined value.</p>
3796
+ <p>callback that will execute when receiving new conections If the connections
3797
+ is a websocket will call method <span class='object_link'><a href="#route_websocket-instance_method" title="CelluloidPubsub::WebServer#route_websocket (method)">#route_websocket</a></span> and if the connection is
3798
+ HTTP will call method <span class='object_link'><a href="#route_request-instance_method" title="CelluloidPubsub::WebServer#route_request (method)">#route_request</a></span> For websocket connections , the
3799
+ connection is detached from the server and dispatched to another actor</p>
3800
+
3801
+
3802
+ </div>
3803
+ </div>
3804
+ <div class="tags">
3805
+ <p class="tag_title">Parameters:</p>
3806
+ <ul class="param">
3807
+
3808
+ <li>
3809
+
3810
+ <span class='name'>connection</span>
3811
+
3812
+
3813
+ <span class='type'>(<tt>Reel::WebSocket</tt>)</span>
3814
+
3815
+
3816
+
3817
+ &mdash;
3818
+ <div class='inline'>
3819
+ <p>The connection that was made to the webserver</p>
3820
+ </div>
3821
+
3822
+ </li>
3823
+
3824
+ </ul>
3825
+
3826
+
3827
+ <p class="tag_title">See Also:</p>
3828
+ <ul class="see">
3829
+
3830
+ <li><span class='object_link'><a href="#route_websocket-instance_method" title="CelluloidPubsub::WebServer#route_websocket (method)">#route_websocket</a></span></li>
3831
+
3832
+ <li><span class='object_link'><a href="#route_request-instance_method" title="CelluloidPubsub::WebServer#route_request (method)">#route_request</a></span></li>
3833
+
3834
+ </ul>
3835
+
3836
+ </div><table class="source_code">
3837
+ <tr>
3838
+ <td>
3839
+ <pre class="lines">
3840
+
3841
+
3842
+ 129
3843
+ 130
3844
+ 131
3845
+ 132
3846
+ 133
3847
+ 134
3848
+ 135
3849
+ 136
3850
+ 137
3851
+ 138
3852
+ 139
3853
+ 140
3854
+ 141
3855
+ 142
3856
+ 143
3857
+ 144
3858
+ 145
3859
+ 146
3860
+ 147
3861
+ 148</pre>
3862
+ </td>
3863
+ <td>
3864
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 129</span>
3865
+
3866
+ <span class='kw'>def</span> <span class='id identifier rubyid_on_connection'>on_connection</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='rparen'>)</span>
3867
+ <span class='kw'>while</span> <span class='id identifier rubyid_request'>request</span> <span class='op'>=</span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span>
3868
+ <span class='kw'>if</span> <span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket?'>websocket?</span>
3869
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Received a WebSocket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
3870
+
3871
+ <span class='comment'># We&#39;re going to hand off this connection to another actor (Writer/Reader)
3872
+ </span> <span class='comment'># However, initially Reel::Connections are &quot;attached&quot; to the
3873
+ </span> <span class='comment'># Reel::Server::HTTP actor, meaning that the server manages the connection
3874
+ </span> <span class='comment'># lifecycle (e.g. error handling) for us.
3875
+ </span> <span class='comment'>#
3876
+ </span> <span class='comment'># If we want to hand this connection off to another actor, we first
3877
+ </span> <span class='comment'># need to detach it from the Reel::Server (in this case, Reel::Server::HTTP)
3878
+ </span> <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_detach'>detach</span>
3879
+ <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span><span class='rparen'>)</span>
3880
+ <span class='kw'>return</span>
3881
+ <span class='kw'>else</span>
3882
+ <span class='id identifier rubyid_route_request'>route_request</span> <span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span>
3883
+ <span class='kw'>end</span>
3884
+ <span class='kw'>end</span>
3885
+ <span class='kw'>end</span></pre>
3886
+ </td>
3887
+ </tr>
3888
+ </table>
3889
+ </div>
3890
+
3891
+ <div class="method_details ">
3892
+ <h3 class="signature " id="parse_options-instance_method">
3893
+
3894
+ - (<tt>void</tt>) <strong>parse_options</strong>(options)
3895
+
3896
+
3897
+
3898
+
3899
+
3900
+ </h3><div class="docstring">
3901
+ <div class="discussion">
3902
+ <p class="note returns_void">This method returns an undefined value.</p>
3903
+ <p>receives a list of options that are used to configure the webserver</p>
3904
+
3905
+
3906
+ </div>
3907
+ </div>
3908
+ <div class="tags">
3909
+ <p class="tag_title">Parameters:</p>
3910
+ <ul class="param">
3911
+
3912
+ <li>
3913
+
3914
+ <span class='name'>options</span>
3915
+
3916
+
3917
+ <span class='type'>(<tt>Hash</tt>)</span>
3918
+
3919
+
3920
+
3921
+ &mdash;
3922
+ <div class='inline'>
3923
+ <p>the options that can be used to connect to webser and send additional data</p>
3924
+ </div>
3925
+
3926
+ </li>
3927
+
3928
+ </ul>
3929
+
3930
+
3931
+
3932
+
3933
+ <p class="tag_title">Options Hash (<tt>options</tt>):</p>
3934
+ <ul class="option">
3935
+
3936
+ <li>
3937
+ <span class="name">:hostname</span>
3938
+ <span class="type">(<tt>String</tt>)</span>
3939
+ <span class="default">
3940
+
3941
+ </span>
3942
+
3943
+ &mdash; <div class='inline'>
3944
+ <p>The hostname on which the webserver runs on</p>
3945
+ </div>
3946
+
3947
+ </li>
3948
+
3949
+ <li>
3950
+ <span class="name">:port</span>
3951
+ <span class="type">(<tt>Integer</tt>)</span>
3952
+ <span class="default">
3953
+
3954
+ </span>
3955
+
3956
+ &mdash; <div class='inline'>
3957
+ <p>The port on which the webserver runs on</p>
3958
+ </div>
3959
+
3960
+ </li>
3961
+
3962
+ <li>
3963
+ <span class="name">:path</span>
3964
+ <span class="type">(<tt>String</tt>)</span>
3965
+ <span class="default">
3966
+
3967
+ </span>
3968
+
3969
+ &mdash; <div class='inline'>
3970
+ <p>The request path that the webserver accepts</p>
3971
+ </div>
3972
+
3973
+ </li>
3974
+
3975
+ <li>
3976
+ <span class="name">:spy</span>
3977
+ <span class="type">(<tt>Boolean</tt>)</span>
3978
+ <span class="default">
3979
+
3980
+ </span>
3981
+
3982
+ &mdash; <div class='inline'>
3983
+ <p>Enable this only if you want to enable debugging for the webserver</p>
3984
+ </div>
3985
+
3986
+ </li>
3987
+
3988
+ <li>
3989
+ <span class="name">:backlog</span>
3990
+ <span class="type">(<tt>Integer</tt>)</span>
3991
+ <span class="default">
3992
+
3993
+ </span>
3994
+
3995
+ &mdash; <div class='inline'>
3996
+ <p>How many connections the server accepts</p>
3997
+ </div>
3998
+
3999
+ </li>
4000
+
4001
+ </ul>
4002
+
4003
+
4004
+
4005
+ </div><table class="source_code">
4006
+ <tr>
4007
+ <td>
4008
+ <pre class="lines">
4009
+
4010
+
4011
+ 73
4012
+ 74
4013
+ 75
4014
+ 76
4015
+ 77
4016
+ 78
4017
+ 79
4018
+ 80
4019
+ 81</pre>
4020
+ </td>
4021
+ <td>
4022
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 73</span>
4023
+
4024
+ <span class='kw'>def</span> <span class='id identifier rubyid_parse_options'>parse_options</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
4025
+ <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Options is not a hash </span><span class='tstring_end'>&#39;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
4026
+ <span class='ivar'>@options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_stringify_keys'>stringify_keys</span>
4027
+ <span class='ivar'>@backlog</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:backlog</span><span class='comma'>,</span> <span class='int'>1024</span><span class='rparen'>)</span>
4028
+ <span class='ivar'>@hostname</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:hostname</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>HOST</span><span class='rparen'>)</span>
4029
+ <span class='ivar'>@port</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:port</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PORT</span><span class='rparen'>)</span>
4030
+ <span class='ivar'>@path</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:path</span><span class='comma'>,</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>WebServer</span><span class='op'>::</span><span class='const'>PATH</span><span class='rparen'>)</span>
4031
+ <span class='ivar'>@spy</span> <span class='op'>=</span> <span class='ivar'>@options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:spy</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rparen'>)</span>
4032
+ <span class='kw'>end</span></pre>
4033
+ </td>
4034
+ </tr>
4035
+ </table>
4036
+ </div>
4037
+
4038
+ <div class="method_details ">
4039
+ <h3 class="signature " id="publish_event-instance_method">
4040
+
4041
+ - (<tt>void</tt>) <strong>publish_event</strong>(current_topic, message)
4042
+
4043
+
4044
+
4045
+
4046
+
4047
+ </h3><div class="docstring">
4048
+ <div class="discussion">
4049
+ <p class="note returns_void">This method returns an undefined value.</p>
4050
+ <p>checks if debug is enabled</p>
4051
+
4052
+
4053
+ </div>
4054
+ </div>
4055
+ <div class="tags">
4056
+ <p class="tag_title">Parameters:</p>
4057
+ <ul class="param">
4058
+
4059
+ <li>
4060
+
4061
+ <span class='name'>current_topic</span>
4062
+
4063
+
4064
+ <span class='type'>(<tt>String</tt>)</span>
4065
+
4066
+
4067
+
4068
+ &mdash;
4069
+ <div class='inline'>
4070
+ <p>The Channel to which the reactor instance Rector will
4071
+ publish the message to</p>
4072
+ </div>
4073
+
4074
+ </li>
4075
+
4076
+ <li>
4077
+
4078
+ <span class='name'>message</span>
4079
+
4080
+
4081
+ <span class='type'>(<tt>Object</tt>)</span>
4082
+
4083
+
4084
+
4085
+ </li>
4086
+
4087
+ </ul>
4088
+
4089
+
4090
+ </div><table class="source_code">
4091
+ <tr>
4092
+ <td>
4093
+ <pre class="lines">
4094
+
4095
+
4096
+ 109
4097
+ 110
4098
+ 111
4099
+ 112
4100
+ 113
4101
+ 114</pre>
4102
+ </td>
4103
+ <td>
4104
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 109</span>
4105
+
4106
+ <span class='kw'>def</span> <span class='id identifier rubyid_publish_event'>publish_event</span><span class='lparen'>(</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
4107
+ <span class='kw'>return</span> <span class='kw'>if</span> <span class='id identifier rubyid_current_topic'>current_topic</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>||</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
4108
+ <span class='ivar'>@subscribers</span><span class='lbracket'>[</span><span class='id identifier rubyid_current_topic'>current_topic</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_hash'>hash</span><span class='op'>|</span>
4109
+ <span class='id identifier rubyid_hash'>hash</span><span class='lbracket'>[</span><span class='symbol'>:reactor</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_websocket'>websocket</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_message'>message</span>
4110
+ <span class='kw'>end</span>
4111
+ <span class='kw'>end</span></pre>
4112
+ </td>
4113
+ </tr>
4114
+ </table>
4115
+ </div>
4116
+
4117
+ <div class="method_details ">
4118
+ <h3 class="signature " id="route_request-instance_method">
4119
+
4120
+ - (<tt>void</tt>) <strong>route_request</strong>(connection, request)
4121
+
4122
+
4123
+
4124
+
4125
+
4126
+ </h3><div class="docstring">
4127
+ <div class="discussion">
4128
+ <p class="note returns_void">This method returns an undefined value.</p>
4129
+ <p>HTTP connections are not accepted so this method will show 404 message “Not
4130
+ Found”</p>
4131
+
4132
+
4133
+ </div>
4134
+ </div>
4135
+ <div class="tags">
4136
+ <p class="tag_title">Parameters:</p>
4137
+ <ul class="param">
4138
+
4139
+ <li>
4140
+
4141
+ <span class='name'>connection</span>
4142
+
4143
+
4144
+ <span class='type'>(<tt>Reel::WebSocket</tt>)</span>
4145
+
4146
+
4147
+
4148
+ &mdash;
4149
+ <div class='inline'>
4150
+ <p>The HTTP connection that was received</p>
4151
+ </div>
4152
+
4153
+ </li>
4154
+
4155
+ <li>
4156
+
4157
+ <span class='name'>request</span>
4158
+
4159
+
4160
+ <span class='type'>(<tt>Reel::Request</tt>)</span>
4161
+
4162
+
4163
+
4164
+ &mdash;
4165
+ <div class='inline'>
4166
+ <p>The request that was made to the webserver and contains the type , the url,
4167
+ and the parameters</p>
4168
+ </div>
4169
+
4170
+ </li>
4171
+
4172
+ </ul>
4173
+
4174
+
4175
+ </div><table class="source_code">
4176
+ <tr>
4177
+ <td>
4178
+ <pre class="lines">
4179
+
4180
+
4181
+ 158
4182
+ 159
4183
+ 160
4184
+ 161</pre>
4185
+ </td>
4186
+ <td>
4187
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 158</span>
4188
+
4189
+ <span class='kw'>def</span> <span class='id identifier rubyid_route_request'>route_request</span><span class='lparen'>(</span><span class='id identifier rubyid_connection'>connection</span><span class='comma'>,</span> <span class='id identifier rubyid_request'>request</span><span class='rparen'>)</span>
4190
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>404 Not Found: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_request'>request</span><span class='period'>.</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
4191
+ <span class='id identifier rubyid_connection'>connection</span><span class='period'>.</span><span class='id identifier rubyid_respond'>respond</span> <span class='symbol'>:not_found</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Not found</span><span class='tstring_end'>&#39;</span></span>
4192
+ <span class='kw'>end</span></pre>
4193
+ </td>
4194
+ </tr>
4195
+ </table>
4196
+ </div>
4197
+
4198
+ <div class="method_details ">
4199
+ <h3 class="signature " id="route_websocket-instance_method">
4200
+
4201
+ - (<tt>void</tt>) <strong>route_websocket</strong>(socket)
4202
+
4203
+
4204
+
4205
+
4206
+
4207
+ </h3><div class="docstring">
4208
+ <div class="discussion">
4209
+ <p class="note returns_void">This method returns an undefined value.</p>
4210
+ <p>If the socket url matches with the one accepted by the server, it will
4211
+ dispatch the socket connection to a new reactor
4212
+ <span class='object_link'><a href="Reactor.html#work-instance_method" title="CelluloidPubsub::Reactor#work (method)">Reactor#work</a></span> The new actor is linked to the webserver</p>
4213
+
4214
+
4215
+ </div>
4216
+ </div>
4217
+ <div class="tags">
4218
+
4219
+
4220
+ <p class="tag_title">See Also:</p>
4221
+ <ul class="see">
4222
+
4223
+ <li><span class='object_link'><a href="Reactor.html#work-instance_method" title="CelluloidPubsub::Reactor#work (method)">@param [Reel::WebSocket] socket The web socket connection that was received</a></span></li>
4224
+
4225
+ </ul>
4226
+
4227
+ </div><table class="source_code">
4228
+ <tr>
4229
+ <td>
4230
+ <pre class="lines">
4231
+
4232
+
4233
+ 173
4234
+ 174
4235
+ 175
4236
+ 176
4237
+ 177
4238
+ 178
4239
+ 179
4240
+ 180
4241
+ 181
4242
+ 182
4243
+ 183</pre>
4244
+ </td>
4245
+ <td>
4246
+ <pre class="code"><span class="info file"># File 'lib/celluloid_pubsub/web_server.rb', line 173</span>
4247
+
4248
+ <span class='kw'>def</span> <span class='id identifier rubyid_route_websocket'>route_websocket</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='rparen'>)</span>
4249
+ <span class='kw'>if</span> <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span> <span class='op'>==</span> <span class='ivar'>@path</span>
4250
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>Reactor handles new socket connection</span><span class='tstring_end'>&#39;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
4251
+ <span class='id identifier rubyid_reactor'>reactor</span> <span class='op'>=</span> <span class='const'>CelluloidPubsub</span><span class='op'>::</span><span class='const'>Reactor</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
4252
+ <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='period'>.</span><span class='id identifier rubyid_link'>link</span> <span class='id identifier rubyid_reactor'>reactor</span>
4253
+ <span class='id identifier rubyid_reactor'>reactor</span><span class='period'>.</span><span class='id identifier rubyid_async'>async</span><span class='period'>.</span><span class='id identifier rubyid_work'>work</span><span class='lparen'>(</span><span class='id identifier rubyid_socket'>socket</span><span class='comma'>,</span> <span class='const'>Actor</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='rparen'>)</span>
4254
+ <span class='kw'>else</span>
4255
+ <span class='id identifier rubyid_info'>info</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Received invalid WebSocket request for: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_url'>url</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>if</span> <span class='id identifier rubyid_debug_enabled?'>debug_enabled?</span>
4256
+ <span class='id identifier rubyid_socket'>socket</span><span class='period'>.</span><span class='id identifier rubyid_close'>close</span>
4257
+ <span class='kw'>end</span>
4258
+ <span class='kw'>end</span></pre>
4259
+ </td>
4260
+ </tr>
4261
+ </table>
4262
+ </div>
4263
+
4264
+ </div>
4265
+
4266
+ </div>
4267
+
4268
+ <div id="footer">
4269
+ Generated on Fri Mar 13 12:25:43 2015 by
4270
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
4271
+ 0.8.7.6 (ruby-2.0.0).
4272
+ </div>
4273
+
4274
+ </body>
4275
+ </html>