line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Bitcoin::Feed::Site::BtcChina::Socket; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
489
|
use JSON; |
|
1
|
|
|
|
|
8900
|
|
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
439
|
use Mojo::Base 'Mojo::Transaction::WebSocket'; |
|
1
|
|
|
|
|
6033
|
|
|
1
|
|
|
|
|
8
|
|
5
|
1
|
|
|
1
|
|
110434
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
562
|
|
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
|
|
|
|
|
7
|
weaken($self->{owner}); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# call parse when receive text event |
20
|
|
|
|
|
|
|
$self->on( |
21
|
|
|
|
|
|
|
text => sub { |
22
|
4
|
|
|
4
|
|
5166
|
my ($self, $message) = @_; |
23
|
4
|
|
|
|
|
10
|
$self->parse($message); |
24
|
1
|
|
|
|
|
8
|
}); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
################################################ |
27
|
|
|
|
|
|
|
# setup events |
28
|
|
|
|
|
|
|
$self->on( |
29
|
|
|
|
|
|
|
subscribe => sub { |
30
|
3
|
|
|
3
|
|
17
|
my ($self, $channel) = @_; |
31
|
|
|
|
|
|
|
$self->on( |
32
|
|
|
|
|
|
|
'setup', |
33
|
|
|
|
|
|
|
sub { |
34
|
3
|
|
|
|
|
99
|
$self->send({text => qq(42["subscribe","$channel"])}); |
35
|
3
|
|
|
|
|
16
|
}); |
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
|
|
|
|
|
4
|
$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
|
|
|
|
|
9
|
}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$self->on( |
54
|
|
|
|
|
|
|
'ping', |
55
|
|
|
|
|
|
|
sub { |
56
|
0
|
|
|
0
|
|
0
|
$self->send({text => '2'}); |
57
|
1
|
|
|
|
|
5
|
}); |
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
|
|
|
|
|
10
|
}); |
69
|
1
|
|
|
|
|
9
|
$self->timer($timer); |
70
|
1
|
|
|
|
|
5
|
return; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#socket.io v2.2.2 |
74
|
|
|
|
|
|
|
sub parse { |
75
|
4
|
|
|
4
|
0
|
4
|
my ($self, $data) = @_; |
76
|
4
|
|
|
|
|
8
|
$self->owner->last_activity_at(time()); |
77
|
4
|
50
|
|
|
|
44
|
return unless $data =~ /^\d+/; |
78
|
4
|
|
|
|
|
16
|
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
|
|
|
|
5
|
$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
|
|
|
|
|
5
|
$self->last_pong_at(time()); |
91
|
1
|
|
|
|
|
4
|
$self->last_ping_at(time()); |
92
|
1
|
|
|
|
|
6
|
$self->emit('setup'); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# pong |
96
|
|
|
|
|
|
|
elsif ($code == 3) { |
97
|
1
|
|
|
|
|
4
|
$self->last_pong_at(time()); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#disconnect ? reconnect! |
101
|
|
|
|
|
|
|
elsif ($code == 41) { |
102
|
1
|
|
|
|
|
4
|
$self->owner->debug('disconnected by server'); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#set timeout |
105
|
1
|
|
|
|
|
3
|
$self->owner->set_timeout(); |
106
|
|
|
|
|
|
|
} elsif ($code == 42) { |
107
|
1
|
|
|
|
|
13
|
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; |