【问题标题】:SignalR using App in browser working well, just not as native app (cordova)SignalR 在浏览器中使用 App 运行良好,只是不像本机应用程序 (cordova)
【发布时间】:2016-12-17 19:43:12
【问题描述】:

我刚刚使用本指南构建了一个简单的“聊天”应用程序:http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20-and-mvc-5

技术:

服务器: ASP.NET MVC 5 + SignalR 2.0
客户端: HTML5/jQuery 2.0.3
构建应用程序: 科尔多瓦

只要我将它用作常规网络应用程序,我的聊天就会正常工作,但如果我开始使用 cordova 将其编译为应用程序,则什么都不会发生。

1) "ChatHub" [Serverh/C#]

using System;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace SignalRChatMVC.Hubs{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.
            Clients.All.addNewMessageToPage(name, message);
        }
    }
}

2) 路径中的“index.html”

"VisualStudioPath\Projects\SignalRChatMVC\SignalRChatMVC\native\net.Companyname.Projectname\www"

.

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat - My ASP.NET Application</title>
    <link href="/Content/bootstrap.css" rel="stylesheet">
    <link href="/Content/site.css" rel="stylesheet">


    <script src="/Scripts/modernizr-2.7.2.js"></script>
    <script src="/Scripts/jquery-2.1.0.min.js"></script>
</head>
<body>
    <div class="app">
        <h2>Chat</h2>
        <div class="container">
            <input type="text" id="message" />
            <input type="button" id="sendmessage" value="Send" />
            <input type="hidden" id="displayname" />
            <ul id="discussion"></ul>
        </div>
        <!--Script references. -->
        <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
        <!--Reference the SignalR library. -->
        <script src="/Scripts/jquery.signalR-2.0.3.min.js"></script>
        <!--Reference the autogenerated SignalR hub script. -->
        <script src="/signalr/hubs"></script>
        <!--SignalR script to update the chat page and send messages.-->
        <script>
            $(function () {
                // Reference the auto-generated proxy for the hub.
                var chat = $.connection.chatHub;
                <!-- $.connection.hub.url = "http://nightcrawler:1234"; -->
                // Create a function that the hub can call back to display messages.
                chat.client.addNewMessageToPage = function (name, message) {
                    // Add the message to the page.
                    $('#discussion').append('<li><strong>' + htmlEncode(name)
                        + '</strong>: ' + htmlEncode(message) + '</li>');
                };
                // Get the user name and store it to prepend to messages.
                $('#displayname').val(prompt('Enter your name:', ''));
                // Set initial focus to message input box.
                $('#message').focus();
                // Start the connection.
                $.connection.hub.start().done(function () {
                    $('#sendmessage').click(function () {
                        // Call the Send method on the hub.
                        chat.server.send($('#displayname').val(), $('#message').val());
                        // Clear text box and reset focus for next comment.
                        $('#message').val('').focus();
                    });
                });
            });
            // This optional function html-encodes messages for display in the page.
            function htmlEncode(value) {
                var encodedValue = $('<div />').text(value).html();
                return encodedValue;
            }
        </script>
    </div>
    <!-- <script type="text/javascript" src="cordova.js"></script> -->
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>

3) 在浏览器中调用我的 mashine,例如

"http://{computername}:1234" 

工作,我可以在任何浏览器/标签或设备上写消息。

4)现在我开始用cordova编译:

cmd: "{VisualStudioPath}\Projects\SignalRChatMVC\SignalRChatMVC\native\net.{Companyname}.{Projectname}"> cordova build blackberry10

cmd: "{VisualStudioPath}\Projects\SignalRChatMVC\SignalRChatMVC\native\net.{Companyname}.{Projectname}"> cordova run blackberry10

然后只出现主屏幕。

有谁知道如何调试本机应用程序和/或知道为什么它不适用于本机应用程序? 是否可能与黑莓的路径有关,它不在

"{VisualStudioPath}\Projects\SignalRChatMVC\SignalRChatMVC\native\net.{Companyname}.{Projectname}\www"

但在

"{VisualStudioPath}\Projects\SignalRChatMVC\SignalRChatMVC\native\net.oh22.oh22Push\platforms\blackberry10\www" 

或者可能是因为没有真正的浏览器能够使用 SignalR?

(应该可以直接在黑莓上调试)

非常感谢

【问题讨论】:

    标签: jquery asp.net-mvc cordova signalr


    【解决方案1】:

    &lt;script src="/signalr/hubs"&gt;&lt;/script&gt; 可能是错误的。

    如果资源没有与您的“index.html”绑定,而是需要通过网络访问,您可能需要使用绝对路径来引用它。例如:

    <script src="http://{computername}:1234/signalr/hubs"></script>
    

    您可以预先生成 /signalr/hubs 脚本,以便将其捆绑:http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#manualproxy

    最后,$.connection.hub.url = "http://nightcrawler:1234"; 应该是 $.connection.hub.url = "http://nightcrawler:1234/signalr";

    【讨论】:

      【解决方案2】:

      添加方法

           $.connection.hub.url = "http://{computername}:1234/signalr";
      

      对我来说效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 2016-02-01
        • 1970-01-01
        • 2013-03-02
        • 2012-07-11
        相关资源
        最近更新 更多