How to return Response.ok with socket data?


How to return Response.ok with socket data?



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:_:xrn');
});

// 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:_:xrn');

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");
}
}




1 Answer
1



The .listen() will not wait for completion to execute the rest of the code then you are hitting return new Response.ok("Customer Token: $_xCustomerToken, CustomerName: $_xCustomerName"); right bellow with null values. You can try using await for instead:


.listen()


return new Response.ok("Customer Token: $_xCustomerToken, CustomerName: $_xCustomerName");


class LoginController extends Controller {
String _xCustomerToken;
String _xCustomerName;
var list = ;

@override
Future handle(Request request) async {
String reply;

var socket = await Socket.connect('192.168.1.22’, 1024);
socket.write('Q101:_:49785:_:xrn');

await for (var data in socket) {
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));
}

return new Response.ok("Customer Token: $_xCustomerToken, CustomerName: $_xCustomerName");
}
}





Thanks Leo, I modify and update my code. If I put return statement inside await for its working.
– Niyazi Toros
Jul 3 at 5:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy