/// <summary> /// 傳送對話 /// </summary> /// <param name="chat">對話</param> /// <param name="targetId">朋友或群組 Id</param> /// <returns>執行結果</returns> protected async Task<ExecuteResult> Chat(long targetId, Chat chat) { this.DbContext.Chats.Add(chat); try { await this.DbContext.SaveChangesAsync(); await this.PushChatToClient(this.HubContext, targetId, chat); } catch (Exception ex) { throw this.CreateResponseException(HttpStatusCode.InternalServerError, ex.Message); } return new ExecuteResult(); }
/// <summary> /// 推送對話到相關的客戶端 /// </summary> /// <param name="hub">SignalR HubContext</param> /// <param name="targetId">朋友或群組 Id</param> /// <param name="model">對話</param> protected abstract Task PushChatToClient(IHubContext hub, long targetId, Chat model);
/// <summary> /// 推送對話到自己與特定朋友的客戶端 /// </summary> /// <param name="hub">SignalR Hub</param> /// <param name="friendId">朋友 Id</param> /// <param name="model">對話</param> protected override async Task PushChatToClient(IHubContext hub, long friendId, Chat model) { hub.Clients.User(this.UserId.ToString()).receiveChat(friendId, model); hub.Clients.User(friendId.ToString()).receiveChat(this.UserId, model); await Task.FromResult<object>(null); }