예제 #1
0
        /// client received message
        private void _appClient_DataReceived(object sender, DataEventArgs e)
        {
            // bug: 已修复
            //string message = Encoding.Default.GetString(e.Data);
            string message = Encoding.Default.GetString(e.Data, e.Offset, e.Length);

            // todo: 解析消息
            // 解析并触发事件
            JObject jMsg = (JObject)JsonConvert.DeserializeObject(message);

            MessageReceievedEvent?.Invoke(jMsg);
        }
예제 #2
0
        /// <summary>
        /// socket 接收到数据 - 事件处理函数
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        private void _appServer_NewRequestReceived(MySession session, MyRequestInfo requestInfo)
        {
            if (!Enabled)
            {
                return;
            }

            // StringRequestInfo 的格式
            // todo: 在使用 json convertor 的时候,如果解析错误怎么办?会发生异常吗?
            if (requestInfo.JsonString.Length != 0)
            {
                // 解析并触发事件
                // todo: exception 解析方法错误
                JObject jMsg = (JObject)JsonConvert.DeserializeObject(requestInfo.JsonString);
                MessageReceievedEvent?.Invoke(jMsg);
            }
            else
            {
                nlogger.Error("received bad message from MySocketSever: requestInfo.Body.Length == 0");
            }
        }
예제 #3
0
            /// <summary>
            /// client 接收到数据
            /// </summary>
            /// <param name="e"></param>
            private void MqttApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e)
            {
                if (!Enabled)
                {
                    return;
                }

                try
                {
                    string text     = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                    string tpString = e.ApplicationMessage.Topic;
                    string QoS      = e.ApplicationMessage.QualityOfServiceLevel.ToString();
                    string Retained = e.ApplicationMessage.Retain.ToString();
                    Console.WriteLine("MessageReceived >>Topic:" + tpString + "; QoS: " + QoS + "; Retained: " + Retained + ";");
                    Console.WriteLine("MessageReceived >>Msg: " + text);

                    // 解析主题
                    string[] tp = tpString.Split('/');
                    if (tp[0] != topicDeviceId)
                    {
                        Console.WriteLine("Error: unknown topicDeviceId from server.");
                        return;
                    }

                    IotTopic tpE;
                    if (!Enum.TryParse(tp[1], out tpE))
                    {
                        Console.WriteLine("Error: unknown IotCSClient.Topic.type from server.");
                        return;
                    }

                    // 触发事件
                    MessageReceievedEvent?.Invoke(tpE, text);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }