Ich arbeite in SignalR Chat Projekt like this examplewie SignalR
Es funktioniert gut, aber ich möchte machen Chat hub.cs in einem Web-API-Projekt und nennen es von anderen Kundenprojekten in Web-API arbeitet.
Ich habe nach Lösungen gesucht, habe aber keine gefunden. Wie erreiche ich das? Mein
chatHub
public class ChatHub : Hub
{
dbcontext db = new dbcontext();
ChatManager chatManager = new ChatManager();
public static string ConnectionId = null;
public void Send(string name, string message)
{
ConnectedUsers cu = chatManager.getCurrentUserRow(Context.ConnectionId);
if (cu != null)
{
string L_ConnectionId = cu.Other_ConnectionId;
Clients.Client(L_ConnectionId)
.addNewMessageToPage(name, message);
}
}
public override Task OnConnected()
{
Console.WriteLine("Connected user: " + Context.User.Identity.Name);
ConnectionId = Context.ConnectionId;
chatManager.AddAndRemoveUser(true, Context.ConnectionId); // to add user in connectedUser Table
chatManager.OpenRandomChat(Context.ConnectionId);
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
chatManager.RemoveLeftUser(Context.ConnectionId);
chatManager.AddAndRemoveUser(false, Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}
public override Task OnReconnected()
{
Console.WriteLine("ReConnected user: " + Context.User.Identity.Name);
return base.OnReconnected();
}
public void NewConnection()
{
int retVal = -1;
chatManager.RemoveLeftUser(Context.ConnectionId); // to remove left user from 2 users
chatManager.OpenRandomChat(Context.ConnectionId); // to chosse random user for me
// to get me and my if i have partner ..
ConnectedUsers cu = chatManager.getCurrentUserRow(Context.ConnectionId);
if (cu != null)
{
retVal = 0;
//string groupId = cu.ConnectionId + cu.Other_ConnectionId;
//string R_ConnectionId = cu.ConnectionId;
string L_ConnectionId = cu.Other_ConnectionId;
}
Clients.Client(Context.ConnectionId)
.sendGroupData(retVal);
}
public void SendQuestion(string question)
{
ConnectedUsers cu = chatManager.getCurrentUserRow(Context.ConnectionId);
if (cu != null)
{
string L_ConnectionId = cu.Other_ConnectionId;
Clients.Client(L_ConnectionId)
.sendQuestion(question);
}
}
public void AnswerQuestion(string answer)
{
ConnectedUsers cu = chatManager.getCurrentUserRow(Context.ConnectionId);
if (cu != null)
{
string L_ConnectionId = cu.Other_ConnectionId;
Clients.Client(L_ConnectionId)
.answerQuestion(answer);
}
}
}
entschuldigen Sie mich es ist nicht mein Problem. Mein Problem ist, wie kann ich meinen Hub in API erstellen und es aus Client-Projekt aufrufen –