-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSocketJ.as
More file actions
214 lines (181 loc) · 5.61 KB
/
SocketJ.as
File metadata and controls
214 lines (181 loc) · 5.61 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
package socketJ
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.OutputProgressEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
public class SocketJ
{
private static var socketListener:Socket ;
private static var Ip:String,Port:uint ;
private static var shouldBeConnect:Boolean = false ;
private static var dispatcher:SocketJDispatcher;
private static var connectionRetrierInterval:uint = 5000 ;
private static var connectionRetrierTimeOutId:uint;
private static var dataToSendList:Vector.<SocketJRequestModel> ;
/**Pass true for ConnectInstantly variable to make it connect to server isntantly, otherwise, you should call Connect function by your self*/
public static function setUp(ip:String,port:uint,connectInstantly:Boolean=false):void
{
if(socketListener==null)
{
socketListener = new Socket();
dispatcher = new SocketJDispatcher();
}
else
{
disconnect();
}
dataToSendList = new Vector.<SocketJRequestModel>();
Ip = ip ;
Port = port ;
socketListener.addEventListener(ProgressEvent.SOCKET_DATA,socketDataRecevied);
socketListener.addEventListener(Event.CONNECT,socketConnected);
socketListener.addEventListener(IOErrorEvent.IO_ERROR,noConnectionAvailable);
socketListener.addEventListener(Event.CLOSE,socketClosed);
socketListener.addEventListener(SecurityErrorEvent.SECURITY_ERROR,sercurityError);
socketListener.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS,socketDataOutputOk);
if(connectInstantly)
{
connect();
}
}
protected static function socketClosed(event:Event):void
{
trace("* SocketJ disconnected *");
if(shouldBeConnect)
{
connect();
}
}
protected static function noConnectionAvailable(event:IOErrorEvent):void
{
trace("* SocketJ no connection available, try to connect again");
tryToConnectLater();
}
protected static function socketConnected(event:Event):void
{
trace("* SocketJ connected!! *");
tryToSendLastData();
}
protected static function socketDataRecevied(event:ProgressEvent):void
{
// TODO Auto-generated method stub
trace("* SocketJ Some Data received *");
}
protected static function sercurityError(event:SecurityErrorEvent):void
{
trace("* SocketJ security Error, Socket should be disconnect *");
disconnect();
}
/**From now, your server should allways be connect*/
public static function connect():void
{
disconnect();
trace("* SocketJ try connect "+Ip+":"+Port+" *");
if(shouldBeConnect==false)
{
dispatcher.dispatchEvent(new Event(Event.CONNECT));
}
shouldBeConnect = true ;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,internetConnectionIsOK);
loader.addEventListener(IOErrorEvent.IO_ERROR,noInternetConnectionStablished);
loader.load(new URLRequest("https://google.com"));
}
/**There is no connection at all, try to connect later till disconnect function calls.*/
protected static function noInternetConnectionStablished(event:IOErrorEvent):void
{
trace("* SocketJ no internet Connection Stablished *");
tryToConnectLater();
}
/**Try to connect to server later*/
private static function tryToConnectLater():void
{
clearTimeout(connectionRetrierTimeOutId);
if(shouldBeConnect)
{
connectionRetrierTimeOutId = setTimeout(connect,connectionRetrierInterval);
}
}
/**Stop connectiong*/
private static function stopConnecting():void
{
clearTimeout(connectionRetrierTimeOutId);
}
private static function internetConnectionIsOK(e:Event)
{
trace("* SocketJ is connected to web, try to conect to your socket now");
socketListener.connect(Ip,Port);
}
/**From this time, all connection should drop*/
public static function disconnect():void
{
stopConnecting();
shouldBeConnect = false ;
if(socketListener.connected)
socketListener.close();
trace("* SockjetJ Disconnected *");
if(shouldBeConnect==false)
{
dispatcher.dispatchEvent(new Event(Event.CLOSE));
}
}
////////////////////////////////////////////////////////////////////////////
/**Send data to server, it will open the connection if the connection was closed.*/
public static function sendData(functinoId:uint,dataToSend:Object,replaceWithUnSentCommand:Boolean=true):void
{
if(replaceWithUnSentCommand)
{
for(var i:int = 0 ; i<dataToSendList.length ; i++)
{
if(functinoId == dataToSendList[i].FunctionId)
{
dataToSendList.splice(i,1);
i--;
}
}
}
dataToSendList.push(new SocketJRequestModel(functinoId,dataToSend));
if(socketListener.connected)
{
tryToSendLastData();
}
else
{
connect();
}
}
/**Try to send data que*/
private static function tryToSendLastData():void
{
if(dataToSendList.length>0)
{
trace("* SocketJ try to send first data *");
socketListener.writeUTFBytes(JSON.stringify(dataToSendList[0]));
socketListener.flush();
}
else
{
trace("* SocketJ No data left to send *");
}
}
/**Socket data sent*/
protected static function socketDataOutputOk(event:OutputProgressEvent):void
{
// TODO Auto-generated method stub
trace("* Socket data sent *");
if(event.bytesPending==0)
{
dataToSendList.shift();
tryToSendLastData();
}
}
}
}