Fix of close method for wp and android platforms.

This commit is contained in:
Martin Obrátil 2014-11-10 10:46:51 +01:00
parent b35a04b159
commit c99ea1cf48
3 changed files with 19 additions and 29 deletions

View File

@ -45,6 +45,7 @@ public class SocketAdapterImpl implements SocketAdapter {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
this.invokeCloseEventHandler(false);
this.socket.close(); this.socket.close();
} }

View File

@ -63,6 +63,7 @@ namespace Blocshop.ScoketsForCordova
public void Close() public void Close()
{ {
this.CloseEventHandler(false);
this.socket.Close(); this.socket.Close();
} }

View File

@ -21,56 +21,36 @@ namespace Blocshop.ScoketsForCordova
this.socketStorage = SocketStorage.CreateSocketStorage(); this.socketStorage = SocketStorage.CreateSocketStorage();
} }
public void create(string parameters)
{
string socketKey = JsonHelper.Deserialize<string[]>(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) public void connect(string parameters)
{ {
string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0]; string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
string host = JsonHelper.Deserialize<string[]>(parameters)[1]; string host = JsonHelper.Deserialize<string[]>(parameters)[1];
int port = int.Parse(JsonHelper.Deserialize<string[]>(parameters)[2]); int port = int.Parse(JsonHelper.Deserialize<string[]>(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 try
{ {
socket.Connect(host, port).Wait(); socketAdapter.Connect(host, port).Wait();
this.socketStorage.Add(socketKey, socketAdapter);
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
} }
catch (SocketException ex) catch (SocketException ex)
{ {
this.ErrorHandler(socketKey, ex);
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message)); this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
socketStorage.Remove(socketKey);
} }
catch (AggregateException ex) catch (AggregateException ex)
{ {
this.ErrorHandler(socketKey, ex.InnerException);
this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message)); 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<string[]>(parameters)[0]; string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
string dataJsonArray = JsonHelper.Deserialize<string[]>(parameters)[1]; string dataJsonArray = JsonHelper.Deserialize<string[]>(parameters)[1];
@ -107,6 +87,14 @@ namespace Blocshop.ScoketsForCordova
socket.Close(); 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 { //private void setOptions(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
// String socketKey = args.getString(0); // String socketKey = args.getString(0);