shelf_route and shelf_web_socket

268 visninger
Gå til det første ulæste opslag

Oliver

ulæst,
20. sep. 2014, 09.31.0720.09.2014
til mi...@dartlang.org
I'm trying to use shelf_route in combination with shelf_web_socket to have both multiple WebSocket endpoints and a REST api on the same webserver.
My current code looks like this (stripped down):

import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart' as route;
import 'package:shelf_web_socket/shelf_web_socket.dart';


void main(List<String> arguments) {

  r
(shelf.Request re) {
   
return new shelf.Response.ok('test');
 
}

 
var router = (route.router()
     
..add( '/rest', ['GET'], r)
     
..add( '/ws', ['GET'], webSocketHandler((ws) {
       
print('Connected!');
     
})));

 
var handler = const shelf.Pipeline()
     
.addMiddleware(shelf.logRequests())
     
.addHandler(router.handler);

  io
.serve(handler, '127.0.0.1', 30091).then((server) {
   
print('Serving at http://${server.address.host}:${server.port}');
 
});
}


The code works fine for the REST part. But if I try to connect to the websocket server, I get the following exception:

Error thrown by handler.
Illegal argument(s): webSocketHandler may only be used with a server that supports request hijacking.
package:shelf_web_socket/src/web_socket_handler.dart 53:7  handle
package:shelf_route/shelf_route.dart 294:20                _Route.handle
package:shelf_route/shelf_route.dart 224:24                _handleRequest
dart
:isolate                                               _RawReceivePortImpl._handleMessage


At the beginning of a request, hijacking seems to be supported. But the problem comes in combination with the route package.
I tracked the problem down to the Request.change method in the shelf package. It copies a request, but doesn't copy the hijacking handler. 
I wonder if this behavior is intentional and I'm using the packages in the wrong way, or if it was missed during writing the package?

Oliver

ulæst,
21. sep. 2014, 12.27.1121.09.2014
til mi...@dartlang.org
I created a simple fix for the problem here.

Natalie Weizenbaum

ulæst,
22. sep. 2014, 16.17.0422.09.2014
til General Dart Discussion
It looks like the shelf_route package isn't properly forwarding the request objects so that they preserve all of their metadata, in particular the onHijack field. It should be using Request.change to create new requests based on old ones, but it may not be.

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Anders Holmgren

ulæst,
22. sep. 2014, 16.17.1322.09.2014
til mi...@dartlang.org
I recommend you file a bug on shelf. It seems pretty clear cut to me unless I'm missing something too.

Natalie Weizenbaum

ulæst,
22. sep. 2014, 16.21.5622.09.2014
til General Dart Discussion
I believe this is a bug in shelf_route, not shelf itself.

On Mon, Sep 22, 2014 at 1:17 PM, Anders Holmgren <andersm...@gmail.com> wrote:
I recommend you file a bug on shelf. It seems pretty clear cut to me unless I'm missing something too.

Oliver

ulæst,
22. sep. 2014, 16.38.3622.09.2014
til mi...@dartlang.org
No, shelf_route uses Request.change. But the problem is that the Request.change method from shelf is not forwarding the onHijack callback at all. See https://github.com/dart-lang/bleeding_edge/blob/master/dart/pkg/shelf/lib/src/request.dart#L197
I would expect that it looks somehow like this: https://github.com/Fox32/shelf/blob/master/lib/src/request.dart#L119

Natalie Weizenbaum

ulæst,
22. sep. 2014, 16.54.1922.09.2014
til General Dart Discussion
Ah, you're right. Can you file an issue against shelf for that, or better yet, submit a patch?

Oliver

ulæst,
22. sep. 2014, 17.21.1922.09.2014
til mi...@dartlang.org
I created issue 21043 for that.

I would submit a patch if that package would be hosted on GitHub. Setting it up for Google Code is to much work =(
Svar alle
Svar til forfatter
Videresend
0 nye opslag