首页 > 解决方案 > xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行

问题描述

我正在尝试使用 xamarin 表单和 signalR 核心构建聊天应用程序,但遇到了一个奇怪的问题 - 我能够将消息从 IOS 设备发送到 android 设备,但我无法从 Android 设备发送消息到IOS设备。

这是我的客户端信号 R 连接代码

        {
            if (hubConnection != null && hubConnection.State == HubConnectionState.Connected)
                return hubConnection;
            else
            {
                try
                {
                   
                    hubConnection = new HubConnectionBuilder()
                    //.WithUrl($"https://{ip}:5050/chatHub")
                    .WithUrl(Constants.UriHostNameType + "chatHub")
                    .ConfigureLogging(logging =>
                    {
                        logging.AddDebug();
                        logging.SetMinimumLevel(LogLevel.Trace);
                    }).WithAutomaticReconnect()
                    .Build();
                    HubConnectionExtensions.On<String, String>(hubConnection, RPCName, (user, message) => OnFunction(user, message));
                    await Connect();

                }
                catch(Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                return hubConnection;
            }
        }

这是我的客户处理程序

        {
            try{
                var messageObj = JsonConvert.DeserializeObject<Message>(message);
                var connection = DependencyService.Get<ISQLiteDb>().GetConnection();
                connection.InsertAsync(messageObj);

                messageRecievedEvent?.BeginInvoke(messageObj, null, null);
            } catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            
        }

这是我从 Android 手机发送消息时在客户端捕获的 Signalr 跟踪

Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Received 21 bytes. Parsing SSE frame.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Passing message to application. Payload size: 11.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Processing 11 byte message from server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Resetting keep-alive timer, received a message from the server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Received a ping message.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Acquired the Connection Lock in order to ping the server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Sending PingMessage message.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Sending PingMessage message completed.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Sending 11 bytes to the server using url: XXXXXXXXXXX.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Releasing Connection Lock in RunTimerActions (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:1817).
Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler: Trace: Sending HTTP request POST 'XXXXXXXX'.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Message(s) sent successfully.

这是我的服务器集线器代码

public async Task SendMessage(string userEmail, string message, string targetUserToken)
        {

            var messageObj = JsonConvert.DeserializeObject<Message>(message);
            try
            {
                await Clients.Group(userEmail).SendAsync("IncomingMessage", userEmail, message);

            }
            catch (Exception e)
            {

            }
            finally
            {
                
                await _notificationService.SendNotificationToUserUsingToken(targetUserToken, messageObj.Text, "New Message from " + messageObj.UIName, 5, message);
                await InserIntoDb(messageObj);
            }



        }

        public async Task InserIntoDb(Message obj)
        {
            using (var context = CoachingContext.GetRawContext(configuration))
            {
                context.messages.Add(obj);
                await context.SaveChangesAsync();
            }
        }

        public async Task LogInUser(string email)
        {
            
            await Groups.AddToGroupAsync(Context.ConnectionId, email);
        }

        public async Task RemoveFromGroup(string email)
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, email);
        }

标签: c#xamarin.formssignalr.clientasp.net-core-signalr

解决方案


您可以检查Constants.UriHostNameType它是否符合 Xamarin.iOS 中的应用程序传输安全性

或者,您可以对应用程序的Info.plist文件进行以下更改,以完全禁用所有域和互联网通信的 ATS:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

推荐阅读