Cześć
Mam taki problem , w hubie chce pobrać z tabeli Messages jakieś tam rekordy ale zawsze zwracany jest null , mimo że w bazie są rekordy. Załączam screeny. Nie wiem o co może chodzić , w kontrolerze MVC normalnie sie pojawiają.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.Identity;
using System.Web.Security;
using Communicator.Models;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Cryptography;
namespace Communicator.Hubs
{
public class ChatHub : Hub
{
private DefaultConnection Datebase = new DefaultConnection();
static private List<string> ActiveGroups = new List<string>();
private string GetCallerGroupName()
{
return Context.User.Identity.GetUserId<string>();
}
private bool CheckIfUserIsAvaibleNow(string UserId)
{
string FoundUser = ActiveGroups.Find(x => x == UserId);
return FoundUser != null ? true : false ;
}
public override async Task OnConnected()
{
#if DEBUG
var id = Context.ConnectionId;
Debug.WriteLine("OnConnected: " + id.ToString());
#endif
await Groups.Add(Context.ConnectionId, GetCallerGroupName());
ActiveGroups.Add(GetCallerGroupName());
await base.OnConnected();
}
public override async Task OnDisconnected(bool StopCalled)
{
#if DEBUG
var id = Context.ConnectionId;
Debug.WriteLine("OnConnected: " + id.ToString());
#endif
await Groups.Remove(Context.ConnectionId, GetCallerGroupName());
ActiveGroups.Remove(GetCallerGroupName());
await base.OnDisconnected(StopCalled);
}
public override Task OnReconnected()
{
#if DEBUG
var id = Context.ConnectionId;
Debug.WriteLine("OnReconnected: " + id.ToString());
#endif
return base.OnReconnected();
}
public void GetToken()
{
byte[] Time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
byte[] Key = Guid.NewGuid().ToByteArray();
string Token = Convert.ToBase64String(Time.Concat(Key).ToArray());
Clients.Caller.ServerMessage("Your token: " + Token, DateTime.Now);
Clients.Caller.GetToken(Token);
}
public void GetHistory()
{
string UserIdFrom = Context.User.Identity.GetUserId<string>();
var messages = Datebase.Messages.ToList();
}
public void Send(string Message,
string UserIdTo,
string Token)
{
string UserIdFrom = Context.User.Identity.GetUserId<string>();
bool IsUserToActive = CheckIfUserIsAvaibleNow(UserIdTo);
MessageFlagType Flag = MessageFlagType.MESSAGE_WAITING_FOR_DELIVER;
if (IsUserToActive)
{
Flag = MessageFlagType.MESSAGE_SENDED;
}
try
{
Message SendedMessage = new Message(Message,
UserIdTo,
UserIdFrom,
DateTime.Now,
Context.Request.Environment["server.RemoteIpAddress"].ToString(),
Token,
Flag);
#if DEBUG
Debug.WriteLine(SendedMessage.ToString());
#endif
Datebase.Messages.Add(SendedMessage);
Datebase.SaveChangesAsync();
Clients.Caller.message(UserIdFrom, Message, DateTime.Now);
if (IsUserToActive)
{
Clients.Group(UserIdTo).message(UserIdFrom, Message, DateTime.Now);
}
}
catch (Exception exception)
{
Clients.Group(GetCallerGroupName()).ServerMessage(exception.Message,
DateTime.Now);
}
}
}
}
varmanpl