Subject: | New features. Patchfile included. |
Hi.
New features in patchfile:
What's new?
# example 0: # AnyEvent::HTTP initialization parameters
my $client = AnyEvent::MtGox::Stream->new(
request_params => {timeout => 30, headers =>
{'user-agent' => 'MySearchClient 1.0'},},
on_message => sub { },
on_error => sub { },
on_disconnect => sub { },
);
# example 1: custom service url
my $client = AnyEvent::MtGox::Stream->new(
url => 'http://socketio.mtgox.com/socket.io/',
on_message => sub { },
on_error => sub { },
on_disconnect => sub { },
);
# example 2: custom heartbeat period
my $client = AnyEvent::MtGox::Stream->new(
heartbeat_eval => sub { $_[0] - 10; },
on_message => sub { },
on_error => sub { },
on_disconnect => sub { },
);
# example 3: dynamic heartbeat timer
my $time0;
my $scalar = {heartbeat_timer => undef,};
$cl = AnyEvent::MtGox::Stream->new(
heartbeat_eval => sub{ ($time0 = $_[0]) / 2 }, # set
half of time0
store => \$scalar,
on_message => sub { },
on_error => sub { },
on_disconnect => sub { },
);
...
$store->{heartbeat_timer}->set(1, $time0 * 2/3 ); # set 2/3 of time0
Regards,
Anonymous.
Subject: | patchfile.patch |
diff --git a/Stream.pm b/Stream.pm
index d31e73f..10ef937 100644
--- a/Stream.pm
+++ b/Stream.pm
@@ -16,6 +16,11 @@ use URI;
our $VERSION = '0.02';
$VERSION = eval $VERSION;
+our $conf = {
+ def_url => 'http://socketio.mtgox.com/socket.io/1',
+ heartbeat_dec => 2,
+ };
+
sub new {
my ($class, %params) = @_;
@@ -24,14 +29,16 @@ sub new {
my $on_error = $params{on_error} || sub { croak @_ };
my $on_message = $params{on_message} || sub { };
- my $server = 'socketio.mtgox.com';
+ my $store = $params{store} || \{};
my $handle;
+ my @rqst_params = [];
+ $params{request_params} and @rqst_params = %{$params{request_params}};
# Socket.IO handshake.
- my $uri = URI->new("http://$server/socket.io/1");
+ my $uri = URI->new($params{url} || $conf->{def_url});
$uri->scheme('https') if $secure;
AE::log debug => "Making Socket.IO handshake to $uri";
- http_post $uri, undef, sub {
+ http_post $uri, undef, @rqst_params, sub {
my ($body, $headers) = @_;
return $on_error->('Socket.IO handshake failed')
unless '200' eq $headers->{Status} and defined $body;
@@ -75,8 +82,15 @@ sub new {
$send_message->('2::');
};
- $timer = AE::timer $heartbeat - 2, $heartbeat - 2, $send_heartbeat
- if $heartbeat;
+ $heartbeat and (
+ ($timer = AE::timer $heartbeat - 2,
+ ($params{heartbeat_eval}
+ and abs(int $params{heartbeat_eval}->($heartbeat))) ||
+ ($heartbeat - $conf->{heartbeat_dec}),
+ $send_heartbeat),
+ (exists $$store->{heartbeat_timer} and $$store->{heartbeat_timer} = $timer
+ )
+ );
my $json = JSON->new;
diff --git a/out.patch b/out.patch
new file mode 100644
index 0000000..e69de29