Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Block mutiple open socket
Browse files Browse the repository at this point in the history
  • Loading branch information
JAICHANG.PARK committed May 21, 2020
1 parent 41014fe commit 86b6eb5
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
var _deviceAddress = "";
var _isConnected = false;
var _isHost = false;
var _isOpen = false;


P2pSocket _socket;
List<WifiP2pDevice> devices = [];
Expand Down Expand Up @@ -101,25 +103,27 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
}

void _openPortAndAccept(int port) async {
var socket = await FlutterP2p.openHostPort(port);
setState(() {
_socket = socket;
});
if(!_isOpen){
var socket = await FlutterP2p.openHostPort(port);
setState(() {
_socket = socket;
});

var buffer = "";
socket.inputStream.listen((data) {
var msg = String.fromCharCodes(data.data);
buffer += msg;
if (data.dataAvailable == 0) {
snackBar("Data Received from ${_isHost ? "Client" : "Host"}: $buffer");
socket.writeString("Successfully received: $buffer");
buffer = "";
}
});
var buffer = "";
socket.inputStream.listen((data) {
var msg = String.fromCharCodes(data.data);
buffer += msg;
if (data.dataAvailable == 0) {
snackBar("Data Received from ${_isHost ? "Client" : "Host"}: $buffer");
socket.writeString("Successfully received: $buffer");
buffer = "";
}
});

print("_openPort done");
await FlutterP2p.acceptPort(port);
print("_accept done");
print("_openPort done");
_isOpen = await FlutterP2p.acceptPort(port);
print("_accept done: $_isOpen");
}
}

_connectToPort(int port) async {
Expand Down Expand Up @@ -149,7 +153,13 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
return true;
}


Future<bool> _disconnect() async{
bool result = await FlutterP2p.removeGroup();
_socket = null;
if(result) _isOpen = false;
return result;
}

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand Down Expand Up @@ -177,6 +187,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
title: Text("Discover Devices"),
onTap: () async {
if (!_isConnected) await FlutterP2p.discoverDevices();
else return;
},
),
Divider(),
Expand All @@ -199,7 +210,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
Divider(),
ListTile(
title: Text("Disconnect"),
onTap: _isConnected ? () async => await FlutterP2p.removeGroup() : null,
onTap: _isConnected ? () async => await _disconnect() : null,
),
Divider(),
Padding(
Expand Down

0 comments on commit 86b6eb5

Please sign in to comment.