line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::WebSocketProxy; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
338748
|
use strict; |
|
17
|
|
|
|
|
37
|
|
|
17
|
|
|
|
|
412
|
|
4
|
17
|
|
|
17
|
|
86
|
use warnings; |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
551
|
|
5
|
|
|
|
|
|
|
|
6
|
17
|
|
|
17
|
|
91
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
17
|
|
|
|
|
28
|
|
|
17
|
|
|
|
|
99
|
|
7
|
17
|
|
|
17
|
|
7699
|
use Mojo::WebSocketProxy::Backend; |
|
17
|
|
|
|
|
38
|
|
|
17
|
|
|
|
|
513
|
|
8
|
17
|
|
|
17
|
|
5619
|
use Mojo::WebSocketProxy::Config; |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
106
|
|
9
|
17
|
|
|
17
|
|
6161
|
use Mojo::WebSocketProxy::Dispatcher; |
|
17
|
|
|
|
|
42
|
|
|
17
|
|
|
|
|
116
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Other backend types may be available; we default to 'jsonrpc' in the code below |
12
|
17
|
|
|
17
|
|
7198
|
use Mojo::WebSocketProxy::Backend::JSONRPC; |
|
17
|
|
|
|
|
35
|
|
|
17
|
|
|
|
|
8292
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.11'; ## VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
22
|
|
|
22
|
1
|
48059
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
22
|
50
|
|
|
|
79
|
die 'No base path found!' unless $config->{base_path}; |
20
|
|
|
|
|
|
|
|
21
|
22
|
|
|
|
|
34
|
my $url_setter; |
22
|
22
|
50
|
33
|
|
|
141
|
$url_setter = delete $config->{url} if $config->{url} and ref($config->{url}) eq 'CODE'; |
23
|
|
|
|
|
|
|
$app->helper( |
24
|
|
|
|
|
|
|
call_rpc => sub { |
25
|
2
|
|
|
2
|
|
66
|
my ($c, $req_storage) = @_; |
26
|
2
|
50
|
33
|
|
|
8
|
$url_setter->($c, $req_storage) if $url_setter && !$req_storage->{url}; |
27
|
2
|
|
|
|
|
7
|
return $c->forward($req_storage); |
28
|
22
|
|
|
|
|
190
|
}); |
29
|
|
|
|
|
|
|
$app->helper( |
30
|
|
|
|
|
|
|
wsp_error => sub { |
31
|
5
|
|
|
5
|
|
166
|
shift; # $c |
32
|
5
|
|
|
|
|
14
|
my ($msg_type, $code, $message, $details) = @_; |
33
|
|
|
|
|
|
|
|
34
|
5
|
|
|
|
|
19
|
my $error = { |
35
|
|
|
|
|
|
|
code => $code, |
36
|
|
|
|
|
|
|
message => $message |
37
|
|
|
|
|
|
|
}; |
38
|
5
|
50
|
33
|
|
|
21
|
$error->{details} = $details if ref($details) eq 'HASH' && keys %$details; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return { |
41
|
5
|
|
|
|
|
23
|
msg_type => $msg_type, |
42
|
|
|
|
|
|
|
error => $error, |
43
|
|
|
|
|
|
|
}; |
44
|
22
|
|
|
|
|
489
|
}); |
45
|
|
|
|
|
|
|
|
46
|
22
|
|
|
|
|
310
|
my $r = $app->routes; |
47
|
22
|
|
|
|
|
236
|
for ($r->under($config->{base_path})) { |
48
|
22
|
|
|
|
|
5657
|
$_->to('Dispatcher#ok', namespace => 'Mojo::WebSocketProxy'); |
49
|
22
|
|
|
|
|
786
|
$_->websocket('/')->to('Dispatcher#open_connection', namespace => 'Mojo::WebSocketProxy'); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
22
|
|
|
|
|
3443
|
my $actions = delete $config->{actions}; |
53
|
22
|
|
|
|
|
153
|
my $dispatcher_config = Mojo::WebSocketProxy::Config->new; |
54
|
22
|
|
|
|
|
200
|
$dispatcher_config->init($config); |
55
|
|
|
|
|
|
|
|
56
|
22
|
50
|
|
|
|
78
|
if (ref $actions eq 'ARRAY') { |
57
|
22
|
|
|
|
|
68
|
for (my $i = 0; $i < @$actions; $i++) { |
58
|
30
|
|
|
|
|
89
|
$dispatcher_config->add_action($actions->[$i], $i); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
0
|
die 'No actions found!'; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# For backwards compatibility, we always want to add a plain JSON::RPC backend |
65
|
|
|
|
|
|
|
$dispatcher_config->add_backend( |
66
|
|
|
|
|
|
|
default => Mojo::WebSocketProxy::Backend->backend_instance( |
67
|
|
|
|
|
|
|
jsonrpc => url => delete $config->{url}, |
68
|
22
|
50
|
|
|
|
120
|
)) unless exists $config->{backends}{default}; |
69
|
|
|
|
|
|
|
|
70
|
22
|
50
|
|
|
|
247
|
if (my $backend_configs = delete $config->{backends}) { |
71
|
22
|
|
|
|
|
59
|
foreach my $name (keys %$backend_configs) { |
72
|
2
|
|
|
|
|
3
|
my %args = %{$backend_configs->{$name}}; |
|
2
|
|
|
|
|
6
|
|
73
|
2
|
|
100
|
|
|
6
|
my $type = delete($args{type}) // 'jsonrpc'; |
74
|
2
|
|
|
|
|
6
|
$dispatcher_config->add_backend($name => Mojo::WebSocketProxy::Backend->backend_instance($type => %args)); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$app->helper( |
79
|
|
|
|
|
|
|
wsp_config => sub { |
80
|
256
|
|
|
256
|
|
6432
|
my $c = shift; |
81
|
256
|
|
|
|
|
712
|
return $dispatcher_config; |
82
|
22
|
|
|
|
|
142
|
}); |
83
|
|
|
|
|
|
|
|
84
|
22
|
|
|
|
|
327
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__END__ |