line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Bitcoin::Feed::Site::CoinSetter::Socket; |
2
|
1
|
|
|
1
|
|
533
|
use JSON; |
|
1
|
|
|
|
|
8928
|
|
|
1
|
|
|
|
|
4
|
|
3
|
1
|
|
|
1
|
|
121
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
360
|
use Mojo::Base 'Mojo::Transaction::WebSocket'; |
|
1
|
|
|
|
|
6215
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has 'owner'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub configure { |
10
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
11
|
1
|
|
|
|
|
2
|
my $owner = shift; |
12
|
1
|
|
|
|
|
5
|
$self->owner($owner); |
13
|
1
|
|
|
|
|
9
|
weaken($self->{owner}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# call parse when receive text event |
16
|
|
|
|
|
|
|
$self->on( |
17
|
|
|
|
|
|
|
text => sub { |
18
|
2
|
|
|
2
|
|
2031
|
my ($self, $message) = @_; |
19
|
2
|
|
|
|
|
7
|
$self->parse($message); |
20
|
1
|
|
|
|
|
18
|
}); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
################################################ |
23
|
|
|
|
|
|
|
# setup events |
24
|
|
|
|
|
|
|
$self->on( |
25
|
|
|
|
|
|
|
subscribe => sub { |
26
|
1
|
|
|
1
|
|
12
|
my ($self, $channel) = @_; |
27
|
|
|
|
|
|
|
$self->on( |
28
|
|
|
|
|
|
|
'setup', |
29
|
|
|
|
|
|
|
sub { |
30
|
1
|
|
|
|
|
19
|
$self->send({text => qq(5:::{"name":"$channel","args":[""]})}); |
31
|
1
|
|
|
|
|
8
|
}); |
32
|
1
|
|
|
|
|
13
|
}); |
33
|
1
|
|
|
|
|
14
|
$self->emit('subscribe', 'last room'); |
34
|
|
|
|
|
|
|
$self->on( |
35
|
|
|
|
|
|
|
last => sub { |
36
|
1
|
|
|
1
|
|
8
|
my ($self, $data) = @_; |
37
|
1
|
|
|
|
|
4
|
$self->owner->emit('data_out', $data->[0]{'timeStamp'}, 'BTCUSD', $data->[0]{price}); |
38
|
1
|
|
|
|
|
21
|
}); |
39
|
1
|
|
|
|
|
8
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#socketio v0.9.6 |
43
|
|
|
|
|
|
|
sub parse { |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
2
|
0
|
4
|
my ($tx, $data) = @_; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
8
|
my @packets = ('disconnect', 'connect', 'heartbeat', 'message', 'json', 'event', 'ack', 'error', 'noop'); |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
8
|
my $regexp = qr/([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/; |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
27
|
my @pieces = $data =~ $regexp; |
52
|
2
|
50
|
|
|
|
7
|
return {} unless @pieces; |
53
|
2
|
|
50
|
|
|
10
|
my $id = $pieces[1] || ''; |
54
|
2
|
|
100
|
|
|
7
|
$data = $pieces[4] || ''; |
55
|
2
|
|
50
|
|
|
15
|
my $packet = { |
56
|
|
|
|
|
|
|
type => $packets[$pieces[0]], |
57
|
|
|
|
|
|
|
endpoint => $pieces[3] || '', |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# whether we need to acknowledge the packet |
61
|
2
|
50
|
|
|
|
5
|
if ($id) { |
62
|
0
|
|
|
|
|
0
|
$packet->{id} = $id; |
63
|
0
|
0
|
|
|
|
0
|
if ($pieces[3]) { |
64
|
0
|
|
|
|
|
0
|
$packet->{ack} = 'data'; |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
0
|
$packet->{ack} = 'true'; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# handle different packet types |
72
|
2
|
50
|
|
|
|
21
|
if ($packet->{type} eq 'error') { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# need do nothing now. |
75
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'message') { |
76
|
0
|
|
0
|
|
|
0
|
$packet->{data} = $data || ''; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#"5:::{"name":"last","args":[{"price":367,"size":0.03,"exchangeId":"COINSETTER","timeStamp":1417382915802,"tickId":14667678802537,"volume":14.86,"volume24":102.43}]}" |
80
|
|
|
|
|
|
|
elsif ($packet->{type} eq 'event') { |
81
|
1
|
50
|
|
|
|
2
|
eval { |
82
|
1
|
|
|
|
|
35
|
my $opts = decode_json($data); |
83
|
1
|
|
|
|
|
3
|
$packet->{name} = $opts->{name}; |
84
|
1
|
|
|
|
|
7
|
$packet->{args} = $opts->{args}; |
85
|
|
|
|
|
|
|
} || 0; # add '|| 0' to avoid critic test failed |
86
|
1
|
|
50
|
|
|
3
|
$packet->{args} ||= []; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
6
|
$tx->emit($packet->{name}, $packet->{args}); |
89
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'json') { |
90
|
0
|
|
|
|
|
0
|
evel { |
91
|
0
|
|
|
|
|
0
|
$packet->{data} = decode_json($data); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'connect') { |
94
|
1
|
|
50
|
|
|
7
|
$packet->{qs} = $data || ''; |
95
|
1
|
|
|
|
|
9
|
$tx->emit('setup'); |
96
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'ack') { |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# nothing to do now |
99
|
|
|
|
|
|
|
# because this site seems don't emit this packet. |
100
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'heartbeat') { |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#send back the heartbeat |
103
|
0
|
|
|
|
|
0
|
$tx->send({text => qq(2:::)}); |
104
|
|
|
|
|
|
|
} elsif ($packet->{type} eq 'disconnect') { |
105
|
0
|
|
|
|
|
0
|
$tx->owner->debug('disconnected by server'); |
106
|
0
|
|
|
|
|
0
|
$tx->owner->set_timeout(); |
107
|
|
|
|
|
|
|
} |
108
|
2
|
|
|
|
|
104
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |