Пример #1
0
        /// <summary>
        /// Connects the IR client to the host configured in the settings.
        /// </summary>
        /// <returns><c>true</c>, if the client could successfully be started, else <c>false</c>.</returns>
        private bool StartClient()
        {
            try
            {
                ISettingsManager   settingsManager = ServiceRegistration.Get <ISettingsManager>();
                WifiRemoteSettings settings        = settingsManager.Load <WifiRemoteSettings>();

                // Start listening for client connections
                _socketServer = new SocketServer(Convert.ToUInt16(settings.Port))
                {
                    PassCode          = settings.PassCode,
                    AllowedAuth       = (AuthMethod)settings.AuthenticationMethod,
                    AutologinTimeout  = settings.AutoLoginTimeout,
                    ShowNotifications = false
                };

                _socketServer.Start();

                if (settings.EnableBonjour)
                {
                    // start ZeroConfig
                    _zeroConfig = new ZeroConfig(Convert.ToUInt16(settings.Port), settings.ServiceName, "");
                    _zeroConfig.PublishBonjourService();
                }

                // start the MP Message Handler
                _mpMessageHandler = new MPMessageHandler();
                _mpMessageHandler.SubscribeToMessages();

                // Status updated
                StatusUpdater.Start();

                ServiceRegistration.Get <ILogger>().Info("WifiRemote: Server started");
                return(true);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Error("WifiRemote: Failed to start server, input service is unavailable for this session", ex);
            }
            return(false);
        }
        public async Task <IActionResult> Index(long timestamp, string nonce, string signature, string echostr, string openid)
        {
            _logger.LogInformation($"请求: {Request.Method} {Request.Path} {Request.QueryString}");

            _logger.LogInformation($"请求参数: ");
            _logger.LogInformation($"timestamp:{timestamp}, nonce:{nonce}, signature:{signature}, echostr:{echostr}");

            string body = string.Empty;

            try
            {
                if (Request.Body != null)
                {
                    using (StreamReader stream = new StreamReader(Request.Body))
                    {
                        body = stream.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
            }

            _logger.LogInformation($"请求 Body: ");
            _logger.LogInformation($"{body}");

            string token = _configuration.GetValue <string>("WeiXinMp:Token");

            var signatureString = _bizMessageCryptService.Encrypt(token, timestamp, nonce);

            _logger.LogInformation($"签名结果: {signatureString} {string.Equals(signatureString, signature, StringComparison.InvariantCultureIgnoreCase)}");

            if (!string.Equals(signatureString, signature, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Ok("fail[签名比对不正确]"));
            }

            // 回复
            if (!string.IsNullOrEmpty(echostr))
            {
                return(Ok(echostr));
            }

            if (!string.IsNullOrEmpty(body))
            {
                var handle = new MPMessageHandler();
                handle.Notifications.HandleMessage = (context) =>
                {
                    if (context.Request != null)
                    {
                        //var res = new ResponseMessageBuilder().Create(new ResponseMessage
                        //{
                        //    ToUserName = context.Request.FromUserName,
                        //    FromUserName = context.Request.ToUserName,
                        //})
                        //.SetContent(new TextResponseMessageContentModel()
                        //{
                        //    Content = "hello. \n你好. \n</aaa>"
                        //})
                        //.Build();

                        //context.SetResponse(res);

                        if (context.MessageType == MessageType.Event)
                        {
                            if (context.EventType == MessageEventType.Subscribe)
                            {
                                _logger.LogInformation("用户关注");

                                context.SetResponse(
                                    context.CreateResponse(new TextResponseMessageContentModel()
                                {
                                    Content = $"欢迎关注。\n {Newtonsoft.Json.JsonConvert.SerializeObject(context.Request)}"
                                })
                                    );
                            }
                            else if (context.EventType == MessageEventType.Unsubscribe)
                            {
                                _logger.LogInformation("用户 取消关注");
                            }
                            else if (context.EventType == MessageEventType.Location)
                            {
                                _logger.LogInformation("上报位置信息");
                                _logger.LogInformation(Newtonsoft.Json.JsonConvert.SerializeObject(context.GetLocationEventRequestMessage()));

                                context.SetResponse(
                                    context.CreateResponse(new TextResponseMessageContentModel()
                                {
                                    Content = $"上报位置信息。\n {Newtonsoft.Json.JsonConvert.SerializeObject(context.GetLocationEventRequestMessage())} ",
                                })
                                    );
                            }
                            else
                            {
                                context.SetResponse(
                                    context.CreateResponse(new TextResponseMessageContentModel()
                                {
                                    Content = $"请求内容:\n" +
                                              $"{Newtonsoft.Json.JsonConvert.SerializeObject(context.Request)}"
                                })
                                    );
                            }
                        }
                        else
                        {
                            //context.SetResponse(new ResponseMessageModel()
                            //{
                            //    ToUserName = context.Request.FromUserName,
                            //    FromUserName = context.Request.ToUserName,
                            //    MsgType = "text",
                            //    Content = new TextResponseMessageContentModel()
                            //    {
                            //        Content = "hello. \n你好. \n</aaa>"
                            //    },
                            //});

                            context.SetResponse(
                                context.CreateResponse(new TextResponseMessageContentModel()
                            {
                                Content = $"hello. \n" +
                                          $"你好. \n" +
                                          $"<a href='http://baidu.com'>baidu</a>。\n" +
                                          $"请求内容:\n" +
                                          $"{Newtonsoft.Json.JsonConvert.SerializeObject(context.Request)}"
                            })
                                );
                        }
                    }

                    return(Task.CompletedTask);
                };

                var result = await handle.HandleAsync(body);

                _logger.LogInformation($"处理消息结果: ");
                _logger.LogInformation(result);

                return(Ok(result));
            }

            return(Ok(1));
        }