Perl Mojolicious & Socket.IO doesn't play well

I tried to experiment with Socket.IO and Perl Mojolicious . while I was able to do the same with WebSockets , I tried to do the same with Socket.IO (due to the wider browser support that I need), it did not play well.

I am using a Morbo server. code:

#!/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 part of the code works well ( WebSocket ), when I uncomment the part of Socket.IO , it fails 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

Answer:

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 receive from the client when using 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

, Socket.IO doenst ? , Mojo :

/socket.io/1

:

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

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

+4
2

, cdn , cdn. , .

+1

, socket.io - node. websocket api mojo.

0

All Articles