line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Bitcoin::Feed::Site::CoinSetter::Socket; |
2
|
1
|
|
|
1
|
|
499
|
use JSON; |
|
1
|
|
|
|
|
9076
|
|
|
1
|
|
|
|
|
4
|
|
3
|
1
|
|
|
1
|
|
101
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
410
|
use Mojo::Base 'Mojo::Transaction::WebSocket'; |
|
1
|
|
|
|
|
5881
|
|
|
1
|
|
|
|
|
5
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has 'owner'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub configure { |
10
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
11
|
1
|
|
|
|
|
1
|
my $owner = shift; |
12
|
1
|
|
|
|
|
3
|
$self->owner($owner); |
13
|
1
|
|
|
|
|
6
|
weaken($self->{owner}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# call parse when receive text event |
16
|
|
|
|
|
|
|
$self->on( |
17
|
|
|
|
|
|
|
text => sub { |
18
|
2
|
|
|
2
|
|
1031
|
my ($self, $message) = @_; |
19
|
2
|
|
|
|
|
4
|
$self->parse($message); |
20
|
1
|
|
|
|
|
10
|
}); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
################################################ |
23
|
|
|
|
|
|
|
# setup events |
24
|
|
|
|
|
|
|
$self->on( |
25
|
|
|
|
|
|
|
subscribe => sub { |
26
|
1
|
|
|
1
|
|
8
|
my ($self, $channel) = @_; |
27
|
|
|
|
|
|
|
$self->on( |
28
|
|
|
|
|
|
|
'setup', |
29
|
|
|
|
|
|
|
sub { |
30
|
1
|
|
|
|
|
12
|
$self->send({text => qq(5:::{"name":"$channel","args":[""]})}); |
31
|
1
|
|
|
|
|
3
|
}); |
32
|
1
|
|
|
|
|
7
|
}); |
33
|
1
|
|
|
|
|
7
|
$self->emit('subscribe', 'last room'); |
34
|
|
|
|
|
|
|
$self->on( |
35
|
|
|
|
|
|
|
last => sub { |
36
|
1
|
|
|
1
|
|
8
|
my ($self, $data) = @_; |
37
|
1
|
|
|
|
|
3
|
$self->owner->emit('data_out', $data->[0]{'timeStamp'}, 'BTCUSD', $data->[0]{price}); |
38
|
1
|
|
|
|
|
9
|
}); |
39
|
1
|
|
|
|
|
5
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#socketio v0.9.6 |
43
|
|
|
|
|
|
|
sub parse { |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
2
|
0
|
3
|
my ($tx, $data) = @_; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
5
|
my @packets = ('disconnect', 'connect', 'heartbeat', 'message', 'json', 'event', 'ack', 'error', 'noop'); |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
5
|
my $regexp = qr/([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/; |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
20
|
my @pieces = $data =~ $regexp; |
52
|
2
|
50
|
|
|
|
5
|
return {} unless @pieces; |
53
|
2
|
|
50
|
|
|
8
|
my $id = $pieces[1] || ''; |
54
|
2
|
|
100
|
|
|
5
|
$data = $pieces[4] || ''; |
55
|
2
|
|
50
|
|
|
12
|
my $packet = { |
56
|
|
|
|
|
|
|
type => $packets[$pieces[0]], |
57
|
|
|
|
|
|
|
endpoint => $pieces[3] || '', |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# whether we need to acknowledge the packet |
61
|
2
|
50
|
|
|
|
3
|
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
|
|
|
|
11
|
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
|
|
|
|
|
21
|
my $opts = decode_json($data); |
83
|
1
|
|
|
|
|
1
|
$packet->{name} = $opts->{name}; |
84
|
1
|
|
|
|
|
5
|
$packet->{args} = $opts->{args}; |
85
|
|
|
|
|
|
|
} || 0; # add '|| 0' to avoid critic test failed |
86
|
1
|
|
50
|
|
|
2
|
$packet->{args} ||= []; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
4
|
$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
|
|
|
4
|
$packet->{qs} = $data || ''; |
95
|
1
|
|
|
|
|
4
|
$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
|
|
|
|
|
73
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |