snoofkin
snoofkin

Reputation: 8895

Perl Mojolicious & Socket.IO doesn't play well

I was trying to experiment with Socket.IO and Perl's Mojolicious. while I was able to perform the same with WebSockets I tried to do the same with Socket.IO (due to wider browser support that I need) it didn't play well.

I'm using the Morbo server. the code is:

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/' => sub {
    my $self = shift;

    $self->render('index');
};

websocket '/echo' => sub {
    my $self       = shift;
    state $counter = 1;

    $self->res->headers->header('Access-Control-Allow-Origin' => "*");
    Mojo::IOLoop->recurring(1.5 => sub {
          my $loop = shift;
          $self->send(sprintf("Websocket request: %d\n",$counter++));

  });
};

app->start;

__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
  <head>
     <title>Socket.IO test</title>
     <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
     <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
  </head>
  <body>
  <div id="dumper"></div>
<script>
   var ws = new WebSocket('<%= url_for('echo')->to_abs %>');

   ws.onmessage = function(e) {
      var message = e.data;
      console.log(message);
      $("#dumper").text(message);
    };
 // var socket = io.connect('<%= url_for('echo')->to_abs %>');
//  socket.on('data', function (data) {
//    console.log(data);
//  });
</script>


  </body>
</html>

This code part work well (WebSocket), when I uncomment the Socket.IO part, it fails on the io.connect(...) with the following error (Header and Response):

Request:

GET /socket.io/1/?t=1385234823089 HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Accept: */*
Referer: http://127.0.0.1:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Response:

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Type: text/html;charset=UTF-8
Date: Sat, 23 Nov 2013 19:27:03 GMT
Content-Length: 6163
Server: Mojolicious (Perl)

The Request I get from the client while using the WebSocket is this:

GET ws://127.0.0.1:3000/echo HTTP/1.1
Pragma: no-cache
Origin: http://127.0.0.1:3000
Host: 127.0.0.1:3000
Sec-WebSocket-Key: zgob5gss5XTr9vzYwYNe+A==
Upgrade: websocket
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cache-Control: no-cache
Connection: Upgrade
Sec-WebSocket-Version: 13

it seems that Socket.IO doenst request for the socket upgrade? Also, Mojo is not aware of this route:

/socket.io/1

I also tried replacing:

io.connect('<%= url_for('echo')->to_abs %>');

with io.connect('http://localhost:300/echo');

Upvotes: 4

Views: 1014

Answers (2)

shaneburgess
shaneburgess

Reputation: 15872

I think that socket.io is a node app. I have always had to go with the stock websocket api for my mojo apps.

Upvotes: 0

Joel Berger
Joel Berger

Reputation: 20280

It looks to me like the file from the cdn is requesting files from your server that ought to be coming from the cdn. My guess is there is additional configuration needed.

Upvotes: 1

Related Questions