From c99ea1cf4817c9d6dcfde570bffe5e3cc9c5dbb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Obr=C3=A1til?= Date: Mon, 10 Nov 2014 10:46:51 +0100 Subject: [PATCH] Fix of close method for wp and android platforms. --- .../socketsforcordova/SocketAdapterImpl.java | 1 + src/wp8/src/SocketAdapter.cs | 1 + src/wp8/src/SocketPlugin.cs | 46 +++++++------------ 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java index 681a0de..bc11d3f 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java @@ -45,6 +45,7 @@ public class SocketAdapterImpl implements SocketAdapter { @Override public void close() throws IOException { + this.invokeCloseEventHandler(false); this.socket.close(); } diff --git a/src/wp8/src/SocketAdapter.cs b/src/wp8/src/SocketAdapter.cs index 07d353c..f71126e 100644 --- a/src/wp8/src/SocketAdapter.cs +++ b/src/wp8/src/SocketAdapter.cs @@ -63,6 +63,7 @@ namespace Blocshop.ScoketsForCordova public void Close() { + this.CloseEventHandler(false); this.socket.Close(); } diff --git a/src/wp8/src/SocketPlugin.cs b/src/wp8/src/SocketPlugin.cs index c73d13e..c85ab4f 100644 --- a/src/wp8/src/SocketPlugin.cs +++ b/src/wp8/src/SocketPlugin.cs @@ -21,56 +21,36 @@ namespace Blocshop.ScoketsForCordova this.socketStorage = SocketStorage.CreateSocketStorage(); } - public void create(string parameters) - { - string socketKey = JsonHelper.Deserialize(parameters)[0]; - - ISocketAdapter socketAdapter = new SocketAdapter(); - socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError); - socketAdapter.DataConsumer = (data) => this.DataConsumer(socketKey, data); - socketAdapter.ErrorHandler = (ex) => this.ErrorHandler(socketKey, ex); - - this.socketStorage.Add(socketKey, socketAdapter); - - this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); - } - - public void registerWPEventDispatcher(string parameters) - { - this.eventDispatcherCallbackId = this.CurrentCommandCallbackId; - PluginResult result = new PluginResult(PluginResult.Status.OK); - result.KeepCallback = true; - DispatchCommandResult(result, this.eventDispatcherCallbackId); - } - public void connect(string parameters) { string socketKey = JsonHelper.Deserialize(parameters)[0]; string host = JsonHelper.Deserialize(parameters)[1]; int port = int.Parse(JsonHelper.Deserialize(parameters)[2]); - ISocketAdapter socket = this.socketStorage.Get(socketKey); + ISocketAdapter socketAdapter = new SocketAdapter(); + socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError); + socketAdapter.DataConsumer = (data) => this.DataConsumer(socketKey, data); + socketAdapter.ErrorHandler = (ex) => this.ErrorHandler(socketKey, ex); + try { - socket.Connect(host, port).Wait(); + socketAdapter.Connect(host, port).Wait(); + + this.socketStorage.Add(socketKey, socketAdapter); this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (SocketException ex) { - this.ErrorHandler(socketKey, ex); this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message)); - socketStorage.Remove(socketKey); } catch (AggregateException ex) { - this.ErrorHandler(socketKey, ex.InnerException); this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message)); - socketStorage.Remove(socketKey); } } - public void write(string parameters/*, string socketKey, byte[] data*/) + public void write(string parameters) { string socketKey = JsonHelper.Deserialize(parameters)[0]; string dataJsonArray = JsonHelper.Deserialize(parameters)[1]; @@ -107,6 +87,14 @@ namespace Blocshop.ScoketsForCordova socket.Close(); } + public void registerWPEventDispatcher(string parameters) + { + this.eventDispatcherCallbackId = this.CurrentCommandCallbackId; + PluginResult result = new PluginResult(PluginResult.Status.OK); + result.KeepCallback = true; + DispatchCommandResult(result, this.eventDispatcherCallbackId); + } + //private void setOptions(CordovaArgs args, CallbackContext callbackContext) throws JSONException { // String socketKey = args.getString(0);