/
main.dart
224 lines (214 loc) · 7.51 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:npusher/npusher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String connectionState;
NEvent event;
NChannel channel;
String get info => '''
Connection State: $connectionState
Last Event Channel: ${event?.channel}
Last Event Name: ${event?.event}
Last Event Data: ${event?.data}
''';
final TextEditingController channelController =
TextEditingController(text: "channel-name");
final TextEditingController eventController =
TextEditingController(text: "event-name");
final TextEditingController triggerController =
TextEditingController(text: "client-trigger");
final NPusher _pusher = NPusher();
@override
void initState() {
super.initState();
initPusher();
}
Future<void> initPusher() async {
try {
await _pusher.init(
appKey: 'nhancv',
authUrl: 'https://nhancv.com/api/mobile/broadcasting/auth',
headers: <String, String>{
'Authorization': 'Bearer nhancdeptrai',
'Content-Type': 'application/json',
'Accept': 'application/json'
});
} on PlatformException catch (e) {
print('initPusher: ${e.message}');
}
// _pusher.connect((previousState, currentState) async {
// if (currentState.toLowerCase() == 'connected') {
// final NChannel channel = await _pusher.subscribe('event');
// await _pusher.bindEchoPublic(channel, 'OrderCreated', (NEvent event) {
// print('event: $event');
// });
// await _pusher.echoPresencePeriodicStart('event-presence',
// onEventHere: (NEvent event) {
// print('onEventHere: $event');
// });
// }
// }, (message, code, exception) {
// print('error: $message');
// });
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('NPusher'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(info ?? ''),
RaisedButton(
child: Text("Connect"),
onPressed: () {
_pusher.connect((previousState, currentState) {
if (mounted)
setState(() {
connectionState = currentState;
});
}, (message, code, exception) {
debugPrint("Error: $message");
});
},
),
RaisedButton(
child: Text("Disconnect"),
onPressed: () async {
await _pusher.disconnect();
setState(() {
connectionState = null;
event = null;
channel = null;
});
},
),
Row(
children: <Widget>[
Container(
width: 200,
child: TextField(
autocorrect: false,
controller: channelController,
decoration: InputDecoration(hintText: "Channel"),
),
),
RaisedButton(
child: Text("Subscribe"),
onPressed: () async {
channel =
await _pusher.subscribe(channelController.text);
print('Subscribe');
},
)
],
),
Row(
children: <Widget>[
Container(
width: 200,
child: TextField(
controller: channelController,
decoration: InputDecoration(hintText: "Channel"),
),
),
RaisedButton(
child: Text("Unsubscribe"),
onPressed: () async {
await _pusher.unsubscribe(channelController.text);
channel = null;
print('Unsubscribe');
},
)
],
),
Row(
children: <Widget>[
Container(
width: 200,
child: TextField(
controller: eventController,
decoration: InputDecoration(hintText: "Event"),
),
),
RaisedButton(
child: Text("Bind"),
onPressed: () async {
await channel.bind(eventController.text,
(NEvent _event) {
if (mounted)
setState(() {
event = _event;
});
});
print('Bind');
},
)
],
),
Row(
children: <Widget>[
Container(
width: 200,
child: TextField(
controller: eventController,
decoration: InputDecoration(hintText: "Event"),
),
),
RaisedButton(
child: Text("Unbind"),
onPressed: () async {
await channel.unbind(eventController.text);
if (mounted)
setState(() {
event = null;
});
print('Unbind');
},
)
],
),
Row(
children: <Widget>[
Container(
width: 200,
child: TextField(
controller: triggerController,
decoration: InputDecoration(hintText: "Trigger"),
),
),
RaisedButton(
child: Text("Trigger"),
onPressed: () async {
await channel.trigger(triggerController.text,
data:
'{"testValue": 123, "anotherOne": false, "nested": {"w0t": "m8"}}');
},
)
],
),
],
)),
),
),
);
}
}