2017-07-17 87 views
1

我可以發送消息 但我的OnReceiveMessage事件不會觸發?Xamarin + SignalR(發送可能,但不接收消息)

服務器端

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

Xamarin,我從chatpage

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 
} 
+0

是否OnMessageReceived在客戶端上調用,如果服務器將其推送給所有客戶端(而不是僅僅是房間)? –

+1

@AndrewTavera,似乎不得不啓用cors。 – Eli

回答

0

chatservice

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後面的代碼,我不得不讓我的web應用程序CORS。

1)安裝包'Microsoft.Owin.Cors'。

2)映射signalR並允許它使用CORS。

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