简体   繁体   中英

Self-Host SignalR and Local Http Server on the same port

I want to host a Self-Host SignalR process locally on port 80 and having a local http directory hosted on the same port (serving files from a certain directory, mapping the directory structure to HTTP requests).

For example accessing localhost:80/index.html will return a html file located in a certain directory, that also communicates with the SignalR process on the same port , because having different ports will cause an error in chrome browser due to same origin policy .

Any idea how can I achieve it? since I cannot bind the same port with two different applications.

As long as we're talking about SignalR 2.x and you can serve your other content via WebAPI, this is possible since they both use OWIN. With MVC, things are more complicated .

To run SignalR and WebAPI on the same port, you'd have to install Microsoft.AspNet.WebApi.OwinSelfHost in your SignalR applilcation and modify the Owin.IAppBuilder to use Web API:

public static class Startup
{
    public static void Configuration(IAppBuilder app)
    {
        var httpConfig = new HttpConfiguration();
        httpConfig.Routes.MapHttpRoute("Default", "api/{controller}/{id}",
                                       new {id = RouteParameter.Optional});
        app.UseWebApi(httpConfig);

        app.MapSignalR();
    }
}

Note that you could also use CORS to get around that browser issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM