Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Last active September 22, 2015 06:21
Show Gist options
  • Save p120ph37/c2fb59ead8983cdbd130 to your computer and use it in GitHub Desktop.
Save p120ph37/c2fb59ead8983cdbd130 to your computer and use it in GitHub Desktop.
Perl NPH CGI streaming Comet events
#!/usr/bin/perl
use warnings;
use strict;
sub chunk { sprintf "%x\x0D\x0A%s\x0D\x0A", length($_[0]), $_[0]; }
$|=1;
print "HTTP/1.1 200 Ok\x0D\x0A";
print "Content-Type: text/html\x0D\x0A";
print "Transfer-Encoding: chunked\x0D\x0A";
print "\x0D\x0A";
print chunk((' ' x 1024)."\n"); # enough blank space to break buffering
print chunk("<div id=\"counter\">0</div>\n");
for my $i (1..10) {
print chunk("<script>document.getElementById('counter').innerHTML='$i';</script>\n");
sleep 1;
}
print chunk('');
@p120ph37
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment