コード例 #1
0
        private string serializeJson(SmartCityEvent scEvent)
        {
            //++++
            //HACK: NOT WORKING. Need to do this manually for now
            //++++

            //need to use the DataContractJsonSerializer becuase NewtonSoft Json does not work in Mono

            //MemoryStream scEventStream = new MemoryStream();
            //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(SmartCityEvent));
            //ser.WriteObject(scEventStream, scEvent);

            //string jsonString = Encoding.Default.GetString((scEventStream.ToArray()));
            //string postString = "{id:\"" + cityName + "\",parkedCount:\"" + ParkedCount + "\",vehicleCount:\"" + VehicleCount + "\"}";

            string jsonString = String.Format(@"{{" +
                                @"""gameTime"":""{0}""," +
                                @"""cityName"":""{1}""," +
                                @"""parkedCount"":""{2}""," +
                                @"""vehicleCount"":""{3}""," +
                                @"""income"":""{4}""," +
                                @"""expenses"":""{5}""" +
                                @"}}",
                                scEvent.gameTime.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), // 0 output date and time with milliseconds
                                scEvent.cityName, //1
                                scEvent.parkedCount.ToString(),  //2
                                scEvent.vehicleCount.ToString(), //3
                                scEvent.income.ToString(), //4
                                scEvent.expenses.ToString()); //5

            return jsonString;
        }
コード例 #2
0
        private void SendEvent()
        {
            string serviceBusNamespace = AppConfig.serviceBusNamespace;
            string eventHubName = AppConfig.eventHubName;
            string publisherName = AppConfig.publisherName;
            string sas = AppConfig.sas;

            Uri uri = new Uri("https://" + serviceBusNamespace +
                                        ".servicebus.windows.net/" + eventHubName +
                                        "/publishers/" + publisherName + "/messages");

            WebClient wc = new WebClient();
            //Next 2 lines are a HACK to work around a MONO bug when posting
            System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
            System.Security.Cryptography.AesCryptoServiceProvider b = new System.Security.Cryptography.AesCryptoServiceProvider();

            wc.Headers[HttpRequestHeader.ContentType] = "application/atom+xml;type=entry;charset=utf-8";
            wc.Headers[HttpRequestHeader.Authorization] = sas;
            //HACK:
            System.Random rnd = new System.Random();
            string temp = rnd.Next(1, 100).ToString();

            int ParkedCount;
            int VehicleCount;
            getVehicleCounts(out ParkedCount, out VehicleCount);

            decimal income;
            decimal expenses;
            getEconomyData(out income, out expenses);

            SmartCityEvent scEvent = new SmartCityEvent
            {
                gameTime = this.gameTime,
                cityName = cityName,
                parkedCount = ParkedCount,
                vehicleCount = VehicleCount,
                income = income,
                expenses = expenses
            };

            //UnityEngine.Debug.Log("Starting to send Azure event");
            try {
                string scEventJson = serializeJson(scEvent);
                //string scEventJson = "test";
                Debug.Log(scEventJson);
                //string postString = "{id:\"" + cityName + "\",parkedCount:\"" + ParkedCount + "\",vehicleCount:\"" + VehicleCount + "\"}";
                //wc.UploadString(uri, "POST", "{id:\"" + cityName + "\",temp:\"" + temp + "\"}");
                wc.UploadString(uri, "POST", scEventJson);
                Debug.Log("POST: " + scEventJson);
            }
            catch(Exception ex )
            {
                UnityEngine.Debug.LogError(ex.Message);
            }
            //UnityEngine.Debug.Log("Completed send to Azure event");
        }