Question

I'm picking my way through the dartiverse_search example from the welcome page in dart editor. I see that it uses a path route to decide whether to transform a request into a websocket:

// The client will connect using a WebSocket. Upgrade requests to '/ws' and
// forward them to 'handleWebSocket'.
router.serve('/ws')
  .transform(new WebSocketTransformer())
  .listen(handleWebSocket);

Is it possible to turn a request into a websocket without using a routing path, for example using a query string to the root url?

Was it helpful?

Solution

You can specify any condition for upgrading to a WebSocket connection. You can even upgrade any connection request to a WebSocket connection without specifying a condition like this:

WebSocketTransformer.upgrade(request).then((WebSocket websocket) {
    websocket.listen((String text) { 
         // process sent data
    });
    websocket.add(JSON.encode("Hello"));
});

If the request is not a valid web socket upgrade request a HTTP response with status code 500 will be returned. Otherwise the returned future will complete with the [WebSocket] when the upgrade process is complete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top