public string AddReplyHandler(DRP_WebsocketConn wsConn, TaskCompletionSource <object> callback) { string token = GetToken(wsConn); wsConn.ReplyHandlerQueue[token] = callback; return(token); }
public DRP_Client(BrokerProfile argBrokerProfile) { brokerProfile = argBrokerProfile; // Connect to WS wsConn = new DRP_WebsocketConn(brokerProfile.URL, new string[] { "drp" }); wsConn.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12; if (brokerProfile.ProxyAddress != "") { wsConn.SetProxy(brokerProfile.ProxyAddress, brokerProfile.ProxyUser, brokerProfile.ProxyPass); } wsConn.OnOpen += (sender, e) => StartClientSession(wsConn, e); wsConn.OnMessage += (sender, e) => ReceiveMessage(wsConn, e); wsConn.OnError += (sender, e) => Console.WriteLine("Error: " + e.Message); wsConn.OnClose += EndClientSession; wsConn.OnClose += (sender, e) => { if (!clientReady.Task.IsCompleted) { clientReady.SetResult(false); } }; }
public void ReceiveMessage(DRP_WebsocketConn wsConn, EventArgs e) { var thisEndpoint = this; // We received data WebSocketSharp.MessageEventArgs messageArgs = (WebSocketSharp.MessageEventArgs)e; //Console.WriteLine("> " + messageArgs.Data); // See what we received DRP_MsgIn message = Newtonsoft.Json.JsonConvert.DeserializeObject <DRP_MsgIn>(messageArgs.Data); //Console.WriteLine(messageArgs.Data); switch (message.type) { case "cmd": thisEndpoint.ProcessCmd(wsConn, message); break; case "reply": thisEndpoint.ProcessReply(wsConn, message); break; case "stream": thisEndpoint.ProcessStream(wsConn, message); break; default: Console.WriteLine("Invalid message.type; here's the JSON data..." + messageArgs.Data); break; } }
public void ProcessCmd(DRP_WebsocketConn wsConn, DRP_MsgIn message) { if (message.token != null && message.token.Length > 0) { //thisEndpoint.SendReply(wsConn, message.token, cmdResults.status, cmdResults.output); } }
public string GetToken(DRP_WebsocketConn wsConn) { // Generate token //string token = Guid.NewGuid().ToString(); int token = wsConn.TokenNum; wsConn.TokenNum++; return(token.ToString()); }
// Send DRP Cmd public void SendCmd(DRP_WebsocketConn wsConn, string serviceName, string cmd, Dictionary <string, object> @params, TaskCompletionSource <object> callback) { // Get token string token = AddReplyHandler(wsConn, callback); // Send command DRP_Cmd sendCmd = new DRP_Cmd(cmd, serviceName, token, @params); wsConn.Send(Newtonsoft.Json.JsonConvert.SerializeObject(sendCmd)); }
// Send DRP Cmd and wait for results public JObject SendCmd(DRP_WebsocketConn wsConn, string serviceName, string cmd, object @params, int timeout) { // Define return object JObject returnObject = null; // Define task dummy task to await return Task <object> ReturnDataTask = new Task <object>(() => { return(null); }); // Define action to execute task /* * void returnAction() * { * ReturnDataTask.Start(); * } */ Console.WriteLine("Sending cmd"); // Send command, wait up to 30 seconds for return /* * SendCmd(wsConn, serviceName, cmd, @params, data => * { * Console.WriteLine("Starting callback passed to SendCmd..."); * try * { * if (data.GetType() == typeof(JObject)) * { * JObject returnData = (JObject)data; * returnObject = returnData; * } * } * catch (Exception ex) * { * Console.Error.WriteLine("Error converting message to JObject: " + ex.Message + "\r\n<<<" + data + ">>>"); * } * ReturnDataTask.Start(); * }); */ Console.WriteLine("Starting wait"); // Wait for task to complete ReturnDataTask.Wait(timeout); Console.WriteLine("Received response"); // Return Data return(returnObject); }
public void ProcessReply(DRP_WebsocketConn wsConn, DRP_MsgIn message) { if (wsConn.ReplyHandlerQueue.ContainsKey(message.token)) { // Execute callback TaskCompletionSource <object> thisTcs = wsConn.ReplyHandlerQueue[message.token]; thisTcs.SetResult(message.payload); wsConn.ReplyHandlerQueue.Remove(message.token); } else { // Bad token Console.WriteLine("Received command token with no pending callback -> [{0}]", message.token); } }
public async void StartClientSession(DRP_WebsocketConn wsConn, EventArgs e) { // We have a connection //Console.WriteLine("Session open!"); // If we have credentials, authenticate JObject returnedData = await SendCmd_Async("DRP", "hello", new Dictionary <string, object>() { { "userAgent", "dotnet" }, { "user", brokerProfile.User }, { "pass", brokerProfile.Pass } }); if (!clientReady.Task.IsCompleted) { if (returnedData is null) { clientReady.SetResult(false); } else { clientReady.SetResult(true); } } }
public void DeleteReplyHandler(DRP_WebsocketConn wsConn, string token) { wsConn.ReplyHandlerQueue.Remove(token); }
public void ProcessStream(DRP_WebsocketConn wsConn, DRP_MsgIn message) { }