简体   繁体   English

我的signalR客户端缺少什么基本组件?

[英]What basic component am I missing from my signalR client?

Following this tutorial: 遵循本教程:

http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

I managed to implement a really, really simple signalr chat client. 我设法实现了一个非常非常简单的Signalr聊天客户端。 That is, a hub that broadcasts the message sent to it from javascript. 即,一个广播从javascript发送给它的消息的集线器。

Great, that works. 很好,行得通。

But now I've tried to create a second project with the same index.html file and js (ie the code that sends the message), and I'm getting an undefined error. 但是现在我试图用相同的index.html文件和js(即发送消息的代码)创建第二个项目,但出现了未定义的错误。 The only difference between the file when it was as the tutorial specifies and as I've done (in the 'remote' project), is that I've explicitly specified the connection.hub.url as I've been advised to from various posts. 正如本教程指定的和我完成的一样(在“远程”项目中),文件之间的唯一区别是我已明确建议指定connection.hub.url ,这是我从各种建议中得到的建议。帖子。

What exactly am I missing? 我到底在想什么? It's obviously not enough to simply set the url to the other site hosting the signalr hub, do I need to add a proxy of some sort? 仅仅将URL设置到托管信号中心的另一个站点显然是不够的,我是否需要添加某种代理? Totally confused at this point. 在这一点上完全困惑。

My javascript file (the javascript on the website without the hub): 我的javascript文件(不带集线器的网站上的javascript):

$(function () {
            var chat = $.connection.chatHub;
            $.connection.hub.url = "http://localhost:14113/signalr";

            chat.client.broadcastMessage = function (name, message) {
                // Do something
            };

            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    chat.server.send($('#displayname').val(), $('#message').val());
                });
            });
        });

It is on the first reference to the chat variable I have the undefined error. 它是对chat变量的第一次引用,我有未定义的错误。

NOTE: I can confirm both sites are running, the hub is available as I can navigate to the other tab (the same index.html file in the original solution) and it functions as expected. 注意:我可以确认两个站点都在运行,可以使用集线器,因为我可以导航到另一个选项卡(原始解决方案中的相同index.html文件),并且它可以正常运行。

I can also confirm the presence of my jQuery library (referenced correctly), and the jQuery signalR script (again, no 404s, all referenced correctly). 我还可以确认我的jQuery库(正确引用)和jQuery signalR脚本(同样,没有404,都正确引用)的存在。

You are trying to access to the chatHub which is generated in your host application. 您正在尝试访问在主机应用程序中生成的chatHub And it looks like your code fails on getting that chatHub . 看起来您的代码无法获取该chatHub You need to make sure that you are including hubs in your client application like so: 您需要确保在客户端应用程序中包括集线器,如下所示:

<script src="localhost:14113/signalr/hubs"></script>

Try a proxy with xdomain such as 尝试使用xdomain这样的代理,例如

$(function () {

    $.connection.hub.url = 'http://localhost:14113/signalr';

    $.connection.hub.start({ xdomain: true })
        .done(function () { console.log('Connected. connectionId : ' + $.connection.hub.id); })
        .fail(function () { console.log('Could not connect!');
    });

    var proxy = $.connection.chatHub;

    proxy.client.broadcastMessage = function(uri) {
        //Do something
    };

    $('#sendmessage').click(function () {
        proxy.server.send($('#displayname').val(), $('#message').val());
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM