I can connect my aqueduct to close network socket (listener and get the data) Problem is I can't return new Response.ok. It shows null...
I am using Aqueduct 3.0. The most of the documentation looks like Snippets. I had difficulty to apply dart socket. But now I can get text from socket and I cannot sent the data over internet from my aqueduct web api.
class LoginController extends Controller {
String _xCustomerToken;
String _xCustomerName;
var list = [];
@override
Future handle(Request request) async {
String reply;
Socket.connect('192.168.1.22’, 1024).then((socket) async {
socket.listen((data) async {
reply = await new String.fromCharCodes(data).trim();
print("reply: $reply");
var list = reply.split(":_:");
print(list);
_xCustomerToken = list[2];
_xCustomerName = list[3];
// CAN PRINT THE SOCKET DATA
// EXAMPLE: ”Customer Token: 998877, CustomerName: NIYAZI TOROS”
print(
"Customer Token: $_xCustomerToken, CustomerName: $_xCustomerName");
await new Future.delayed(new Duration(seconds: 2));
}, onDone: () {
print("Done");
});
socket.write('Q101:_:49785:_:x\r\n');
});
// CANT RETURN THE SOCKET DATA
return new Response.ok(
// EXAMPLE: "Customer Token: null, CustomerName: null”
"Customer Token: $_xCustomerToken, CustomerName: $_xCustomerName");
}
}
Update: I put return statement inside await for (listen) and now it I type http://192.168.1.22:8888/login I get the information correctly.
class LoginController extends Controller {
String _xCustomerToken;
String _xCustomerName;
String _xResult;
var list = [];
@override
Future<RequestOrResponse> handle(Request request) async {
String reply;
var socket = await Socket.connect('192.168.1.22', 1024);
socket.write('Q101:_:49785:_:x\r\n');
await for (var data in socket) {
reply = await new String.fromCharCodes(data).trim();
var list = reply.split(":_:");
_xCustomerToken = list[2];
_xCustomerName = list[3];
_xResult = "$_xCustomerToken:_:$_xCustomerName";
print("$_xResult");
return new Response.ok("$_xResult");
}
return new Response.ok("No data");
}
}