Skip to content

Commit 2bbf29e

Browse files
authored
Merge pull request #9 from youngmonkeys/dev
update example
2 parents 402e843 + a119f12 commit 2bbf29e

File tree

2 files changed

+179
-177
lines changed

2 files changed

+179
-177
lines changed

example/lib/main.dart

Lines changed: 23 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import 'package:flutter/material.dart';
2-
3-
import 'package:ezyfox_server_flutter_client/ezy_constants.dart';
4-
import 'package:ezyfox_server_flutter_client/ezy_entities.dart';
5-
import 'package:ezyfox_server_flutter_client/ezy_handlers.dart';
6-
import 'package:ezyfox_server_flutter_client/ezy_clients.dart';
7-
import 'package:ezyfox_server_flutter_client/ezy_client.dart';
8-
import 'package:ezyfox_server_flutter_client/ezy_config.dart';
9-
10-
const ZONE_NAME = "example";
11-
const APP_NAME = "hello-world";
2+
import 'socket_proxy.dart';
123

134
void main() {
145
runApp(const MyApp());
@@ -23,15 +14,6 @@ class MyApp extends StatelessWidget {
2314
return MaterialApp(
2415
title: 'Flutter Demo',
2516
theme: ThemeData(
26-
// This is the theme of your application.
27-
//
28-
// Try running your application with "flutter run". You'll see the
29-
// application has a blue toolbar. Then, without quitting the app, try
30-
// changing the primarySwatch below to Colors.green and then invoke
31-
// "hot reload" (press "r" in the console where you ran "flutter run",
32-
// or simply save your changes to "hot reload" in a Flutter IDE).
33-
// Notice that the counter didn't reset back to zero; the application
34-
// is not restarted.
3517
primarySwatch: Colors.blue,
3618
),
3719
home: const MyHomePage(title: 'Flutter Demo Home Page'),
@@ -42,15 +24,6 @@ class MyApp extends StatelessWidget {
4224
class MyHomePage extends StatefulWidget {
4325
const MyHomePage({Key? key, required this.title}) : super(key: key);
4426

45-
// This widget is the home page of your application. It is stateful, meaning
46-
// that it has a State object (defined below) that contains fields that affect
47-
// how it looks.
48-
49-
// This class is the configuration for the state. It holds the values (in this
50-
// case the title) provided by the parent (in this case the App widget) and
51-
// used by the build method of the State. Fields in a Widget subclass are
52-
// always marked "final".
53-
5427
final String title;
5528

5629
@override
@@ -63,94 +36,48 @@ class _MyHomePageState extends State<MyHomePage> {
6336
String socketState = 'Socket has not connected yet';
6437
String sslMessage = '';
6538

66-
Future<void> connectToServer() async {
67-
EzyConfig config = EzyConfig();
68-
config.clientName = "hello";
69-
config.enableSSL = true;
70-
config.enableDebug = true;
71-
EzyClients clients = EzyClients.getInstance();
72-
EzyClient client = clients.newDefaultClient(config);
73-
client.setup.addEventHandler(
74-
EzyEventType.DISCONNECTION,
75-
_SocketDisconnectionHandler(() => {
76-
setState(() {
77-
socketState = "Disconnected, retry ...";
78-
sslMessage = "";
79-
})
80-
})
81-
);
82-
client.setup.addEventHandler(
83-
EzyEventType.CONNECTION_FAILURE,
84-
_SocketConnectionFailureHandler(() => {
85-
setState(() {
86-
socketState = "Can not connect to server";
87-
sslMessage = "";
88-
})
89-
})
90-
);
91-
client.setup.addDataHandler(EzyCommand.HANDSHAKE, _SocketHandshakeHandler());
92-
client.setup.addDataHandler(EzyCommand.LOGIN, _SocketLoginSuccessHandler());
93-
client.setup.addDataHandler(EzyCommand.APP_ACCESS, _SocketAppAccessHandler());
94-
var appSetup = client.setup.setupApp(APP_NAME);
95-
appSetup.addDataHandler("greet", _SocketGreetResponseHandler((message) {
39+
Future<void> setup() async {
40+
SocketProxy socketProxy = SocketProxy.getInstance();
41+
socketProxy.onDisconnected(() => {
42+
setState(() {
43+
socketState = "Disconnected, retry ...";
44+
sslMessage = "";
45+
})
46+
});
47+
socketProxy.onConnectionFailed(() => {
48+
setState(() {
49+
socketState = "Can not connect to server";
50+
sslMessage = "";
51+
})
52+
});
53+
socketProxy.onGreet((message) => {
9654
setState(() {
9755
socketState = message;
98-
});
99-
}));
100-
appSetup.addDataHandler("secureChat", _SocketSecureChatResponseHandler((message) {
56+
})
57+
});
58+
socketProxy.onSecureChat((message) => {
10159
setState(() {
10260
sslMessage = message;
103-
});
104-
}));
105-
// client.connect("tvd12.com", 3005);
106-
client.connect("127.0.0.1", 3005);
61+
})
62+
});
10763
}
10864

10965
void _incrementCounter() {
11066
setState(() {
111-
// This call to setState tells the Flutter framework that something has
112-
// changed in this State, which causes it to rerun the build method below
113-
// so that the display can reflect the updated values. If we changed
114-
// _counter without calling setState(), then the build method would not be
115-
// called again, and so nothing would appear to happen.
11667
_counter++;
11768
});
118-
119-
connectToServer();
69+
SocketProxy.getInstance().connectToServer("flutter", "123456");
12070
}
12171

12272
@override
12373
Widget build(BuildContext context) {
124-
// This method is rerun every time setState is called, for instance as done
125-
// by the _incrementCounter method above.
126-
//
127-
// The Flutter framework has been optimized to make rerunning build methods
128-
// fast, so that you can just rebuild anything that needs updating rather
129-
// than having to individually change instances of widgets.
74+
setup();
13075
return Scaffold(
13176
appBar: AppBar(
132-
// Here we take the value from the MyHomePage object that was created by
133-
// the App.build method, and use it to set our appbar title.
13477
title: Text(widget.title),
13578
),
13679
body: Center(
137-
// Center is a layout widget. It takes a single child and positions it
138-
// in the middle of the parent.
13980
child: Column(
140-
// Column is also a layout widget. It takes a list of children and
141-
// arranges them vertically. By default, it sizes itself to fit its
142-
// children horizontally, and tries to be as tall as its parent.
143-
//
144-
// Invoke "debug painting" (press "p" in the console, choose the
145-
// "Toggle Debug Paint" action from the Flutter Inspector in Android
146-
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
147-
// to see the wireframe for each widget.
148-
//
149-
// Column has various properties to control how it sizes itself and
150-
// how it positions its children. Here we use mainAxisAlignment to
151-
// center the children vertically; the main axis here is the vertical
152-
// axis because Columns are vertical (the cross axis would be
153-
// horizontal).
15481
mainAxisAlignment: MainAxisAlignment.center,
15582
children: <Widget>[
15683
const Text(
@@ -185,84 +112,3 @@ class _MyHomePageState extends State<MyHomePage> {
185112
);
186113
}
187114
}
188-
189-
class _SocketHandshakeHandler extends EzyHandshakeHandler {
190-
@override
191-
List getLoginRequest() {
192-
var request = [];
193-
request.add("example");
194-
request.add("username");
195-
request.add("password");
196-
request.add([]);
197-
return request;
198-
}
199-
}
200-
201-
class _SocketLoginSuccessHandler extends EzyLoginSuccessHandler {
202-
@override
203-
void handleLoginSuccess(responseData) {
204-
client.send(EzyCommand.APP_ACCESS, [APP_NAME]);
205-
}
206-
}
207-
208-
class _SocketAppAccessHandler extends EzyAppAccessHandler {
209-
@override
210-
void postHandle(EzyApp app, List data) {
211-
app.send("greet", {"who": "Flutter's developer"});
212-
}
213-
}
214-
215-
class _SocketGreetResponseHandler extends EzyAppDataHandler<Map> {
216-
217-
late Function(String) _callback;
218-
219-
_SocketGreetResponseHandler(Function(String) callback) {
220-
_callback = callback;
221-
}
222-
223-
@override
224-
void handle(EzyApp app, Map data) {
225-
_callback(data["message"]);
226-
app.send("secureChat", {"who": "Young Monkey"}, true);
227-
}
228-
}
229-
230-
class _SocketSecureChatResponseHandler extends EzyAppDataHandler<Map> {
231-
232-
late Function(String) _callback;
233-
234-
_SocketSecureChatResponseHandler(Function(String) callback) {
235-
_callback = callback;
236-
}
237-
238-
@override
239-
void handle(EzyApp app, Map data) {
240-
_callback(data["secure-message"]);
241-
}
242-
}
243-
244-
class _SocketConnectionFailureHandler extends EzyConnectionFailureHandler {
245-
late Function _callback;
246-
247-
_SocketConnectionFailureHandler(Function callback) {
248-
_callback = callback;
249-
}
250-
251-
@override
252-
void onConnectionFailed(Map event) {
253-
_callback();
254-
}
255-
}
256-
257-
258-
class _SocketDisconnectionHandler extends EzyDisconnectionHandler {
259-
late Function _callback;
260-
261-
_SocketDisconnectionHandler(Function callback) {
262-
_callback = callback;
263-
}
264-
@override
265-
void postHandle(Map event) {
266-
_callback();
267-
}
268-
}

0 commit comments

Comments
 (0)