SignalR:生成的代理与动态创建的集线器文件 [英] SignalR: Generated proxy vs. dynamically created hub file

查看:72
本文介绍了SignalR:生成的代理与动态创建的集线器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SignalR 集线器代理生成器的输出是否与动态生成的集线器代理文件基本相同?如果不是,有什么区别?

Is the output from the SignalR hub proxy generator essentially the same as the dynamically generated hub proxy file? If not, what are the differences?

我的问题的一些背景:由于执行期间的依赖性问题,我正在努力使用命令行工具创建集线器代理,我确实认为获取动态生成的文件可能是一种更简单的方法.

Some background on my question: I am struggling creating the hub proxy using the command line tool due to dependency issues during execution and I do think obtaining the dynamically generated file might be an easier way.

推荐答案

如前所述 在这个 ASP.NET 页面关于使用带有 SignalR 的集线器:

As stated on this ASP.NET page about using hubs with SignalR:

生成的代理及其作用

您可以编写 JavaScript 客户端以与 SignalR 通信带有或不带有 SignalR 为您生成的代理的服务.什么代理为您做的是简化您使用的代码的语法连接,编写服务器调用的方法,并调用服务器上的方法服务器.

You can program a JavaScript client to communicate with a SignalR service with or without a proxy that SignalR generates for you. What the proxy does for you is simplify the syntax of the code you use to connect, write methods that the server calls, and call methods on the server.

当你编写代码调用服务器方法时,生成的代理使您能够使用看起来好像您正在执行本地函数:你可以写 serverMethod(arg1, arg2) 而不是调用('serverMethod',arg1,arg2).生成的代理语法也如果您输入错误,则启用立即且可理解的客户端错误服务器方法名称.如果您手动创建定义的文件代理,您还可以获得编写代码的 IntelliSense 支持调用服务器方法.

When you write code to call server methods, the generated proxy enables you to use syntax that looks as though you were executing a local function: you can write serverMethod(arg1, arg2) instead of invoke('serverMethod', arg1, arg2). The generated proxy syntax also enables an immediate and intelligible client-side error if you mistype a server method name. And if you manually create the file that defines the proxies, you can also get IntelliSense support for writing code that calls server methods.

长话短说:

如果您输入错误的 SignalR 集线器或方法名称,这会使您的生活更轻松,因为真正的 JS 错误.

This makes your life easier with real JS errors if you mistype SignalR hubs or method names.

使用代理:

var contosoChatHubProxy = $.connection.contosoChatHub;
contosoChatHubProxy.client.addContosoChatMessageToPage = function (name, message) {
    console.log(name + ' ' + message);
};

代理:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
    console.log(name + ' ' + message);
});

如果您需要生成代理文件一次而不是在运行时生成它,您可以按照本部分,它允许您预先生成它(用于缓存或捆绑行为).

If you need to generate the Proxy file once instead of generating it at runtime, you can follow this section, which allows you generate it beforehand (for caching or bundling behavior).

这篇关于SignalR:生成的代理与动态创建的集线器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆