protected override void MainRceiveProcess(byte[] msgBytes) { CommandInfo info = JsonConvert.DeserializeObject <CommandInfo>(Encoding.UTF8.GetString(msgBytes)); try { switch (info.Mode) { case CommandInfo.CommandMode.Cmd: CommandProcess(info); break; case CommandInfo.CommandMode.PowerShell: PowerShellProcess(info); break; case CommandInfo.CommandMode.Script: ScriptOrocess(info); break; } } catch (Exception e) { Console.WriteLine(e); } Response(JsonConvert.SerializeObject(info)).Wait(); WS.CloseAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None).Wait(); }
/// <summary> /// Disconnect from the server. /// </summary> /// <param name="async">If true then async</param> public void Disconnect(bool async = false) { if (WSServer != null) { throw new Exception("Cannot both be a server and a client." + " Please use Disconnect if you are diconnecting from a server or StopListening to stop the server."); } if (WS == null) { return; } if (!WS.IsAlive) { return; } if (async) { WS.CloseAsync(); } else { WS.Close(); } DataSocket.Close(); }
/// <summary> /// Creates the client. Will override old clients. /// </summary> /// <param name="address">If != null then use this addres (changes the address of the websocket pipe)</param> public void MakeClient(Uri address = null) { if (address == null) { address = Address; } if (WS != null && WS.IsAlive) { WS.CloseAsync(); } WS = new WebSocket(address.ToString()); WS.OnClose += (s, e) => OnClose(e, SendAsClientWebsocketID); WS.OnOpen += (s, e) => OnOpen(SendAsClientWebsocketID); WS.OnError += (s, e) => OnError(e, SendAsClientWebsocketID); WS.OnMessage += (s, e) => OnDataRecived(e, SendAsClientWebsocketID); WS.Log.Output = (d, s) => WebsocketLogMessage(SendAsClientWebsocketID, d.ToString()); if (m_WaitTimeout == null) { m_WaitTimeout = WS.WaitTime; } else { WS.WaitTime = WaitTimeout; } }
protected override void MainSendProcess(byte[] msgBytes) { CommandInfo info = JsonConvert.DeserializeObject <CommandInfo>(Encoding.UTF8.GetString(msgBytes)); info.OutputList.ForEach(x => x.ConsoleWrite()); WS.CloseAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None).Wait(); }
public async Task DisconnectAsync() { if (WS is null) { return; } // TODO: requests cleanup code, sub-protocol dependent. if (WS.State == WebSocketState.Open) { CTS.CancelAfter(TimeSpan.FromSeconds(2)); await WS.CloseOutputAsync(WebSocketCloseStatus.Empty, "", CancellationToken.None); await WS.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } WS.Dispose(); WS = null; CTS.Dispose(); CTS = null; }