Future返回套接字数据

Future返回套接字数据

本文介绍了如何从Dart中的Future返回套接字数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

就像我将数据发送到服务器,并使用下面的代码(============行)将数据甚至是非常大的数据取回。这是我的实验,因为我需要一些中间解决方案,以便可以将Flutter应用程序连接到tcp套接字。为此,我选择auqeduct.io。今天,从加济(Gazi)的帮助下,我找到了可以在===========行之间完成我的代码的解决方案。

Likely I send data to server and get data back even very big data using code below between =========== lines. This was to my experiment for I need to some intermediate solution so I can connect my Flutter app to tcp socket. For this I choose auqeduct.io. Today help from Gazi I found the solution to complete my code between =========== lines.

因为我使用了auqeduct他们的handle函数

Because of I am using auqeduct their handle function look like this.

@override
  Future<RequestOrResponse> handle(Request request) async {

因此,我尝试修改代码并使用Future返回套接字数据作为字符串。我可以使用下面的代码在===========行之间获取数据,但是我无法使用Future _handle(String _request)异步获取任何数据。

So, I try to modify my code and use Future to return socket data as string. I can get data using code below between =========== lines, but I cannot get any data using Future _handle(String _request) async.

_socket.listen((data)打印数据无法将数据分配给字符串,因此我无法返回字符串。

_socket.listen((data) prints the data cannot assign data to string, so I cannot return string.

工作原理: print( (1)$ _secureResponse);

works: print("(1) $_secureResponse");

不起作用: _secureResponse = new String.fromCharCodes(data).trim();

not worked: _secureResponse = new String.fromCharCodes(data).trim();

如果我不使用Future并将代码放入main()内,则一切运行正常。

If I don't use Future and put the code inside main() all is working perfectly.

在测试服务器中,我发送 Hello World,并返回 Welcome Sir / Madam。

In my test server I send a "Hello World" and in return I get "Welcome Sir/Madam".

我的问题是:如何从Future中的Dart中返回套接字数据?

My question is: How to return socket data from Future in Dart?

import 'dart:async';
import 'dart:io';
import 'dart:convert';

Socket socket;
String _errorData;
String _secureResponse;

void main() async {

  var _test = await _handle("Hello World");
  print("Print _test: $_test");

}


Future<String> _handle(String _request) async {
  print("Future Starts Here");
  print("Request: $_request");
    _errorData = "Server_Error";
    if (_request != null) {
      print("Request has data");
      // =============================================================
      Socket _socket;
      await Socket.connect("192.168.22.120", 3000).then((Socket sock) {
        _socket = sock;
        _socket.listen((data) {
          print("Socket start to listen to database");
          // GET FROM SERVER *********************
          _secureResponse =  new String.fromCharCodes(data).trim();
          print("(1) $_secureResponse");
        }, onError: ((error, StackTrace trace) {
          _secureResponse = _errorData;
          print("(2) $_secureResponse");
        }), onDone: ((){
          _socket.destroy();
          exit(0);
        }), cancelOnError: false);
      }).then((_) {
        print("Socket sends data to database");
        // SENT TO SERVER ************************
        _socket.write('$_request\r\n');
      }).catchError((AsyncError e) {
        _secureResponse = _errorData;
        print("(3) $_secureResponse");
        exit(1);
      });
      // ==============================================================
    } else {
      _secureResponse = _errorData;
      print("(4) $_secureResponse");
    }
  print("(6) $_secureResponse");
  print("Future Ends Here");
  return _secureResponse;
}

结果:


  1. 未来从这里开始

  2. 请求: Hello World

  3. 请求有数据套接字将数据发送到数据库

  4. (6)空。--预期返回 欢迎先生/女士

  5. 未来在这里结束

  6. Print_test: --- 预期返回 欢迎先生/女士

  7. 套接字开始监听数据库

  8. (1) 欢迎先生/女士

  1. Future Starts Here
  2. Request: "Hello World"
  3. Request has data Socket sends data to database
  4. (6) null. --- expected return "Welcome Sir/Madam"
  5. Future Ends Here
  6. Print_test: null --- expected return "Welcome Sir/Madam"
  7. Socket start to listen to database
  8. (1) "Welcome Sir/Madam"


推荐答案

这可能有用。我想您应该阅读有关流/期货/异步/等待的内容。

This might work. I think you should read up about streams/futures/async/await, though.

Future<String> _handle(String _request) async {
  print("Future Starts Here");
  print("Request: $_request");
    _errorData = "Server_Error";
    if (_request != null) {
      print("Request has data");
      // =============================================================
      Socket _socket;
      await Socket.connect("192.168.22.120", 3000).then((Socket sock) {
        _socket = sock;
      }).then((_) {
        print("Socket sends data to database");
        // SENT TO SERVER ************************
        _socket.write('$_request\r\n');
        return _socket.first;
      }).then((data) {
        print("Socket start to listen to database");
        // GET FROM SERVER *********************
        _secureResponse =  new String.fromCharCodes(data).trim();
        print("(1) $_secureResponse");
      }).catchError((AsyncError e) {
        _secureResponse = _errorData;
        print("(3) $_secureResponse");
        exit(1);
      });
      // ==============================================================
    } else {
      _secureResponse = _errorData;
      print("(4) $_secureResponse");
    }
  print("(6) $_secureResponse");
  print("Future Ends Here");
  return _secureResponse;
}

这篇关于如何从Dart中的Future返回套接字数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:56