WinForm中的SignalR聊天应用程序与远程客户端

时间:2013-12-09 17:08:15

标签: signalr

我是signalR的新手,我曾尝试和学习github等不同的网站等。

但我无法找到解决问题的方法......现在我感到困惑......

我的问题是:

我在Winform中使用Web服务和集中式数据库开发了一个聊天应用程序,它在不同的国家和一个组织的不同分支机构中运行良好。

但我想将Chat App转换为SignalR以实现更高的效率,但我无法理解,如何在SignalR中实现。因为在一个解决方案中Web上的SignalR的所有教程。

像Web,Console或WinRT之间相互通信但是它们在一个解决方案中但在我的场景中我无法将服务或网页放在WinForm应用程序中。

请以这种方式帮助我。

1 个答案:

答案 0 :(得分:7)

您需要做的是使用SignalR for .NET客户端。假设您正在使用Visual Studio,请使用NuGet将其带入您的项目。

您通常需要导入以下内容:

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

假设您正在关注网络上的大部分教程,您可以通过以下方式进行连接:

 public IHubProxy Proxy { get; set; }
 public HubConnection Connection { get; set; }

您还需要像这样设置连接:

 public string Host = "http://YourSignalRChatAppLocationOnAzureOrLocally.cloudapp.net/"; 
 Connection = new HubConnection(Host);
 //Assuming your SignalR hub is also called ChatHub (If you followed most tutorials it will be)
 Proxy = Connection.CreateHubProxy("ChatHub");

这部分需要处于异步功能中:

 //If you are passing an object back and fourth otherwise String is fine
 Proxy.On<ChatMessage>("Send", hello => OnSendData("Recieved send " + hello.Username + " " + hello.Content));
 await Connection.Start();

以下链接提供了更多资料,这个人在控制台应用,WPF应用和网络客户端上运行,因此您可以看到差异。

Standard tutorial on how to make the web server.

SIGNALR MESSAGING WITH CONSOLE SERVER AND CLIENT, WEB CLIENT, WPF CLIENT

相关问题