11import 'package:flutter/material.dart' ;
22
33import 'ezyclient/ezy_constants.dart' ;
4+ import 'ezyclient/ezy_entities.dart' ;
45import 'ezyclient/ezy_handlers.dart' ;
56import 'ezyclient/ezy_clients.dart' ;
67import 'ezyclient/ezy_client.dart' ;
78import 'ezyclient/ezy_config.dart' ;
89
10+ const ZONE_NAME = "example" ;
11+ const APP_NAME = "hello-world" ;
12+
913void main () {
1014 runApp (const MyApp ());
1115}
@@ -64,10 +68,15 @@ class _MyHomePageState extends State<MyHomePage> {
6468 EzyClients clients = EzyClients .getInstance ();
6569 EzyClient client = clients.newDefaultClient (config);
6670 client.setup.addDataHandler (EzyCommand .HANDSHAKE , _SocketHandshakeHandler ());
71+ client.setup.addDataHandler (EzyCommand .LOGIN , _SocketLoginSuccessHandler ());
72+ client.setup.addDataHandler (EzyCommand .APP_ACCESS , _SocketAppAccessHandler ());
73+ var appSetup = client.setup.setupApp (APP_NAME );
74+ appSetup.addDataHandler ("greet" , _SocketGreetResponseHandler ((message) {
75+ setState (() {
76+ socketState = message;
77+ });
78+ }));
6779 client.connect ("tvd12.com" , 3005 );
68- setState (() {
69- socketState = "Socket connected" ;
70- });
7180 }
7281
7382 void _incrementCounter () {
@@ -125,11 +134,11 @@ class _MyHomePageState extends State<MyHomePage> {
125134 style: Theme .of (context).textTheme.headline4,
126135 ),
127136 const Text (
128- 'Socket state : ' ,
137+ 'Socket message : ' ,
129138 ),
130139 Text (
131140 '$socketState ' ,
132- style: Theme .of (context).textTheme.headline4 ,
141+ style: Theme .of (context).textTheme.headline6 ,
133142 )
134143 ],
135144 ),
@@ -154,3 +163,31 @@ class _SocketHandshakeHandler extends EzyHandshakeHandler {
154163 return request;
155164 }
156165}
166+
167+ class _SocketLoginSuccessHandler extends EzyLoginSuccessHandler {
168+ @override
169+ void handleLoginSuccess (responseData) {
170+ client.send (EzyCommand .APP_ACCESS , [APP_NAME ]);
171+ }
172+ }
173+
174+ class _SocketAppAccessHandler extends EzyAppAccessHandler {
175+ @override
176+ void postHandle (EzyApp app, List data) {
177+ app.send ("greet" , {"who" : "Flutter's developer" });
178+ }
179+ }
180+
181+ class _SocketGreetResponseHandler extends EzyAppDataHandler <Map > {
182+
183+ late Function (String ) _callback;
184+
185+ _SocketGreetResponseHandler (Function (String ) callback) {
186+ _callback = callback;
187+ }
188+
189+ @override
190+ void handle (EzyApp app, Map data) {
191+ _callback (data["message" ]);
192+ }
193+ }
0 commit comments