【问题标题】:Signalr hosting client scripts for javascript client in serverSignalr 在服务器中托管 javascript 客户端的客户端脚本
【发布时间】:2018-04-20 22:29:45
【问题描述】:

我有使用 .net 客户端的 SingnalR(OWIN 自托管)服务器集线器。现在我正在准备编写网络客户端。我看到集线器脚本在 http://localhost:10102/signalr/hubs 提供,但看不到 Scripts/jquery-.min.js 和 Scripts/jquery.signalR-.min.js。

我假设这些脚本不是从服务器集线器提供的(但默认情况下,nuget 包含在解决方案中)-我是对的还是遗漏了什么? 有没有办法直接从服务器引用这些脚本(而不是在 javascript 客户端复制和托管它们)?

【问题讨论】:

    标签: javascript c# signalr


    【解决方案1】:

    一般:

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client:

    JavaScript 客户端需要引用 jQuery 和 SignalR 核心 JavaScript 文件。 jQuery 版本必须是 1.6.4 或更高版本 版本,例如 1.7.2、1.8.2 或 1.9.1。如果您决定使用 生成的代理,您还需要对生成的 SignalR 的引用 代理 JavaScript 文件。下面的例子显示了引用的内容 可能看起来像使用生成的代理的 HTML 页面。

    您只需将以下脚本添加到您的 index.html(注意版本):

    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
    <script src="signalr/hubs"></script>
    

    从服务器提供这些文件:

    1. 在放置这些 JS 文件的服务器项目中创建目录
    2. 配置您的服务器以提供这些文件。为此,在 Startup 类中将 app.UseFileServer(); 添加到您的 Configure(...) 方法。 (查看服务文件详情:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
    3. 在客户端中添加所需的脚本。有一个示例(将地址和脚本文件更改为您的文件和您的服务器地址:

      &lt;script type="text/javascript" src="http://localhost:10310/scripts/signalr-clientES5-1.0.0-alpha2-final.js&gt;&lt;/script&gt;

    【讨论】:

    • Scripts/jquery-1.10.2.min.js 是客户端的一部分。可以从信号器服务器主机提供服务吗?
    • @ITMan:我编辑了我的答案。该解决方案有效。我简短地测试了它。
    • 得到了你的答案的灵感。谢谢你的帮助!我不确定如何在此线程中标记答案...?
    • @IT 人。左边的复选标记? meta.stackexchange.com/questions/147531/…
    • :) 我知道如何标记答案。我不确定你的回答是否准确——肯定是正确的线索!
    【解决方案2】:

    实际上,您不需要脚本来实现 SygnalR 服务(后端部分)。要在客户端 index.html 和您的服务之间建立连接,您需要有某种与 SygnalR 配合使用的客户端库来建立连接。

    【讨论】:

    • 我知道我可以从任何地方包含这些脚本,但我更愿意从信号器服务器主机提供它们。例如,这将确保版本兼容性。
    【解决方案3】:

    这是我根据此线程中的答案得出的确切解决方案:

    静态文件(如附加的 javascript 文件)可以在同一主机中提供,配置如下。当放置在 \Scripts 文件夹 iniside 解决方案中时,脚本将在 http://{yourhost}/scripts/{scriptName} 可用(必须为每个文件的“复制到输出目录”设置“如果更新则复制”)。

        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                // Branch the pipeline here for requests that start with "/signalr"
                app.Map("/signalr", map =>
                {
                    // Setup the CORS middleware to run before SignalR.
                    // By default this will allow all origins. You can 
                    // configure the set of origins and/or http verbs by
                    // providing a cors options with a different policy.
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration = new HubConfiguration
                    {
                        // You can enable JSONP by uncommenting line below.
                        // JSONP requests are insecure but some older browsers (and some
                        // versions of IE) require JSONP to work cross domain
                        // EnableJSONP = true
                    };
                    // Run the SignalR pipeline. We're not using MapSignalR
                    // since this branch already runs under the "/signalr"
                    // path.
                    map.RunSignalR(hubConfiguration);
                });
    
                // Serving javascript libraries required for client as static content from Scripts folder
                app.UseStaticFiles(new StaticFileOptions() {
                    RequestPath = new PathString("/scripts"),
                    FileSystem = new PhysicalFileSystem(@".\Scripts"),
                });
            }
        }
    

    IT 人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      相关资源
      最近更新 更多