title: Dart/Flutter WebSocket server/client is not working properly in the same side
body: <p>im trying to develop an oauth2 demo for my interests. In the demo when user hit authorization server, authorization server return to me with the redirect url. In my server i reserved an endpoint for this redirect url. In this endpoint i take request's queryparameters and i connect to a websocket via <a href="https://github.com/dart-lang/web_socket_channel" rel="nofollow noreferrer">web_socket_channel</a> . After connection i send the query paramater data to web socket. Below i'll add the implementation.</p>
  <pre><code>import 'package:aqueduct/aqueduct.dart';
  import 'package:web_socket_channel/web_socket_channel.dart' as ws;
  class AuthorizationController extends ResourceController{
    static final uri = Uri(scheme: 'ws',host: 'localhost',pathSegments: ['connect'],port: 8888);
  
    @Operation.get()
    Future&lt;Response&gt; manageRedirection()async{
      Map&lt;String,dynamic&gt; map = request.raw.uri.queryParameters;
      sendToWs(map);
      return Response.ok(map.toString());
    }
  
    void sendToWs(Map&lt;String,dynamic&gt; map){
      final channel =ws.WebSocketChannel.connect(uri);
      print(&quot;AuthorizationController: data -&gt; $map&quot;);
      channel.sink.add(map);
    }
  }
  </code></pre>
  <p>Now in the server side(http server's /connect endpoint reserved for this) i upgrade incoming requests and i create a <code>WebSocketClient</code> instance and add that instance to a list. Below i'll show the upgrading part and <code>WebSocketClient</code> class</p>
  <pre><code>List&lt;WebsocketClient&gt; clients =  [];
  
  class Oauth2demoserverChannel extends ApplicationChannel {
    @override
    Future prepare() async {
      logger.onRecord.listen((rec) =&gt; print(&quot;$rec ${rec.error ?? &quot;&quot;} ${rec.stackTrace ?? &quot;&quot;}&quot;));
  
    }
    @override
    Controller get entryPoint {
      final router = Router();
  
      router
        .route(&quot;/redirection&quot;)
        .link(() =&gt; AuthorizationController());
  
      router
        .route(&quot;/connect&quot;)
        .linkFunction((request) async {
          void handleWebSocket(WebSocket webSocket){
            var newC = WebsocketClient(webSocket);
            clients.add(newC);
            print(&quot;clients map:${clients.asMap()}&quot;);
          }
          // ignore: close_sinks
          // ignore: unused_local_variable
          final socket = await WebSocketTransformer.upgrade(request.raw)
            .then(handleWebSocket);
        return null;
        });
      return router;
    }
  }
  </code></pre>
  <p>Above part is about Dart Aqueduct framework and its routing. I reserved <code>/redirection</code> end point for authorize server's redirection job. I get the data on that endpoint and as we can see at the first code part i connect myself to <code>ws://localhost:8888/connect</code> and add data.</p>
  <p>Now i'll show you about these <code>WebSocketClientClass</code>. This class takes an websocket instance at constructor and starts listening. When the message arrives; <code>messageHandler</code>takes all web socket clients in the list and broadcasts the message.</p>
  <pre><code>import 'package:oauth2demoserver/oauth2demoserver.dart';
  import 'package:oauth2demoserver/channel.dart' as ch;
  
  class WebsocketClient{
    WebsocketClient(WebSocket ws){
      _webSocket = ws;
      
  
      _webSocket.listen(messageHandler,onError: errorHandler,onDone: finishHandler);
    }
    WebSocket _webSocket;
  
    void messageHandler(dynamic data){
      for(WebsocketClient wsc in ch.clients){
        if( wsc != this){
          wsc._webSocket.add(data);
        }
  
      }
    }
  
    void errorHandler(error){
      print(&quot;err&quot;);
      _webSocket.close();
    }
  
    void finishHandler(){
      print(&quot;finished&quot;);
      _webSocket.close();
    }
  }
  </code></pre>
  <p>Now in the other hand i have an seperate Flutter application. In that application i connect to the <code>ws://localhost:8888/connect</code> and i can listen or add something and in the server <code>WebSocketClient</code> can handle message and broadcast it. But when i try to hit <code>http://localhost:8888/redirection</code> with some query parameter, freshly created <code>WebSocketClient</code> instance for that redirection doesn't enter the <code>messageHandler</code> function and it goes <code>onDone</code> .Because of these message never goes any channel. I wonder why this is happening, any idea? I'll add a picture that show reaction against get call to <code>/redirection</code> end point.
  <a href="https://i.stack.imgur.com/yyzED.png" rel="nofollow noreferrer">/redirection end point takes the query parameters end sends to web socket server for broadcating but it is not as we can see in this picture</a></p>
  <p>note: i also tried another package for connection in /redirect endpoint, with that client it was giving error code 1005, it may be helpful.</p>
  <p>edit: i also tried with javascript and python web socket server. So there is no problem with the server configurations as it seems. I guess there is a little trick about http server. Still waiting for an help, thanks.</p>