Advertisement
Guest User

Untitled

a guest
Apr 1st, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. my $ua = Mojo::UserAgent->new();
  2. $ua->websocket('ws://localhost:4008/ebuge' => sub { # tried ws: and http:
  3. my ($ua, $tx) = @_;
  4. say "tx is $tx"; # Mojo::Transaction::HTTP=HASH(0xa029678)
  5. $DB::single = 1;
  6. unless ($tx->is_websocket) {
  7. say "Error: ".$tx->res->error; # Error: Premature connection close
  8. }
  9. $tx->on(message => sub {
  10. my ($tx, $message) = @_;
  11. say anydump($message);
  12. });
  13. $tx->send('hello'); # Can't locate object method "send" via package "Mojo::Transaction::HTTP"
  14. });
  15. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. #!/usr/bin/perl
  23. use strict;
  24. use warnings;
  25. use YAML::Syck;
  26. use JSON::XS;d
  27. use File::Slurp;
  28. use Scriptalicious;
  29. use Carp 'confess';
  30. use v5.18;
  31. use FindBin '$Bin';
  32. say "\n\n\nwe are $Bin/$0";
  33.  
  34. use Mojolicious::Lite;
  35.  
  36. my $ebug;
  37. websocket '/ebuge' => sub {
  38. my $self = shift;
  39.  
  40. $self->app->log->info("WebSocket opened");
  41.  
  42. my $output = sub {
  43. my ($return, $time) = @_;
  44. my ($stdout, $stderr) = $ebug->output;
  45. $self->tx->send(encode_json {
  46. line => $ebug->line,
  47. stdout => $stdout,
  48. stderr => $stderr,
  49. finished => $ebug->finished,
  50. package => $ebug->package,
  51. subroutine => $ebug->subroutine,
  52. line => $ebug->line,
  53. filename => $ebug->filename,
  54. code => $ebug->codeline,
  55. return => $return,
  56. });
  57. };
  58.  
  59. $self->on(message => sub {
  60. my ($self, $msg) = @_;
  61.  
  62. $self->app->log->info("WebSocket: $msg");
  63.  
  64. if ($msg =~ /hello/) {
  65. start_timer();
  66. $ebug = _load();
  67. my $time = show_delta();
  68. $output->(undef, $time);
  69. }
  70. elsif ($msg =~ /exec (.+)/) {
  71. my $command = $1;
  72. start_timer();
  73. my $return = $ebug->$command();
  74. my $time = show_delta();
  75. $output->($return, $time);
  76. }
  77. });
  78.  
  79. $self->on(finish => sub {
  80. my ($self, $code, $reason) = @_;
  81. $self->app->log->debug("WebSocket closed with status $code.");
  82. });
  83.  
  84. };
  85.  
  86. sub _load {
  87.  
  88. my $ebug = Devel::ebug->new;
  89. $ebug->program('test/stylehouse.pl');
  90. $ebug->load;
  91.  
  92. # litter it with places to wait
  93. # might even need a middleware so we can consider the jam instead of just jamming
  94. my @filenames = $ebug->filenames();
  95. @filenames = grep { /^lib|stylehouse\.pl$/ } @filenames;
  96. for my $filename (@filenames) {
  97. my $code = capture("cat $filename");
  98. my @codelines = split "\n", $code;
  99. my $line = 0;
  100. for my $codeline (@codelines) {
  101. if ($codeline =~ /my \$self = shift;/) {
  102. $ebug->break_point($filename, $line);
  103. }
  104. $line++;
  105. }
  106. }
  107. }
  108.  
  109. my $daemon = Mojo::Server::Daemon->new(app => app, listen => ['http://localhost:4005']);
  110. $daemon->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement