Xamarin Forms Signalr核心客户端处理程序不在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);
        }

解决方法

您可以在符合{strong> App Transport Security in Xamarin.iOS 的情况下使用Constants.UriHostNameType进行支票。

(可选)您可以对应用程序的Info.plist文件进行以下更改,以完全禁用所有域和Internet通信的ATS:

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

在升级到iOS 14之后,我遇到了同样的问题。检查SignalR nuget的版本,然后尝试将所有项目升级到初步版本5.0 rc。