line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Bitcoin::Feed::Site::BtcChina::Socket; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
595
|
use JSON; |
|
1
|
|
|
|
|
9713
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
532
|
use Mojo::Base 'Mojo::Transaction::WebSocket'; |
|
1
|
|
|
|
|
6363
|
|
|
1
|
|
|
|
|
9
|
|
5
|
1
|
|
|
1
|
|
105140
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
494
|
|
6
|
|
|
|
|
|
|
has 'owner'; |
7
|
|
|
|
|
|
|
has 'ping_interval'; |
8
|
|
|
|
|
|
|
has 'ping_timeout'; |
9
|
|
|
|
|
|
|
has 'last_ping_at'; |
10
|
|
|
|
|
|
|
has 'last_pong_at'; |
11
|
|
|
|
|
|
|
has 'timer'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub configure { |
14
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
15
|
1
|
|
|
|
|
1
|
my $owner = shift; |
16
|
1
|
|
|
|
|
3
|
$self->owner($owner); |
17
|
1
|
|
|
|
|
6
|
weaken($self->{owner}); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# call parse when receive text event |
20
|
|
|
|
|
|
|
$self->on( |
21
|
|
|
|
|
|
|
text => sub { |
22
|
4
|
|
|
4
|
|
5223
|
my ($self, $message) = @_; |
23
|
4
|
|
|
|
|
9
|
$self->parse($message); |
24
|
1
|
|
|
|
|
8
|
}); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
################################################ |
27
|
|
|
|
|
|
|
# setup events |
28
|
|
|
|
|
|
|
$self->on( |
29
|
|
|
|
|
|
|
subscribe => sub { |
30
|
3
|
|
|
3
|
|
16
|
my ($self, $channel) = @_; |
31
|
|
|
|
|
|
|
$self->on( |
32
|
|
|
|
|
|
|
'setup', |
33
|
|
|
|
|
|
|
sub { |
34
|
3
|
|
|
|
|
107
|
$self->send({text => qq(42["subscribe","$channel"])}); |
35
|
3
|
|
|
|
|
11
|
}); |
36
|
1
|
|
|
|
|
8
|
}); |
37
|
1
|
|
|
|
|
9
|
$self->emit('subscribe', 'marketdata_cnybtc'); |
38
|
1
|
|
|
|
|
7
|
$self->emit('subscribe', 'marketdata_cnyltc'); |
39
|
1
|
|
|
|
|
5
|
$self->emit('subscribe', 'marketdata_btcltc'); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#receive trade vent |
42
|
|
|
|
|
|
|
$self->on( |
43
|
|
|
|
|
|
|
trade => sub { |
44
|
1
|
|
|
1
|
|
6
|
my ($self, $data) = @_; |
45
|
1
|
|
|
|
|
3
|
$self->owner->emit( |
46
|
|
|
|
|
|
|
'data_out', |
47
|
|
|
|
|
|
|
$data->{date} * 1000, # the unit of timestamp is ms |
48
|
|
|
|
|
|
|
uc($data->{market}), |
49
|
|
|
|
|
|
|
$data->{price}); |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
7
|
}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$self->on( |
54
|
|
|
|
|
|
|
'ping', |
55
|
|
|
|
|
|
|
sub { |
56
|
0
|
|
|
0
|
|
0
|
$self->send({text => '2'}); |
57
|
1
|
|
|
|
|
12
|
}); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# ping ping! |
60
|
|
|
|
|
|
|
my $timer = AnyEvent->timer( |
61
|
|
|
|
|
|
|
after => 10, |
62
|
|
|
|
|
|
|
interval => 1, |
63
|
|
|
|
|
|
|
cb => sub { |
64
|
0
|
0
|
|
0
|
|
0
|
if (time() - $self->last_ping_at > $self->ping_interval / 1000) { |
65
|
0
|
|
|
|
|
0
|
$self->emit('ping'); |
66
|
0
|
|
|
|
|
0
|
$self->last_ping_at(time()); |
67
|
|
|
|
|
|
|
} |
68
|
1
|
|
|
|
|
12
|
}); |
69
|
1
|
|
|
|
|
10
|
$self->timer($timer); |
70
|
1
|
|
|
|
|
5
|
return; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#socket.io v2.2.2 |
74
|
|
|
|
|
|
|
sub parse { |
75
|
4
|
|
|
4
|
0
|
3
|
my ($self, $data) = @_; |
76
|
4
|
|
|
|
|
7
|
$self->owner->last_activity_at(time()); |
77
|
4
|
50
|
|
|
|
42
|
return unless $data =~ /^\d+/; |
78
|
4
|
|
|
|
|
15
|
my ($code, $body) = $data =~ /^(\d+)(.*)$/; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# connect, setup |
81
|
4
|
100
|
|
|
|
19
|
if ($code == 0) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
82
|
1
|
|
|
|
|
10
|
my $json_data = decode_json($body); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#session_id useless ? |
85
|
|
|
|
|
|
|
|
86
|
1
|
50
|
|
|
|
6
|
$self->ping_interval($json_data->{pingInterval}) |
87
|
|
|
|
|
|
|
if $json_data->{pingInterval}; |
88
|
1
|
50
|
|
|
|
8
|
$self->ping_timeout($json_data->{pingTimeout}) |
89
|
|
|
|
|
|
|
if $json_data->{pingTimeout}; |
90
|
1
|
|
|
|
|
7
|
$self->last_pong_at(time()); |
91
|
1
|
|
|
|
|
5
|
$self->last_ping_at(time()); |
92
|
1
|
|
|
|
|
6
|
$self->emit('setup'); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# pong |
96
|
|
|
|
|
|
|
elsif ($code == 3) { |
97
|
1
|
|
|
|
|
3
|
$self->last_pong_at(time()); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#disconnect ? reconnect! |
101
|
|
|
|
|
|
|
elsif ($code == 41) { |
102
|
1
|
|
|
|
|
3
|
$self->owner->debug('disconnected by server'); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#set timeout |
105
|
1
|
|
|
|
|
3
|
$self->owner->set_timeout(); |
106
|
|
|
|
|
|
|
} elsif ($code == 42) { |
107
|
1
|
|
|
|
|
11
|
my $json_data = decode_json($body); |
108
|
1
|
|
|
|
|
4
|
$self->emit($json_data->[0], $json_data->[1]); |
109
|
|
|
|
|
|
|
} |
110
|
4
|
|
|
|
|
37
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |