Skip to content

Commit

Permalink
Merge pull request #14 from waynieack/HTTP_Updates
Browse files Browse the repository at this point in the history
Http 1.1 updates#3
  • Loading branch information
hplato committed Aug 24, 2017
2 parents fc2e940 + e5e77bd commit 7a74942
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 11 additions & 5 deletions lib/ajax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sub new {
${ $$self{changed} } = 0; # Flag if at least on state is changed
${ $$self{event} } = "&ChangeChecker::setWaiterToChanged ('$self')";
${ $$self{sub} } = $sub;
${ $$self{checkTime} } = 1;

return $self;
}
Expand Down Expand Up @@ -82,10 +83,11 @@ sub checkForUpdate {
# connection on the client end.
my $html_head = "HTTP/1.1 204 No Content\r\n";
$html_head .= "Server: MisterHouse\r\n";
$html_head .= "Connection: close\r\n";
$html_head .= "Date: " . ::time2str(time) . "\r\n";
$html_head .= "\r\n";
&::print_socket_fork( ${ $$self{waitingSocket} }, $html_head );
${ $$self{waitingSocket} }->close;
&::print_socket_fork( ${ $$self{waitingSocket} }, $html_head, 1 );
#${ $$self{waitingSocket} }->close;
return 1;
}

Expand All @@ -97,7 +99,7 @@ sub checkForUpdate {

if ($xml) {
&main::print_log("checkForUpdate sub ${$$self{sub}} returned $xml") if $main::Debug{ajax};
&::print_socket_fork( ${ $$self{waitingSocket} }, $xml );
&::print_socket_fork( ${ $$self{waitingSocket} }, $xml, 1 );
# No need to close the socket with HTTP1.1, also this causes issues with a forked socket
#&main::print_log( "Closing Socket " . ${ $$self{waitingSocket} } ) if $main::Debug{ajax};
#${ $$self{waitingSocket} }->shutdown(2); #Changed this from close() to shutdown(2). In some cases, the parent port wasn't being closed -- ie. speech events
Expand Down Expand Up @@ -184,10 +186,14 @@ sub addWaiter {

sub checkWaiters {
my ($class) = @_;

my $delay = 250;
my $currenttime = &main::get_tickcount;
foreach my $key ( keys %waiters ) {
my $self = $waiters{$key};
next unless ( ($currenttime - ${ $$self{checkTime} }) >= $delay );
${ $$self{checkTime} } = $currenttime;
#&main::print_log("waiter: checkWaiters checking sub sub ".${$$self{sub}} ) if $main::Debug{ajax};
if ( $waiters{$key}->checkForUpdate ) {

# waiter can be removed
delete $waiters{$key};
&main::print_log("waiter '$key' removed") if $main::Debug{ajax};
Expand Down
3 changes: 2 additions & 1 deletion lib/json_server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ sub json_page {
my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: application/json\r\n";
$output .= "Connection: close\r\n";
if ($options =~ m/compress/) {
print_log("json_server.pl: DEBUG: Compressing Data as requested by client") if $Debug{json};
my $json;
Expand Down Expand Up @@ -1320,7 +1321,7 @@ sub json_usage {
my $html_head = "HTTP/1.1 200 OK\r\n";
$html_head .= "Server: MisterHouse\r\n";
$html_head .= "Content-type: application/json\r\n";
$html_head .= "Content-Encoding: gzip\r\n";
$html_head .= "Connection: close\r\n";
$html_head .= "Content-Length: " . ( length $html ) . "\r\n";
$html_head .= "Date: " . time2str(time) . "\r\n";
$html_head .= "\r\n";
Expand Down

0 comments on commit 7a74942

Please sign in to comment.