我正在尝试使用特定于组织的Signalr通知系统。
数据库结构:
Organization:
Id[uniqueidentifier]
Name[nvarchar(max)]
AspNetUsers:
Id[nvarchar(128)]
UserName[nvarchar]
Email(nvarchar)
User.Organization:
Id[uniqueidentifier]
UserId[nvarchar(128)]
OrganizationId[uniqueidentifier]
因此,一个用户可以拥有多个组织,一个组织可以拥有多个用户。
注册用户方法:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
创建组织方法:
public ActionResult Create([Bind(Include = "Id,Name")] Organization organization)
{
if (ModelState.IsValid)
{
organization.Id = Guid.NewGuid();
organization.User_Organization.Add(new User_Organization() { Id = Guid.NewGuid(),UserId = User.Identity.GetUserId(),OrganizationId = organization.Id});
db.Organizations.Add(organization);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(organization);
}
向组织添加更多用户:
public ActionResult Create([Bind(Include = "Id,UserId,OrganizationId")] User_Organization user_Organization)
{
if (ModelState.IsValid)
{
user_Organization.Id = Guid.NewGuid();
db.User_Organization.Add(user_Organization);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", user_Organization.UserId);
ViewBag.OrganizationId = new SelectList(db.Organizations, "Id", "Name", user_Organization.OrganizationId);
return View(user_Organization);
}
我的中心班级:
public class MyHub : Hub
{
public void Announce_serverSide_function(string message)
{
Clients.All.Announce_clientSide_function(message);
}
public Task JoinRoom(string roomName)
{
return Groups.Add(Context.ConnectionId, roomName);
}
public Task LeaveRoom(string roomName)
{
return Groups.Remove(Context.ConnectionId, roomName);
}
}
答案 0 :(得分:1)
您必须在特定组中调用某些动态方法(您需要在客户端实现该方法)。
public void JoinRoom(string roomName)
{
Groups.Add(Context.ConnectionId, roomName).Wait();
string playerName = // get player name somehow
Clients.Group(roomName).NotifyUserAdded(playerName);
}
客户代码
<script type="text/javascript">
$(function()){
var hub = $.connection.myHub;
hub.client.notifyPlayerAdded = function(playerName)
{
// show message about player entered room
}
}
注意方法名称的包装也很重要。