我使用Signalr Hub方法熟悉Signalr . 当我们使用Signalr Hub创建通信程序时,我们就像这样编写代码

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

和客户端我们使用这种方式使用javascript连接到集线器

<script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub. 
            var chat = $.connection.chatHub;
            // 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();
                });
            });
        });
    </script>

当我们使用PersistentConnection编写通信程序时,代码看起来像

public class MyConnection : PersistentConnection
{
    protected override Task OnConnected(IRequest request, string connectionId)
    {
        string msg = string.Format(
            "A new user {0} has just joined. (ID: {1})",
            request.QueryString["name"], connectionId);
        return Connection.Broadcast(msg);
    }
    protected override Task OnReceived(IRequest request, string connectionId, string data)
    {
        // Broadcast data to all clients
        string msg = string.Format(
            "{0}: {1}", request.QueryString["name"], data);
        return Connection.Broadcast(msg);
    }
}

client side for connecting

$(function () {
            $("#join").click(function () {
                var connection = $.connection("/echo", "name=" + $("#name").val(), true);;

                connection.received(function (data) {
                    addMsg(data);
                });
                connection.error(function (err) {
                    addMsg("Error: " + err);
                });

                addMsg("Connecting...");
                connection.start(function () {
                    addMsg("Connected.");
                    $("#send").click(function () {
                        connection.send($("#msg").val());
                    });
                });
            });
        });

现在,当有人看到使用HUB或PersistentConnection开发任何通信应用程序的代码时,他们肯定会注意到有不同的方法通过javascript从客户端连接到服务器端的集线器或PersistentConnection .

当我们从客户端连接到集线器时,我们就像这样编写javscript

// Declare a proxy to reference the hub. 
var chat = $.connection.chatHub;

在这里,我们在hub的情况下以这种方式声明代理 . 连接,然后我的中心名称,无论我给了什么 .

但是在PersistentConnection的情况下,我们就像这样声明代理

var connection = $.connection("/echo", "name=" + $("#name").val(), true);

我的问题是为什么我们不必像这样扩展PersistentConnection类的类名 public class MyConnection : PersistentConnection 而不是我们的代码看起来像

var connection = $.connection.MyConnection ("/echo", "name=" + $("#name").val(), true);

还告诉我什么是回声?还指导我为什么我需要将用户作为查询字符串传递?

只是指导我为什么所以我们需要在使用hub或PersistentConnection时以不同方式编写代码?谢谢