Xamarin + SignalR(可以发送,但不接收消息)

时间:2017-07-17 22:24:57

标签: c# xamarin dependency-injection signalr signalr.client

我可以发送消息 但是我的OnReceiveMessage事件没有触发?

服务器端

public void Send(string name, string message, string roomname)
{
    //All connected clients in a specified group
    Clients.Group(roomname).OnMessageReceived(name, message);
}

Xamarin,我的聊天服务

public event EventHandler<ChatMessage> OnMessageReceived;

public async Task Connect()
{
    await _connection.Start();
    _proxy.On("OnMessageReceived", (string name, string message) => OnMessageReceived(this, new ChatMessage
    {
        Name = name,
        Message = message
    }));
 }

聊天页面背后的Xamarin代码

public ChatViewModel(List<MessageViewModel> messages)
{
    Messages = new ObservableCollection<MessageViewModel>(messages);
    _chatServices = DependencyService.Get<IChatServices>();
    _Channel = "PrivateRoom";
    _chatServices.Connect();
    _chatServices.JoinRoom(_Channel);
    _chatServices.OnMessageReceived += _chatServices_OnMessageReceived;
}
private void _chatServices_OnMessageReceived(object sender, ChatMessage e)
{
    // NO TRIGGER
}

1 个答案:

答案 0 :(得分:1)

我必须在我的网络应用程序上启用CORS。

1)安装软件包&#39; Microsoft.Owin.Cors&#39;。

2)映射signalR并允许它使用CORS。

    public void Configuration(IAppBuilder app)
    {         
        app.Map("/signalR", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration { };
            map.RunSignalR(hubConfiguration);
        });
    }