Javascript 为modelitem上的更改创建信号R回调

Javascript 为modelitem上的更改创建信号R回调,javascript,asp.net-mvc,signalr,Javascript,Asp.net Mvc,Signalr,我总是在web应用程序上显示来自其他客户端的响应消息。到目前为止我做了什么 HomeController private static IHubProxy _hub; private static HubConnection _connection; private string url = @"http://server:port/"; private static Model model = new Model(); public ActionResult Login() {

我总是在web应用程序上显示来自其他客户端的响应消息。到目前为止我做了什么

HomeController

private static IHubProxy _hub;
private static HubConnection _connection;
private string url = @"http://server:port/";
private static Model model = new Model(); 

public ActionResult Login()
{
        _connection = new HubConnection(url);
        _hub = _connection.CreateHubProxy("ServerHub");

        _hub.On<string>("ReturnSendMessage", ShowMessage);
        _hub.On<string>("ReturnSignInMessage", ShowMessage);

        return View();
}

[HttpPost]
public ActionResult Login(FormCollection collection)
{
        model.NickName = collection.Get("NickName");

        _connection.Start().Wait();
        _hub.Invoke("SignIn", model.NickName, true).Wait();

        return RedirectToAction("Chat", "Home");
}

public ActionResult Chat()
{
        return View();
}

[HttpPost]
public ActionResult Chat(FormCollection collection)
{
        model.Message = collection.Get("Message");
        _hub.Invoke("SendMessage", model.NickName, model.Message).Wait(); 

        return View();
}

public ActionResult ChatPartialView()
{
        return PartialView("ChatPartialView");
}
专用静态IHubProxy\u hub;
专用静态HUB连接\u连接;
专用字符串url=@“http://server:port/";
私有静态模型=新模型();
公共操作结果登录()
{
_连接=新连接(url);
_hub=_connection.createhubbroxy(“ServerHub”);
_hub.On(“ReturnSendMessage”,ShowMessage);
_hub.On(“返回信号消息”,ShowMessage);
返回视图();
}
[HttpPost]
公共操作结果登录(FormCollection集合)
{
model.昵称=collection.Get(“昵称”);
_connection.Start().Wait();
_Invoke(“SignIn”,model.昵称,true).Wait();
返回重定向到操作(“聊天”、“主页”);
}
公共行动结果聊天室()
{
返回视图();
}
[HttpPost]
公共操作结果聊天(FormCollection集合)
{
model.Message=collection.Get(“Message”);
_Invoke(“SendMessage”,model.昵称,model.Message).Wait();
返回视图();
}
公共行动结果ChatPartialView()
{
返回PartialView(“ChatPartialView”);
}
查看-聊天室视图

@model DxComWithMe.Models.Model

<div>
    <h3>Chat history</h3>

    @if (Model != null)
    {
        foreach (var item in Model.Messages)
        {
           <strong>@item</strong>
           <br>
        }
    }
</div>
@model DxComWithMe.Models.model
聊天记录
@如果(型号!=null)
{
foreach(Model.Messages中的var项)
{
@item

} }

只要我自己刷新页面,一切都正常。但我想实现一种机制,在模型消息更改后,该机制将自我更新。还是我必须依赖这个方法(javascript…)来实现回调功能:

您在MVC应用程序中使用.NET客户端,无法正常工作(不是您想要的方式)。您需要使用javascript客户端库

您可以从服务器在集线器上调用这样的客户端方法

GlobalHost.ConnectionManager.GetHubContext<ServerHub>()
   .Clients.All.SendMessage(...);
GlobalHost.ConnectionManager.GetHubContext()
.Clients.All.SendMessage(…);

编辑:这涉及到两台服务器吗?

在我看来,您不应该在控制器中处理中心操作,而应该在专用的中心类中处理。signar依赖于客户端的javascript,您必须使用javascript函数在客户端处理中心事件


例如,您可以使用,以便在js模型上绑定dom元素,该模型将与您的中心交互。

我想是这样的:/我只是想在我的wpf客户端中这样做。没有,只有一台服务器(windows服务)处理请求。javascript将是一种方式。我只是想像在我的wpf客户机中那样组织它,但似乎不打算这样做。然后切换到javascript。