gpt4 book ai didi

c# - 在发送数据之前必须调用 SignalR 控制台应用启动

转载 作者:太空狗 更新时间:2023-10-30 01:07:04 25 4
gpt4 key购买 nike

我正在尝试创建一个客户端信号器应用程序。目前,我能够让我的 MVC 客户端向 Hub 发送消息或从 Hub 发送消息,但是,我的 .NET 客户端应用程序遇到了一些困难

服务器中心代码:

namespace ServerHub
{
public class ChatterBox : Hub
{

public void Send(string message)
{
Clients.All.addMessage(message);
}
}
}

Console App代码(几乎是直接从github上提炼出来的)

using Microsoft.AspNet.SignalR.Client.Hubs;
using ServerHub;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Say("hello from console");
}
public static void Say(string message)
{
//var connection = new HubConnection("http://localhost/");

//IHubProxy myHub = connection.CreateHubProxy("ChatterBox");

var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
// Do more stuff here
}
});

connection.Send("Hello").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success");
}
});
}
}
}

我在 connection.Send()

期间收到以下错误消息

Start must be called before data can be sent.

我哪里出错了?

编辑:

第二次尝试:

    var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}",
connection.ConnectionId);

// Do more stuff here

connection.Send("Hello");
}
});


myHub.Invoke("Send", "lol");

还是报错

编辑:第三次尝试

            var connection = new HubConnection("http://localhost:60610/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
//Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

myHub.Invoke("Send", "lol");
}
});

似乎在工作

我还需要设置端口号 - 例如,如果我像这样浏览我的 MVC 网站:http://localhost:60610/,这是我需要设置的地址我的控制台应用程序。

我可以说在部署过程中这不会成为问题吗,因为默认情况下它是端口 80?

最佳答案

阅读此 http://msdn.microsoft.com/en-us/library/dd460717.aspx

TL;DR Start 是异步的,这不是您编写顺序异步代码的方式:

    var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

// Do more stuff here

connection.Send("Hello").ContinueWith(task =>
{
.......
});
}
});

如果您可以使用 .NET 4.5,它会让您的生活更轻松:

var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
try
{
await connection.Start();
Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
}
catch(Exception ex)
{
Console.WriteLine("Failed to start: {0}", ex.GetBaseException());
}

await connection.Send("Hello");

更新:让我们再试一次。下面的代码可以工作,但您应该真正阅读该链接以了解如何编写异步代码。否则,您将一遍又一遍地遇到这些问题。

var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}",
connection.ConnectionId);

myHub.Invoke("Send", "lol");
}
});

关于c# - 在发送数据之前必须调用 SignalR 控制台应用启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13643173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com