Signalr 信号枢纽;持久连接代码差异
我对信号机使用信号机集线器方法有点熟悉。当我们使用SignalrHub创建通信程序时,我们就这样编写代码Signalr 信号枢纽;持久连接代码差异,signalr,persistent,signalr-hub,Signalr,Persistent,Signalr Hub,我对信号机使用信号机集线器方法有点熟悉。当我们使用SignalrHub创建通信程序时,我们就这样编写代码 public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcast
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
var connection = $.connection("/echo", "name=" + $("#name").val(), true);
客户端我们使用类似javascript的方式连接到hub
<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>
用于连接的客户端
$(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从客户端连接到服务器端的HUB或PersistentConnection
当我们从客户端连接到hub时,我们就这样编写javscript
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
var connection = $.connection("/echo", "name=" + $("#name").val(), true);
在这里,我们以这种方式声明代理,以防出现hub。连接,然后我的集线器名称,不管我给了什么
但是在PersistentConnection的情况下,我们这样声明代理
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
var connection = $.connection("/echo", "name=" + $("#name").val(), true);
我的问题是为什么我们不必给出扩展PersistentConnection类的类名,就像这样公共类MyConnection:PersistentConnection
相反,我们的代码应该是
var connection = $.connection.MyConnection ("/echo", "name=" + $("#name").val(), true);
还告诉我什么是回声?
还请指导我为什么需要将用户作为查询字符串传递
请告诉我为什么在使用hub或PersistentConnection时需要编写不同的代码?谢谢