-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSocketCaller.as
More file actions
204 lines (179 loc) · 5.55 KB
/
SocketCaller.as
File metadata and controls
204 lines (179 loc) · 5.55 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
package socketService
{
import com.mteamapp.JSONParser;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
/**Server connected and receveid data is ready to use*/
[Event(name="complete", type="flash.events.Event")]
/**Server connected but the input data was wrong*/
[Event(name="error", type="flash.events.ErrorEvent")]
/**Server is not connected*/
[Event(name="unload", type="flash.events.Event")]
/**Server data is changed*/
[Event(name="change", type="flash.events.Event")]
public class SocketCaller extends EventDispatcher
{
private var funcName:String ;
private var socketListener:Socket ;
private var sendThisJSON:String ;
/**This will make jsons to pars again for debugging*/
private const debug:Boolean = true ;
private var offlineDataIsOK:Boolean,
justLoadOffline:Boolean,
maxAvailableDateForOffline:Date;
private var dataSentOnce:Boolean;
private var request:SocketRequestFormat;
private var timeOutId:uint;
/**This is the returned value from this service*/
public var catchedData:SocketReceivedFormat;
public function SocketCaller(theFunctionName:String,offlineDataIsOK_v:Boolean=false,justLoadOffline_v:Boolean=false,maxAvailableDateForOffline_v:Date=null)
{
funcName = theFunctionName ;
offlineDataIsOK = offlineDataIsOK_v ;
justLoadOffline = justLoadOffline_v ;
maxAvailableDateForOffline = maxAvailableDateForOffline_v;
socketListener = new Socket();
//socketListener.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS,socketProggress);
}
/**Connection fails*/
protected function noConnectionAvailable(event:IOErrorEvent):void
{
trace("!! The connection fails");
this.dispatchEvent(new Event(Event.UNLOAD));
}
/**Reload the request*/
public function reLoad(delay:uint=20000):void
{
if(delay==0)
{
loadParam(request.Params);
}
else
{
timeOutId = setTimeout(loadParam,delay,request.Params);
}
}
public function loadParam(sendData:Object):void
{
cansel();
catchedData = null ;
socketListener.addEventListener(ProgressEvent.SOCKET_DATA,socketDataRecevied);
socketListener.addEventListener(Event.CONNECT,socketConnected);
socketListener.addEventListener(IOErrorEvent.IO_ERROR,noConnectionAvailable);
dataSentOnce = false ;
request = new SocketRequestFormat(funcName,sendData);
sendThisJSON = JSON.stringify(request);
if(offlineDataIsOK && justLoadOffline)
{
var oldData:String = SocketServiceSaver.load(funcName,sendThisJSON);
if(oldData!=null)
{
socketDataRecevied(null,oldData);
if(maxAvailableDateForOffline==null && SocketServiceSaver.isExpired(funcName,sendThisJSON,maxAvailableDateForOffline))
{
trace("The offlie data dispatched but still need to get the new version of data from server");
}
else
{
trace("The dispached data is updated");
return ;
}
}
}
trace("try to connect to server for "+funcName);
socketListener.connect(SocketInit.ip,SocketInit.port);
}
/**Cansel the connection*/
private function cansel():void
{
clearTimeout(timeOutId);
socketListener.removeEventListener(ProgressEvent.SOCKET_DATA,socketDataRecevied);
socketListener.removeEventListener(Event.CONNECT,socketConnected);
socketListener.removeEventListener(IOErrorEvent.IO_ERROR,noConnectionAvailable);
try
{
socketListener.close();
}
catch(e:Error)
{
//SocketListner.close error
}
}
/**Socket connection is connected*/
private function socketConnected(e:Event):void
{
trace(">>Now send this : "+sendThisJSON);
this.dispatchEvent(new Event(Event.CONNECT));
socketListener.writeUTFBytes(sendThisJSON);
socketListener.flush();
}
/**Dispatch error from server or cashed dat*/
private function socketDataRecevied(e:ProgressEvent,myAbsolutData:String=null):void
{
var receivedData:String ;
catchedData = new SocketReceivedFormat();
if(myAbsolutData==null)
{
trace("<<Socket data is : "+socketListener.bytesAvailable);
if(socketListener.bytesAvailable>0)
{
receivedData = socketListener.readUTFBytes(socketListener.bytesAvailable);
}
else
{
trace("!!! there is no data on the socket !!!");
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
return;
}
}
else
{
receivedData = myAbsolutData ;
}
if(receivedData.indexOf("error")==-1)
{
try
{
JSONParser.parse(receivedData,catchedData);
}
catch(e:Error)
{
trace("The server data is not parsable");
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
return ;
}
}
else
{
trace("error is :: "+receivedData);
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
return ;
}
if(offlineDataIsOK && myAbsolutData==null)
{
SocketServiceSaver.save(funcName,sendThisJSON,receivedData);
}
if(debug)
{
trace("The returned data is : "+JSON.stringify(catchedData,null,' '));
}
var dataWasSentOnce:Boolean = dataSentOnce ;
socketListener.close();
dataSentOnce = true ;
if(dataWasSentOnce)
{
this.dispatchEvent(new Event(Event.CHANGE));
}
else
{
this.dispatchEvent(new Event(Event.COMPLETE));
}
}
}
}