我的颤振应用程序通过 https 连接到套接字,我正在使用渡槽获取安全数据。套接字数据是全长字符串,例如;
2_7#a_b_c_d_e_f_g#h_i_j_k_l_m_n#
我将数据转换为 json,我的 json 数据如下所示:“{data:2_7#a_b_c_d_e_f_g#h_i_j_k_l_m_n#}”
并发送到颤振应用程序。2_7# 表示我有 2 行和 7 列。原始服务器套接字数据 152_7# 这意味着我有 152 行 7 列。
当我尝试使用渡槽中的套接字获取此数据(152_7#)时,我只得到 12 行或有时 25 行。
如果服务器数据很短,我会得到所有这些,但可以获得大字符串数据。
我的问题是如何使用渡槽和套接字获取完整数据?
import 'package:aqueduct/aqueduct.dart';
import 'package:ntmsapi/ntmsapi.dart';
Socket socket;
String _reply;
var _secureResponse;
var _errorData;
class NtmsApiController extends Controller {
@override
Future<RequestOrResponse> handle(Request request) async {
try {
String _xRequestValue = "";
_reply = "";
_errorData = "Server_Error";
if (request.path.remainingPath != null) {
_xRequestValue = request.path.remainingPath;
// returns a string like 152 row with 11 column
socket = await Socket.connect('192.168.xxx.xxx’, 8080);
socket.write('$_xRequestValue\r\n');
socket.handleError((data) {
_secureResponse = "$_errorData";
});
await for (var data in socket) {
// _cant get all the data from reply, reply shows about 25 row data
_reply = new String.fromCharCodes(data).trim();
_secureResponse = "$_reply";
socket.close();
socket.destroy();
}
} else {
_secureResponse = "$_errorData";
}
} catch (e) {
_secureResponse = "$_errorData";
}
return new Response.ok("$_secureResponse")
..contentType = ContentType.json;
}
}