예제 #1
0
        // 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));
        }
예제 #2
0
        public async Task <JObject> SendCmd_Async(string serviceName, string cmd, Dictionary <string, object> @params)
        {
            //return await Task.Run(async() =>
            //{
            TaskCompletionSource <object> thisTcs = new TaskCompletionSource <object>();

            /*
             * Task<object> ReturnDataTask = new Task<JObject>(Func<JObject>() =>
             * {
             *  return null;
             * });
             */

            // Get token
            string token = AddReplyHandler(wsConn, thisTcs);

            // Send command
            DRP_Cmd sendCmd       = new DRP_Cmd(serviceName, cmd, token, @params);
            string  sendCmdString = null;

            try
            {
                if (cmd != "hello")
                {
                    //sendCmd = new DRP_Cmd("DRP", "pathCmd", "5", null);
                }
                sendCmdString = Newtonsoft.Json.JsonConvert.SerializeObject(sendCmd);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error serializing DRP_Cmd to string: " + ex.Message);
                return(null);
            }
            wsConn.Send(sendCmdString);
            //Console.WriteLine("< " + sendCmdString);
            object data = null;

            if (await Task.WhenAny(thisTcs.Task, Task.Delay(brokerProfile.Timeout), clientClosed.Task) == thisTcs.Task)
            {
                // task completed within timeout
                data = await thisTcs.Task;
            }
            else
            {
                // timeout logic
                return(null);
            }
            JObject returnObject = null;

            //Console.WriteLine("Starting callback passed to SendCmd...");
            try
            {
                if (data != null && 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 + ">>>");
            }

            return(returnObject);
            //});
        }