@@ -8,7 +8,7 @@ @implementation CDVSocketPlugin : CDVPlugin
88- (NSString *)buildKey : (NSString *)host : (int )port {
99 NSString * tempHost = [host lowercaseString ];
1010 NSString * tempPort = [NSString stringWithFormat: @" %d " , port];
11-
11+
1212 return [[tempHost stringByAppendingString: @" :" ] stringByAppendingString: tempPort];
1313}
1414
@@ -18,50 +18,50 @@ - (void)connect:(CDVInvokedUrlCommand*)command
1818{
1919 // validating parameters
2020 if ([command.arguments count ] < 2 ) {
21-
21+
2222 // triggering parameter error
2323 CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Missing arguments when calling 'connect' action." ];
2424 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
25-
25+
2626 } else {
27-
27+
2828 // checking connection pool
2929 if (!pool) {
3030 self->pool = [[NSMutableDictionary alloc ] init ];
3131 }
32-
33-
32+
33+
3434 // running in background to avoid thread locks
3535 [self .commandDelegate runInBackground: ^{
36-
36+
3737 CDVPluginResult* result= nil ;
3838 Connection* socket = nil ;
3939 NSString * key = nil ;
4040 NSString * host = nil ;
4141 int port = 0 ;
42-
42+
4343 // opening connection and adding into pool
4444 @try {
45-
45+
4646 // preparing parameters
4747 host = [command.arguments objectAtIndex: 0 ];
4848 port = [[command.arguments objectAtIndex: 1 ] integerValue ];
4949 key = [self buildKey: host :port];
50-
50+
5151 // checking existing connections
5252 if ([pool objectForKey: key]) {
5353 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: key];
5454 NSLog (@" Recovered connection with %@ " , key);
5555 } else {
56-
56+
5757 // creating connection
5858 socket = [[Connection alloc ] initWithNetworkAddress: host :port];
5959 [socket setDelegate: self ];
6060 [socket open ];
61-
61+
6262 // adding to pool
6363 [self ->pool setObject: socket forKey: key];
64-
64+
6565 // formatting success response
6666 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: key];
6767 NSLog (@" Established connection with %@ " , key);
@@ -71,42 +71,42 @@ - (void)connect:(CDVInvokedUrlCommand*)command
7171 NSLog (@" Exception: %@ " , exception);
7272 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Unexpected exception when executing 'connect' action." ];
7373 }
74-
74+
7575 // returning callback resolution
7676 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
77-
77+
7878 }];
79-
79+
8080 }
81-
81+
8282}
8383
8484
8585
8686-(BOOL ) disposeConnection : (NSString *)key {
87-
87+
8888 Connection* socket = nil ;
8989 BOOL result = NO ;
90-
91-
90+
91+
9292 @try {
9393 // getting connection from pool
9494 socket = [pool objectForKey: key];
95-
95+
9696 // closing connection
9797 if (socket) {
9898 [pool removeObjectForKey: key];
99-
99+
100100 if ([socket isConnected ]) {
101101 [socket close ];
102102 }
103103 socket = nil ;
104-
104+
105105 NSLog (@" Closed connection with %@ " , key);
106106 } else {
107107 NSLog (@" Connection %@ already closed!" , key);
108108 }
109-
109+
110110 // setting success
111111 result = YES ;
112112 }
@@ -128,15 +128,15 @@ - (void)disconnect:(CDVInvokedUrlCommand*)command
128128 // triggering parameter error
129129 CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Missing arguments when calling 'disconnect' action." ];
130130 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
131-
131+
132132 } else {
133-
133+
134134 // running in background to avoid thread locks
135135 [self .commandDelegate runInBackground: ^{
136-
136+
137137 CDVPluginResult* result= nil ;
138138 NSString *key = nil ;
139-
139+
140140 @try {
141141 // preparing parameters
142142 key = [command.arguments objectAtIndex: 0 ];
@@ -152,45 +152,45 @@ - (void)disconnect:(CDVInvokedUrlCommand*)command
152152 NSLog (@" Exception: %@ " , exception);
153153 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Unexpected exception when executing 'disconnect' action." ];
154154 }
155-
155+
156156 // returning callback resolution
157157 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
158158 }];
159-
159+
160160 }
161-
161+
162162}
163163
164164-(void ) disconnectAll : (CDVInvokedUrlCommand *) command {
165165
166166 // running in background to avoid thread locks
167167 [self .commandDelegate runInBackground: ^{
168-
168+
169169 CDVPluginResult* result = nil ;
170170 Connection * socket = nil ;
171171 BOOL partial = NO ;
172-
172+
173173 @try {
174-
174+
175175 // iterating connection pool
176176 for (id key in pool) {
177177 socket = [pool objectForKey: key];
178-
178+
179179 // try close it
180180 if (![self disposeConnection: key]) {
181181 // if no success, need to set as partial disconnection
182182 partial = YES ;
183183 }
184184 }
185-
185+
186186 // formatting result
187187 if (partial) {
188188 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Some connections could not be closed." ];
189-
189+
190190 } else {
191191 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK];
192192 }
193-
193+
194194 }
195195 @catch (NSException *exception) {
196196 NSLog (@" Exception: %@ " , exception);
@@ -200,51 +200,51 @@ -(void) disconnectAll: (CDVInvokedUrlCommand *) command {
200200 // returning callback resolution
201201 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
202202 }
203-
203+
204204 }];
205-
205+
206206}
207207
208208
209209-(void )send : (CDVInvokedUrlCommand *) command {
210-
210+
211211 // validating parameters
212212 if ([command.arguments count ] < 2 ) {
213213 // triggering parameter error
214214 CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Missing arguments when calling 'send' action." ];
215215 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
216-
216+
217217 } else {
218-
218+
219219 // running in background to avoid thread locks
220220 [self .commandDelegate runInBackground: ^{
221-
221+
222222 CDVPluginResult* result= nil ;
223223 Connection* socket = nil ;
224224 NSString * data = nil ;
225225 NSString * key = nil ;
226-
226+
227227 @try {
228228 // preparing parameters
229229 key = [command.arguments objectAtIndex: 0 ];
230-
230+
231231 // getting connection from pool
232232 socket = [pool objectForKey: key];
233-
233+
234234 // checking if socket was not found and his conenctivity
235235 if (socket == nil ) {
236236 NSLog (@" Connection not found" );
237237 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" No connection found with host." ];
238-
238+
239239 } else if (![socket isConnected ]) {
240240 NSLog (@" Socket is not connected." );
241241 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Invalid connection with host." ];
242-
242+
243243 } else {
244244 // writting on output stream
245245 data = [command.arguments objectAtIndex: 1 ];
246246 [socket write: data];
247-
247+
248248 // formatting success response
249249 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: key];
250250 NSLog (@" Sending data to %@ - %@ " , key, data);
@@ -254,27 +254,27 @@ -(void)send: (CDVInvokedUrlCommand *) command {
254254 NSLog (@" Exception: %@ " , exception);
255255 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" Unexpected exception when executon 'send' action." ];
256256 }
257-
257+
258258 // returning callback resolution
259259 [self .commandDelegate sendPluginResult: result callbackId: command.callbackId];
260260 }];
261-
262-
261+
262+
263263 }
264264}
265265
266266
267267
268268
269269-(void ) sendMessage : (NSString *)host : (int )port : (NSString *)chunk {
270-
270+
271271 // handling escape chars
272272 NSMutableString *data = [NSMutableString stringWithString: chunk];
273273 [data replaceOccurrencesOfString: @" \n " withString: @" \\ n" options: NSCaseInsensitiveSearch range: NSMakeRange (0 , [data length ])];
274-
274+
275275 // relay to webview
276276 NSString *receiveHook= [NSString stringWithFormat: @" window.tlantic.plugins.socket.receive('%@ ', %d , '%@ ', '%@ ' );" , host, port, [self buildKey: host :port], [NSString stringWithString: data]];
277- [self writeJavascript : receiveHook];
278-
277+ [self .commandDelegate evalJs : receiveHook];
278+
279279}
280280@end
0 commit comments