Best method for handling client/server communication? IOWebSocketChannel seems broken.

549 views
Skip to first unread message

Ross Cooper-Smith

unread,
Jan 3, 2020, 5:42:20 PM1/3/20
to Flutter Development (flutter-dev)
Hi all,

Looking for some advice on whether there's a better way to handle client/server communication as my current approach feels very messy.

In essence, I need a method to open a connection from my Flutter app to a device over TCP/IP connection, and pass simple commands and data strings back and forth.  

My first basic testing as I built my GUI just used http.  That works reliably, but there's a lot of unnecessary overhead there when you just want to pass simple commands, and now the GUI side is built I've started the work to implement WebSockets for the communication, using the package IOWebSocketChannel, as described in the Flutter Cookbook: https://flutter.dev/docs/cookbook/networking/web-sockets

Basic functional tests worked ok, but I've hit a stumbling block in that it seems that error handling is lacking or non-existent with this package.  Specifically if the remote webSocket server is offline for any reason, IOWebSocketChannel.connect silently fails.  It doesn't throw an exception as documented, still returns a socket, and there doesn't appear to be any way to detect whether you actually have a working connection or not.

I can see this has been reported a few times in the past, but so far can't find any workaround:
For now I'm using http to test that the remote system is available, and switching to WebSockets afterwards, trusting that the other error handling within this package works.

Does anybody have a better idea?


Wojciech S. Czarnecki

unread,
Jan 5, 2020, 7:50:41 AM1/5/20
to flutt...@googlegroups.com, Ross Cooper-Smith
On Fri, 3 Jan 2020 14:42:20 -0800 (PST)
"'Ross Cooper-Smith' via Flutter Development (flutter-dev)" <flutt...@googlegroups.com> wrote:

> In essence, I need a method to open a connection
> from my Flutter app to a device over TCP/IP connection

https://pub.dev/packages/grpc
https://medium.com/flutter-community/flutter-grpc-810f87612c6d

> Does anybody have a better idea?

https://pub.dev/packages/flat_buffers
https://google.github.io/flatbuffers/flatbuffers_guide_use_dart.html

hope this helps,

--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE

Ross Cooper-Smith

unread,
Jan 5, 2020, 8:26:26 AM1/5/20
to Flutter Development (flutter-dev)
Thanks, I'll take a look at those.

I should have posted back that I found a workaround from another post.  This is working now and seems to be handling everything I throw at it:

import 'dart:io';


Future<IOWebSocketChannel> ConnectWebSocket(String _ipAddr) async {
try {
print('Opening webSocket channel to ' + _ipAddr);
//Should throw an exception if it can't connect, but doesn't handle all situations and silently fails if IP not available
//final channel = IOWebSocketChannel.connect('ws://' + controllerIP);

//Opening a socket first using the WebSocket library seems more reliable
final _socket = await WebSocket.connect('ws://' + _ipAddr+ ':81').timeout(Duration(seconds: 5));
return IOWebSocketChannel(_socket );
}
catch (e) {
print('Error opening websocket channel to ' + _ipAddr);
print('Error is: ' + e.toString());
return null;
}
}

Reply all
Reply to author
Forward
0 new messages