SignalR作为WCF Web套接字服务

时间:2012-09-24 10:08:59

标签: signalr

是否可以将SignalR作为WCF websocket服务的一部分托管,而不是作为ASP.net网站的一部分。我知道将mesage从Web服务推送到signalR客户端,但是当从浏览器打开套接字连接时它是否也可能映射到web服务合同?

2 个答案:

答案 0 :(得分:5)

您可以自行托管SignalR服务器:

取自(https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs):

入门 要开始使用,请安装以下软件包:

Install-Package Microsoft.Owin.Hosting -pre
Install-Package Microsoft.Owin.Host.HttpListener -pre
Install-Package Microsoft.AspNet.SignalR.Owin -pre

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;

namespace SignalR.Hosting.Self.Samples
{
class Program
{
    static void Main(string[] args)
    {
        string url = "http://172.0.0.01:8080";

        using (WebApplication.Start<Startup>(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // This will map out to http://localhost:8080/signalr by default
        // This means a difference in the client connection.

        app.MapHubs();
    }
}

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

}

答案 1 :(得分:2)

您可以在任何.Net应用程序中托管SignalR集线器,例如:

public class Program
{
public static void Main(string[] args)
{
    // Connect to the service
    var hubConnection = new HubConnection("http://localhost/mysite");

    // Create a proxy to the chat service
    var chat = hubConnection.CreateProxy("chat");

    // Print the message when it comes in
    chat.On("addMessage", message => Console.WriteLine(message));

    // Start the connection
    hubConnection.Start().Wait();

    string line = null;
    while((line = Console.ReadLine()) != null)
    {
        // Send a message to the server
        chat.Invoke("Send", line).Wait();
    }
}
}

参考:https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

如果您有任何特定原因要使用WCF?您只能将服务编写为SignalR中心。