gpt4 book ai didi

asp.net - SignalR 1.0alpha2 控制台服务器应用程序

转载 作者:行者123 更新时间:2023-12-02 12:22:10 25 4
gpt4 key购买 nike

我正在使用新的 1.0alpha2 版本的 signalR。我想在 ASP.NET 应用程序之外实现 SignalR 服务器。这样两个控制台应用程序就可以相互通信。

使用旧的 0.5.3 版本,我能够“Install-Package SignalR.Hosting.Self”到:

var server = new Server("http://127.0.0.1:8088/");

但是在新的 1.0alpha2 版本中我无法安装这个 NuGet 包...

任何人都可以给我一个链接,或者两个基于 1.0alpha2 版本的控制台应用程序的工作迷你示例。 (我只能找到不工作的旧 0.5.3 示例...)。

<小时/>

好的。所以我遵循了你的指示。现在:

我的客户端控制台:

class Programm 
{
static void Main(string[] args)
{
var connection = new HubConnection("http://localhost/");
IHubProxy myHub = connection.CreateHubProxy("MyHub");
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
Console.WriteLine("No Connection: " + task.Exception.GetBaseException());
else
Console.WriteLine("Connected!");
});

myHub.Invoke("Send");

Console.ReadLine(); // wait...
}
}

这是我的服务器控制台:

class Program : Hub
{
static void Main(string[] args)
{
Console.ReadKey();
}
public void Send(string message)
{
Debug.WriteLine("Server Method [send] was called");
Console.WriteLine("Server Method [send] was called");
}
}

但我认为这是无稽之谈......

最佳答案

自从 SignalR 正式发布以来,您需要使用新的 NuGet 包:(说是 ASP,但它也在 .NET 应用程序中使用)

安装包 Microsoft.AspNet.SignalR -pre 服务器

Install-Package -pre Microsoft.AspNet.SignalR.Client 客户端

这里还有一个预构建的示例应用程序,您可以将控制台应用程序挂接到其中进行一些测试:

安装包 Microsoft.AspNet.SignalR.Sample

使用客户端;连接两个控制台应用程序,其中一个需要托管集线器,另一个需要使用指向主机的客户端连接。

您需要的所有信息都在这个客户端 wiki 中:link

<小时/>

编辑

服务器:(使用自托管)

class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8081/";
var server = new Server(url);

// Map the default hub url (/signalr)
server.MapHubs();

// Start the server
server.Start();

Console.WriteLine("Server running on {0}", url);

// Keep going until somebody hits 'x'
while (true)
{
ConsoleKeyInfo ki = Console.ReadKey(true);
if (ki.Key == ConsoleKey.X)
{
break;
}
}
}

public class MyHub : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);

}
}

}

客户:

class Program
{
static void Main(string[] args)
{
var connection = new HubConnection("http://localhost:8081/");

IHubProxy proxy = connection.CreateHubProxy("MyHub");

connection.Start().Wait();

proxy.On("addMessage", data => { Console.WriteLine("From Server: " + data); });

while (true)
{
proxy.Invoke("Send", Console.ReadLine());
}

}
}

enter image description here

PS。我将在下面的评论中将下载添加到这两个解决方案中。我相信你会没事的。

关于asp.net - SignalR 1.0alpha2 控制台服务器应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13514052/

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