7

在花了两天时间让这个工作没有成功之后,最后我在这里问这个问题。

背景:我正在尝试使用 SignalR 将实时数据从我的桌面应用程序发送到所有网页。

我所拥有的是一个控制台应用程序(这只是为了让概念工作,然后将其移动到活动项目),需要将实时数据发送到内置于 asp .net 中的网页。两者都使用.Net 4。

在 IE9 中,它在“chat.client.broadcastMessage =”行的调试模式下显示错误,说 chat.client 为空或未定义。

在 firfox 中,它没有向我显示该错误,但它没有工作/没有做任何事情,并且问题与它没有向我显示警报('asking for name')相同;window 所以我猜它没有到达那里并在此之前抛出错误。

这是我的网页代码。这是一个新的独立网站项目。

<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <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. -->
    <!--Reference the jQuery library. -->

    <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
    <script src="Scripts/json2.js" type="text/javascript"></script>
    <!--Reference the SignalR library. -->

    <script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript">
        $(function () {
            alert('starting scrip');
            // Declare a proxy to reference the hub. 
            $.connection.hub.url = 'http://<ipaddressORlochost>:8080/chatroom';
            alert($.connection.hub);
            alert($.connection.hub.url);
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            alert(chat);
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message. 
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            alert('asking for name');
            // 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({ jsonp:true}).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();
                });
            });
        });
    </script>
</body>
</html>
-------------------------

我尝试删除下面的行并在最后添加“/signalr”。

$.connection.hub.url = 'http://:8080/chatroom';

这是我的桌面应用程序(服务器)代码。

class Program
    {

        static void Main(string[] args)
        {


            using (WebApplication.Start<Startup>(@"http://<ipaddressORlochost>:8080/chatroom"))
            {

                while (true)
                {
                   // GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.addMessage("dsf","asdfd");
                    Console.WriteLine("Tags sent :" + DateTime.Now.ToString("HH:mm:ss"));
                    Thread.Sleep(3000);
                }
            }

        }

 class Startup
    {

        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration { EnableCrossDomain=true };
            app.MapHubs(config);
        }
    }

    public class ChatHub : Hub
     {
        public void Send(string b,string a)
        {
          try
           {
                Clients.All.broadcastMessage(b,a);
            }
             catch { }
        }
     }

任何帮助将非常感激。

提前谢谢大家。

-------------- 找到答案 ---------- 已编辑此问题,因为无法在 8 小时内回答我自己的问题

大家好

以防万一其他人有同样的问题。我找到了答案/已修复。

感谢旗木卡卡西

主要问题是 $.connection 现在更改为 $.hubConnection

这是我必须改变的。

我的网页中的脚本。我留下了注释代码以显示替换为什么。

 <script type="text/javascript">
        $(function () {
            alert('starting scrip');
            // Declare a proxy to reference the hub. 
            // $.connection.hub.url = 'http://localhost:8080/chatroom';
            var conn = $.hubConnection('http://localhost:8080/chatroom');

            alert($.connection.hub);
            alert($.connection.hub.url);

            var chat = conn.createHubProxy('chatHub'); //$.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            alert(chat);
            //            chat.client.broadcastMessage = function (name, message) {
            //                // Html encode display name and message. 
            //                var encodedName = $('<div />').text(name).html();
            //                var encodedMsg = $('<div />').text(message).html();
            //                // Add the message to the page. 
            //                $('#discussion').append('<li><strong>' + encodedName
            //                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            //            };
            chat.on('addMessage', function (a, message) {

                $('#discussion').append('<li><strong>' + $('<div />').text(a).html()
                                + '</strong>:&nbsp;&nbsp;' + $('<div />').text(message).html() + '</li>');

            });

            alert('asking for name');
            // 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.
            conn.logging = true;
            // Start the connection
            conn.start().done(function () {
                alert("Now connected!");
            }).fail(function () {
                alert("Could not Connect!");
            });


//            $.connection.hub.start({ jsonp: true }).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();
//                });
//            });
        });
    </script>

在服务器端(桌面应用程序),因为这是一种方式,只有我不需要 ChatHub 中的任何内容

 static void Main(string[] args)
        {


            using (WebApplication.Start<Startup>(@"http://localhost:8080/chatroom"))
            {

                while (true)
                {
                    GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.addMessage("dsf", "Tags sent :" + DateTime.Now.ToString("HH:mm:ss"));
                    Console.WriteLine("Tags sent :" + DateTime.Now.ToString("HH:mm:ss"));
                    Thread.Sleep(500);
                }
            }

        }

 class Startup
    {

        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration { EnableCrossDomain=true };
            app.MapHubs(config);
        }
    }
 public class ChatHub : Hub
       {
            /public void Send(string b, string a)
           //{
           //    try
           //    {
           //        Clients.All.addMessage(b, a);
           //    }
           //    catch { }
           //}
       }

希望这对其他人有帮助。

4

0 回答 0