Weird Websockets Bug?

393 views
Skip to first unread message

Matt f

unread,
May 17, 2014, 1:45:33 PM5/17/14
to mi...@dartlang.org
I made a server and client using Websockets in Dart. It works great and I love it. I've used it with my friends, however for some reason when a certain one of my friends tries to connect to the server, it always crashes the server with this message:

Uncaught Error: WebSocketException: Invalid WebSocket upgrade request
Unhandled exception:
WebSocketException: Invalid WebSocket upgrade request
#0      _rootHandleUncaughtError.<anonymous closure>.<anonymous closure> (dart:async/zone.dart:713)
#1      _AsyncCallbackEntry.callback (dart:async/schedule_microtask.dart:1)
#2      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#3      _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#4      _asyncRunCallback (dart:async/schedule_microtask.dart:36)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:119)

I don't think it's a problem in my code. Unless I forgot to do something. Anyone know how to fix?

Robert Åkerblom-Andersson

unread,
May 18, 2014, 3:16:18 AM5/18/14
to mi...@dartlang.org
Hi Matt,

It's a little hard to say, it sounds like the problem might be on your friends side.

Ask him/her to go here to see if websockets works in his/her browser:

If it does not, then that's the problem.  

I also have a websocket chat up and running here that you can try with that friend to see if it's your code or not:

It written 100% in Dart, the code for it is here:

Regards, Robert 

Tom Yeh

unread,
May 18, 2014, 11:29:23 PM5/18/14
to mi...@dartlang.org
The stack trace is similar to the bug reported in Issue 17468.

Rico Wind

unread,
May 19, 2014, 2:04:25 AM5/19/14
to General Dart Discussion, Anders Johnsen
+ajohnsen
Matt: What browser is your friend using?

Cheers,
Rico


On Mon, May 19, 2014 at 5:29 AM, Tom Yeh <tom....@gmail.com> wrote:
The stack trace is similar to the bug reported in Issue 17468.

--
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 Johnsen

unread,
May 19, 2014, 3:13:50 AM5/19/14
to Rico Wind, General Dart Discussion
Hi Matt,

There are two problems here; the unhandled exception and the fact that you actually get the exception. Let's start with the former.

The "WebSocketException: Invalid WebSocket upgrade request" exception is throws by the dart:io, when it's unable to upgrade HttpRequest to a WebSocket. There are two ways to do this with the API in dart:io.

With futures:

  server.listen((request) {
    WebSocketTransformer.upgrade(request).then((webSocket) {
        // Success, upgraded
      })
      .catchError((error, stackTrace) {
        // Failed to upgrade (the above exception would be caught here).
      });
  });

and with streams:

  server
    .transform(new WebSocketTransformer())
    .listen((webSocket) {
      // Success, a request was upgraded
    }, onError: 
(error, stackTrace) {
      // Failed to upgrade (the above exception would be caught here).
      // Can be called multiple times for streams.
    });
  });

Depending on which API you use, you can use the above for inspiration on how to catch this error.


Now, the other problem, that you actually see this error. I'm not sure how you did this, but if a browser tries to connect to your server as a regular GET request and you try to upgrade the request, it will fail like above, as the right headers are not set. Be sure to only upgrade the requests meant for websocket communication. A common pattern is to use either a different port or a unique path, e.g.

  ws://myhost/ws

And then only attempt to upgrade the requests where the path is equal to '/ws'.

I hope this can help you a bit further.

Cheers,

- Anders

Antoine Goutenoir

unread,
Nov 3, 2014, 4:35:43 PM11/3/14
to mi...@dartlang.org, ri...@google.com
Thanks y'all for this, it helped me manage that weird exception that made my chat server crash.

It still raises a WebSocketException, but now the server handles it gracefully ;)

The Exception happens when somebody tries to connect with a first generation iPad (#MC349NF) (I don't have one handy for a deeper debugging, but one of my friends does).

He can successfully connect and send messages on http://www.websocket.org/echo.html by the way.

Here's the related excerpt of the code I'm using for the server :

    HttpServer.bind(InternetAddress.ANY_IP_V6, 8080).then((HttpServer server) {
      server
       
.where((request) => request.uri.path == '/ws')
       
.handleError((error, stackTrace){
          handleError
('where', error, stackTrace);
       
})
       
.transform(new WebSocketTransformer(protocolSelector: (_) => 'wamp'))
       
.handleError((error, stackTrace){
          handleError
('transform', error, stackTrace);
       
})
       
.pipe(chatHandler)
       
.catchError((error, stackTrace) {
          handleError
('pipe', error, stackTrace);
       
});
     
print("WAMP chat server is running...");
   
});

The handleError function is a simple wrapper for print, and chatHandler is a custom StreamConsumer. I can disclose the whole source if it helps. I'm using the latest Dart version (1.7.3 if memory serves), compiled by myself because of the libc6 shenanigans with debian.

I'm not sure I can fix the WebSocketException on my end, seems like a problem inherent to Dart itself. Am I wrong ?

Reply all
Reply to author
Forward
0 new messages