我的Scenerio是我需要一个SignalR自托管WCF服务,它响应并向来自Winform或WPF的所有连接用户发送消息。
我尝试了很多如下:
namespace SignalRServiceClass { [ServiceContract] public interface ISignalRServiceClass { [OperationContract] string GetsMessage(string name); [OperationContract] void Configuration(IAppBuilder app); [OperationContract] void Send(string name, string message); } } namespace SignalRServiceClass { public class SignalRServiceClass : ISignalRServiceClass { public string GetsMessage(string name) { return "Message From Service " + name + "!"; } } } namespace SignalRServiceClass { class ClassHub : Hub { public void Send(string name, string message) { Clients.All.addMessage(name, message); } } } namespace SignalRServiceClass { class Startup { public void Configuration(IAppBuilder app) { // app.UseCors(CorsOptions.AllowAll); // app.MapSignalR(); app.Map("/signalr", map => { map.UseCors(CorsOptions.AllowAll); var hubConfiguration= new HubConfiguration { EnableDetailedErrors=true, EnableJSONP= true }; map.RunSignalR(hubConfiguration); }); } } }
其次是Winform Client。我在这里很困惑,如何在这里管理客户端代码,但我把一些代码用于测试,如下所示。
private void button1_Click(object sender, EventArgs e) { //MessageBox.Show(test.GetsMessage("This is the Test Message")); var hubConnection = new HubConnection("http://localhost:50172/"); var serverHub = hubConnection.CreateHubProxy("MessageRecievingHub"); serverHub.On("broadCastToClients", message => MessageBox.Show(message)); hubConnection.Start().Wait(); }
请以这种方式指导我。 我们将非常感谢您的帮助。我尝试了很多但是徒劳无功。
非常感谢。
答案 0 :(得分:3)
你不需要SignalR,你需要XSockets WCF sample
答案 1 :(得分:1)
SignalR和WCF不以这种方式互操作,并且实际上并不需要。如果您正在使用SignalR,则没有理由使用WCF-您可以在IIS上发布您的集线器或自托管(请参阅asp.net/signalr上的入门教程和自主教程),并使用它连接到它桌面或JavaScript / HTML客户端。
答案 2 :(得分:0)
您可以轻松创建.NET客户端应用程序以与SignalR服务器通信 - 下面是一个简单的WinForm .NET C#客户端,用于发送和接收SignalR消息:
namespace SignalrTestClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
async void Form1_Load(object sender, EventArgs e)
{
var hubConnection = new HubConnection("http://localhost:8080/");
IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
await hubConnection.Start();
hubProxy.On("addMessage", message => onData(message));
await hubProxy.Invoke("Send", "Hello SignalR Server!");
}
private void onData(string msg)
{
Console.WriteLine(msg);
}
}
}
在SignalR服务器中,您只需要以下集线器类:
public class MyHub : Hub
{
public void Send(string message)
{
Console.WriteLine("Received a message from a client");
if (message.Contains("Hello")) {
Clients.All.addMessage("Well hello there client!");
}
}
}
也可以为SignalR
创建C ++客户端