-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMain.mxml
347 lines (212 loc) · 8.45 KB
/
Main.mxml
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;
private var nc:NetConnection;
[Bindable]
private var connected:Boolean;
[Bindable]
private var connectionURL:String = "rtmp://localhost:1935/connection-messaging-demo";
private function printToConsole(msg:String):void
{
txtConsole.text += msg;
txtConsole.text += "\n";
}
private function init(event:Event):void
{
btnConnect.addEventListener(MouseEvent.CLICK, onConnect);
btnClientToServerTest.addEventListener(MouseEvent.CLICK, onClientToServerCall);
btnClientToServerTestWithParams.addEventListener(MouseEvent.CLICK, onClientToServerCallWithParams);
btnRequestServerForClientCall1.addEventListener(MouseEvent.CLICK, onRequestServerForClientCall);
btnRequestServerForClientCall2.addEventListener(MouseEvent.CLICK, onRequestServerForClientCallWithParams);
btnRequestServerForClientCall3.addEventListener(MouseEvent.CLICK, onRequestServerForClientCallWithParamsForResult);
}
/**
* Connect to server
* @param e
*/
private function onConnect(e:Event):void
{
if (nc == null)
{
printToConsole("Connecting to server :" + txtConnectionUrl.text);
nc = new NetConnection();
nc.client = this;
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect(String(txtConnectionUrl.text));
}
}
/**
* Handling NetStatus events
*
* @param e
*/
private function onNetStatus(e:NetStatusEvent):void
{
printToConsole("onNetStatus status :" + e.info.code);
switch(e.info.code)
{
case "NetConnection.Connect.Success":
connected = true;
break;
case "NetConnection.Connect.Failed":
case "NetConnection.Connect.Rejected":
case "NetConnection.Connect.Closed":
connected = false;
if (nc != null){
nc.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
}
nc = null;
break;
}
}
/**
* Make a call to server
* @param e
*/
private function onClientToServerCall(e:Event):void
{
if (nc != null)
{
printToConsole("Calling method 'clientToServer' on server :" + txtConnectionUrl.text);
nc.call("clientServices.clientToServer", new Responder(function(result:*):void{
printToConsole("Client to server call successful");
}, function(err:Object):void{
printToConsole("Client to server call failed ");
}));
}
}
private function onClientToServerCallWithParams(e:Event):void
{
if (nc != null)
{
printToConsole("Calling method 'clientToServerWithParams' on server :" + txtConnectionUrl.text);
nc.call("clientServices.clientToServerWithParams", new Responder(function(result:*):void{
printToConsole("Client to server call successful");
}, function(err:Object):void{
printToConsole("Client to server call failed ");
}), 1, 2);
}
}
private function onRequestServerForClientCall(e:Event):void
{
if (nc != null)
{
printToConsole("Requesting server to call method 'clientMethod1' on client");
nc.call("clientServices.callClientMethodWithoutParams", new Responder(function(result:*):void{
printToConsole("Request to server successful");
}, function(err:Object):void{
printToConsole("Request to server failed");
}));
}
}
private function onRequestServerForClientCallWithParams(e:Event):void
{
if (nc != null)
{
printToConsole("Requesting server to call method 'clientMethod2' on client");
nc.call("clientServices.callClientMethodWithParams", new Responder(function(result:*):void{
printToConsole("Request to server successful");
}, function(err:Object):void{
printToConsole("Request to server failed");
}));
}
}
private function onRequestServerForClientCallWithParamsForResult(e:Event):void
{
if (nc != null)
{
printToConsole("Requesting server to call method 'clientMethod3' on client");
nc.call("clientServices.callClientMethodWithParamsForResult", new Responder(function(result:*):void{
printToConsole("Request to server successful");
}, function(err:Object):void{
printToConsole("Request to server failed");
}));
}
}
/* Client methods called by server */
/**
* Method without parameters
*/
public function clientMethod1():void
{
printToConsole("Method 'clientMethod1' called from server");
}
/**
* * Method with parameters
* @param ...args
*/
public function clientMethod2(...args):void
{
printToConsole("Method 'clientMethod2' called from server with arguments " + args.toString());
}
/**
* Method with parameters that returns result
*
* @param ...args
* @return
*/
public function clientMethod3(...args):Object
{
printToConsole("Method 'clientMethod3' called from server with arguments for result " + args.toString());
if (args.length > 1){
var result:Object = args[0] + args[1];
printToConsole("Sending result back to server " + result.toString());
return result;
}
return null;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" />
</s:layout>
<s:HGroup width="100%" height="100%" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10">
<s:VGroup width="100%" height="100%" gap="10">
<s:HGroup width="100%" enabled="{!connected}">
<s:TextInput id="txtConnectionUrl" width="100%" height="40" fontSize="16" text="{connectionURL}" />
<s:Button id="btnConnect" width="200" height="40" label="{(!connected)?'CONNECT':'CONNECTED'}" fontSize="14" fontWeight="bold"/>
</s:HGroup>
<s:Spacer width="10" height="50"/>
<s:HGroup width="100%" enabled="{connected}">
<s:Label width="100%" height="40" fontSize="18" text="Client 2 Server"
verticalAlign="middle"/>
<s:Button id="btnClientToServerTest" width="200" height="40" label="INVOKE" fontSize="14" fontWeight="bold" />
</s:HGroup>
<s:HGroup width="100%" enabled="{connected}">
<s:Label width="100%" height="40" fontSize="18" text="Client 2 Server"
verticalAlign="middle"/>
<s:Button id="btnClientToServerTestWithParams" width="200" height="40" label="INVOKE WITH PARAMS" fontSize="14" fontWeight="bold" />
</s:HGroup>
<s:HGroup width="100%" enabled="{connected}">
<s:Label width="100%" height="40" fontSize="18" text="Server 2 Client Without Params"
verticalAlign="middle"/>
<s:Button id="btnRequestServerForClientCall1" width="200" height="40" label="REQUEST" fontSize="14" fontWeight="bold" />
</s:HGroup>
<s:HGroup width="100%" enabled="{connected}">
<s:Label width="100%" height="40" fontSize="18" text="Server 2 Client With Params"
verticalAlign="middle"/>
<s:Button id="btnRequestServerForClientCall2" width="200" height="40" label="REQUEST" fontSize="14" fontWeight="bold" />
</s:HGroup>
<s:HGroup width="100%" enabled="{connected}">
<s:Label width="100%" height="40" fontSize="18" text="Server 2 Client With Params For Result"
verticalAlign="middle"/>
<s:Button id="btnRequestServerForClientCall3" width="200" height="40" label="REQUEST" fontSize="14" fontWeight="bold" />
</s:HGroup>
</s:VGroup>
</s:HGroup>
<s:VGroup width="100%">
<s:Label fontSize="14" fontWeight="bold" text="CONSOLE:"/>
<s:TextArea id="txtConsole" width="100%" height="100%" editable="false"/>
</s:VGroup>
</s:Application>