-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmyWebService.as
More file actions
356 lines (277 loc) · 8.66 KB
/
myWebService.as
File metadata and controls
356 lines (277 loc) · 8.66 KB
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
348
349
350
351
352
353
354
355
356
package webService
{
import flash.display.Stage;
import flash.events.TimerEvent;
import flash.utils.ByteArray;
import flash.utils.Timer;
import mx.rpc.AbstractOperation;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.LoadEvent;
import mx.rpc.soap.SOAPHeader;
import mx.rpc.soap.WebService;
public class myWebService
{
//private static const GAMEID:String = "Console";
public static var DEBUG_DONOT_CONNECT:Boolean = false ;
/**these variables are the variables that will help to send requests after ws is connects*/
private static var operetionsList:Vector.<AbstractOperation>,
operationsVariables:Vector.<Array>;
public static var eventListen:WebEventDispatcher = new WebEventDispatcher();
/**Two new list to manage list of functions that have to call after each event acts*/
public static var onConnectFunctionList:Vector.<Function> = new Vector.<Function>(),
onDisconnectedFunctionList:Vector.<Function> = new Vector.<Function>();
//public static const webServiceWsdl:String = "192.168.0.130:85/STBWebServices.asmx?wsdl";
public static var webServiceWsdl:String ;//= Constants.appDomain+Constants.asmxurl;
private static var ws:WebService,
retryer:Timer,
isitConnected:Boolean=false,
newsID:uint,
recevedNewsID:uint;;
public static var retrys:uint=5,
connectiontimeOut:Number=1500;
//private static var myHeader:String="myHeader";
private static var activatedOperations:Array ;
public static function setUp(wsdlLocation:String):void
{
//myStage = MyStage ;
webServiceWsdl = wsdlLocation ;
if(operetionsList == null)
{
operetionsList = new Vector.<AbstractOperation>();
operationsVariables = new Vector.<Array>();
}
newsID = 0 ;
recevedNewsID = 0 ;
isitConnected = false;
ws = new WebService();
activatedOperations = [] ;
ws.addEventListener(LoadEvent.LOAD,webServiceReady);
retryer = new Timer(connectiontimeOut,retrys);
retryer.addEventListener(TimerEvent.TIMER,tryAgain);
retryer.addEventListener(TimerEvent.TIMER_COMPLETE,connectionFails);
}
public static function activateOperation(operationName:String):void
{
if(activatedOperations.indexOf(operationName) == -1)
{
trace('activate operation');
ws[operationName].resultFormat = "e4x" ;
ws[operationName].addEventListener("result", Result) ;
ws[operationName].addEventListener("fault", serviceNotFound) ;
activatedOperations.push(operationName) ;
}
}
/**this function start trying to connect to the web service*/
public static function tryToConnect()
{
if(!isitConnected)
{
retryer.stop();
retryer.reset();
retryer.start();
tryAgain();
}
else
{
eventListen.dispatchEvent(new WebEvent(WebEvent.EVENT_CONNECTED));
}
}
/**new function to make easier to check internet connection with less addEventListeners<br>
* callDisconnect whenever you needs to cansel the checking*/
public static function Connect(onConnected:Function,onDisconnected:Function)
{
if(isitConnected)
{
onConnected();
}
else
{
onConnectFunctionList.push(onConnected);
onDisconnectedFunctionList.push(onDisconnected);
myWebService.tryToConnect();
}
}
/**Cansel these listeners from listenning to webservice connection from Connect function*/
public static function Disconnect(onConnected:Object, noInternet:Object):void
{
var I:int = onConnectFunctionList.indexOf(onConnected);
if(I!=-1)
{
onConnectFunctionList.splice(I,1);
}
I = onDisconnectedFunctionList.indexOf(onDisconnectedFunctionList);
if(I!=-1)
{
onDisconnectedFunctionList.splice(I,1);
}
}
/**dispathch event to stage
private static function dispathcer(event:String,Status:Boolean=true,xmlListData:XMLList=null,XMLIem:XML=null,ObjectItem:Object=null,pureString:String='')
{
}*/
/**try to connect to web service again*/
private static function tryAgain(e=null)
{
if(!isitConnected)
{
trace('try to connect : '+webServiceWsdl);
if(DEBUG_DONOT_CONNECT)
{
ws.loadWSDL("no where");
}
else
{
//trace('send webServiceWsdl');
ws.loadWSDL(webServiceWsdl);
}
}
}
/**stop trying*/
private static function connectionFails(e=null)
{
if(!isitConnected)
{
trace('connection failds');
for(var i = 0 ; i<onDisconnectedFunctionList.length ; i++)
{
onDisconnectedFunctionList[i]();
}
onDisconnectedFunctionList = new Vector.<Function>();
eventListen.dispatchEvent(new WebEvent(WebEvent.EVENT_DISCONNECTED));
}
}
/**web service is ready to use*/
private static function webServiceReady(e)
{
trace('web service is ready to use');
if(isitConnected==false)
{
isitConnected = true ;
eventListen.dispatchEvent(new WebEvent(WebEvent.EVENT_CONNECTED)) ;
callAllOperations() ;
for(var i = 0 ; i<onConnectFunctionList.length ; i++)
{
onConnectFunctionList[i]();
}
onConnectFunctionList = new Vector.<Function>();
onDisconnectedFunctionList = new Vector.<Function>();
}
}
private static function callAllOperations():void
{
for(var i = 0 ; i<operetionsList.length ; i++)
{
operetionsList[i].send.apply(operationsVariables[i]);
}
operetionsList = new Vector.<AbstractOperation>();
operationsVariables = new Vector.<Array>();
}
/**any connection failds*/
private static function serviceNotFound(e:*=null)
{
trace('faild : '+e);
//i dispatch event instant;ly
var myTocken:AsyncToken ;
if(e is FaultEvent)
{
myTocken = FaultEvent(e).token ;
}
eventListen.dispatchEvent(new WebEvent(WebEvent.EVENT_DISCONNECTED,'',myTocken));
}
/**Global resutl*/
private static function Result(ev:ResultEvent)
{
var result:String = pureData(ev.result);
trace('Result : '+result);
eventListen.dispatchEvent(new WebEvent(WebEvent.Result,result.toString(),ev.token));
}
public static function callFunction(serviceName:String,params:Array):AsyncToken
{
var showValues:Boolean = true ;
var paramList:Array = [] ;
for(var i = 0 ; i<params.length ; i++)
{
if(params[i] is ByteArray)
{
paramList.push("Bytes");
}
else
{
paramList.push(params[i]);
}
}
trace(serviceName+" : "+paramList);
var op:AbstractOperation = ws.getOperation(serviceName);
op.arguments = params;
return op.send()
}
///////////////////////////////////////////////////////TOOLS ↓
/**cansel this operation with this token*/
public static function CanselThisToken(token:AsyncToken)
{
//var operation:AbstractOperation = ws.getOperation("GetContentListTotals");
if(token == null)
{
return ;
}
for each(var i in ws.operations)
{
i.cancel(token.message.messageId);
}
}
/**returns pure data of receved xml*/
public static function pureData(putputXML:*):String
{
var xmldData:XML = XML(String(putputXML));
return String(xmldData.*/*[0].*[0].*[0]*/);
}
/**returns time stamp to check if current news is up to date or not*/
private static function timeStamp():String
{
var date:Date = new Date();
var str:String = date.fullYear+':'+date.month+':'+date.day+':'+date.hours+':'+date.minutes;
/*if(LEGO.debug)
{
//it will prevent to check time stamp of the news
str+=String(Math.random());
}*/
return str ;
}
/**remove unused meta tags from xml*/
private static function clearXML(str:String):XML
{
var i:int = str.indexOf('<');
i = str.indexOf(' ',i);
var j:int = str.indexOf('>',i);
if(!str.indexOf('>')<i)
{
str = str.substring(0,i)+str.substring(j);
}
return XML(str);
}
///////////////////////////////////Header part
/**Add header*/
public static function setHeader(userName:String,password:String,tocken:String,uid:String):void
{
var qname:QName = new QName("http://tmeappuri.org/","SOAPHeaderContent");
var header:SOAPHeader = new SOAPHeader(qname,{UserName:userName,Password:password,Token:tocken,UID:uid});
ws.clearHeaders();
ws.addHeader(header)
}
public static function addHeader(headerName:String,headerValue:String):void
{
var qname:QName = new QName("http://tmeappuri.org/","SOAPHeaderContent");
var headerObject:Object = {};
headerObject[headerName] = headerValue ;
var header:SOAPHeader = new SOAPHeader(qname,headerObject);
ws.addHeader(header);
}
/**Clear header*/
public static function clearHeader():void
{
ws.clearHeaders();
}
}
}