Mojolicious::Lite websocket connection problem

276 views
Skip to first unread message

gtal...@gmail.com

unread,
Mar 1, 2016, 2:10:53 AM3/1/16
to Mojolicious
Dear Sir,

We meet a connection problem when we use Mojo websocket as server and use AS3WebSocket as client, it's can not successly create a connetion between client and server.

This problem just happen when we update to Mojolicious 6, everything is ok in Mojolicious 5.

Please give some guides.

Thanks in advance.

David



ActionScript 3 WebSocket Client  ( https://github.com/theturtle32/AS3WebSocket )
This is an AS3 implementation of a client library of the WebSocket protocol, as specified in the RFC6455 standard.


server code:

use Mojolicious::Lite;

# WebSocket echo service
websocket '/abc' => sub {
  my $c = shift;

  # Opened
  $c->app->log->debug('WebSocket opened');

  # Increase inactivity timeout for connection a bit
  $c->inactivity_timeout(300);

  # Incoming message
  $c->on(message => sub {
    my ($c, $msg) = @_;
    $c->send("echo: $msg");
  });

  # Closed
  $c->on(finish => sub {
    my ($c, $code, $reason) = @_;
    $c->app->log->debug("WebSocket closed with status $code");
  });
};

app->start;

server debug:

set MOJO_REACTOR=Mojo::Reactor::Poll
morbo --listen "http://*:3333" websocket_server.pl
Server available at http://127.0.0.1:3333
[Tue Mar  1 14:51:05 2016] [debug] GET "/abc"
[Tue Mar  1 14:51:05 2016] [debug] Routing to a callback
[Tue Mar  1 14:51:05 2016] [debug] WebSocket opened
[Tue Mar  1 14:51:05 2016] [debug] 101 Switching Protocols (0.002318s, 431.406/s)
[Tue Mar  1 14:51:05 2016] [debug] WebSocket closed with status 1006


client code:

import com.adobe.serialization.json.JSON; 
import com.worlize.websocket.*

var websocket:WebSocket = new WebSocket("ws://127.0.0.1:3333/abc", "*", "my-chat-protocol");
websocket.addEventListener(WebSocketEvent.CLOSED, handleWebSocketClosed);
websocket.addEventListener(WebSocketEvent.OPEN, handleWebSocketOpen);
websocket.addEventListener(WebSocketEvent.MESSAGE, handleWebSocketMessage);
websocket.addEventListener(WebSocketErrorEvent.CONNECTION_FAIL, handleConnectionFail);
websocket.connect();

function handleWebSocketOpen(event:WebSocketEvent):void {
  trace("Connected");
  websocket.sendUTF("Hello World!\n");

  var binaryData:ByteArray = new ByteArray();
  binaryData.writeUTF("Hello as Binary Message!");
  websocket.sendBytes(binaryData);
}

function handleWebSocketClosed(event:WebSocketEvent):void {
  trace("Disconnected");
}

function handleConnectionFail(event:WebSocketErrorEvent):void {
  trace("Connection Failure: " + event.text);
}

function handleWebSocketMessage(event:WebSocketEvent):void {
  if (event.message.type === WebSocketMessage.TYPE_UTF8) {
    trace("Got message: " + event.message.utf8Data);
  }
  else if (event.message.type === WebSocketMessage.TYPE_BINARY) {
    trace("Got binary message of length " + event.message.binaryData.length);
  }
}

client debug:

Disconnect

gtal...@gmail.com

unread,
Mar 2, 2016, 3:02:09 AM3/2/16
to Mojolicious
exactly, this problem just meet when we use Mojolicious version later than that of 6.22.


在 2016年3月1日星期二 UTC+8下午3:10:53,gtal...@gmail.com写道:

Heiko Jansen

unread,
Mar 3, 2016, 10:39:30 AM3/3/16
to Mojolicious
According to
up to v6.22 an incoming protocol selection ("Sec-WebSocket-Protocol" header, 3rd argument to "new WebSocket()" in your client) was automatically copied to the response headers.
That´s no longer the case and I assume your client automatically closes the connection if the subprotocol negotiation fails.
Apparently you have to manually select the appropriate subprotocol your server supports and add that to your response (which makes sense, since it´s an application specific decision and nothing Mojolicious can / should do for you generically).

HTH
Heiko

gtal...@gmail.com

unread,
Mar 3, 2016, 10:39:37 PM3/3/16
to Mojolicious
Thank you very much for your reply.
Reply all
Reply to author
Forward
0 new messages