Dart How to code a simple web-socket echo app

后端 未结 1 983
夕颜
夕颜 2020-12-31 23:09

I\'ve been attempting to learn enough html, css, and Dart to create my first web page and all is going well, except that I do not understand how to

相关标签:
1条回答
  • 2020-12-31 23:38

    Here is a simple websocket client/server echo example. Messages doesn't show in browser window, but they are printed in console window. You have to start server.dart and main.dart separately. Both processes print messages to their own console window. Edit: I added an output div for displaying the message also in browser.

    bin\ws_server.dart:

    import "dart:convert";
    import 'dart:io';
    import 'package:route/server.dart' show Router;
    
    
    
    void handleWebSocket(WebSocket webSocket) {
      // Listen for incoming data. We expect the data to be a JSON-encoded String.
      webSocket
        .map((string)=> JSON.decode(string))
        .listen((json) {
          // The JSON object should contains a 'echo' entry.
          var echo = json['echo'];
          print("Message to be echoed: $echo");
          var response='{"response": "$echo"}';
          webSocket.add(response);
        }, onError: (error) {
          print('Bad WebSocket request');
        });
    }
    
    
    void main() {
      int port = 9223;
    
      HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) {
        print("Search server is running on "
                 "'http://${server.address.address}:$port/'");
        var router = new Router(server);
        // The client will connect using a WebSocket. Upgrade requests to '/ws' and
        // forward them to 'handleWebSocket'.
        router.serve('/ws')
          .transform(new WebSocketTransformer())
          .listen(handleWebSocket);
      });
    }
    

    web\index.html:

    <!DOCTYPE html>
    
    <html>
      <head>
        <meta charset="utf-8">
        <title>Websocket echo</title>
      </head>
      <body>
        <p>Websocket test</p>
        <div id="output"></div>
    
        <script type="application/dart" src="main.dart"></script>
      </body>
    </html>
    

    web\main.dart:

    library main;
    
    import 'dart:async';
    import 'dart:convert';
    import 'dart:html';
    
    class WebsocketService {
    
      WebSocket webSocket;
    
      WebsocketService() {
        connect();
      }
    
      void connect() {
        webSocket = new WebSocket('ws://127.0.0.1:9223/ws');
        webSocket.onOpen.first.then((_) {
          onConnected();
          sendws("Hello websocket server");
          webSocket.onClose.first.then((_) {
            print("Connection disconnected to ${webSocket.url}");
            onDisconnected();
          });
        });
        webSocket.onError.first.then((_) {
          print("Failed to connect to ${webSocket.url}. "
                "Please run bin/server.dart and try again.");
          onDisconnected();
        });
      }
    
      void onConnected() {
        webSocket.onMessage.listen((e) {
          onMessage(e.data);
        });
      }
    
      void onDisconnected() {
        print("Disconnected, trying again in 3s");
        new Timer(new Duration(seconds:3), (){
          connect();
        });
      }
    
      void onMessage(data) {
        var json = JSON.decode(data);
        var echoFromServer = json['response'];
        print("Received message: $echoFromServer");
        var output=querySelector('#output');
        output.text="Received message: $echoFromServer";
    
        new Timer(new Duration(seconds:3), (){ //Send a new message to server after 3s
          String now = new DateTime.now().toString();
          sendws("Time: $now");
        });
    
      }
    
      void sendws(String msg){
        var request = '{"echo": "$msg"}';
        print("Send message to server: $request");
        webSocket.send(request);
      }
    }
    
    void main() {
      WebsocketService ws=new WebsocketService();
    }
    
    0 讨论(0)
提交回复
热议问题