简体   繁体   中英

SignalR Self-Host only localhost javascript clients works

I have a SignalR self-hosted application with javascript (jquery) clients. It works well locally, but I haven't found a way to connect from a remote computer. I always get a Bad Request - Invalid Hostname / HTTP Error 400

I am running Visual as administrator, I have disabled the firewall, as you can see in the following code, I have set the server side url as url = "http://+:8081/"; and I have set my IP address in jquery connection and the reference to the auto-generated signalR hub script.

 $(function () { // Declare a proxy to reference the hub. $.connection.hub.url = "http://192.168.2.101:8081/signalr"; var con = $.connection.myHub; // Create a function that the hub can call to broadcast messages. var Nrobot = 1; con.client.broadcastMessage = function (Nrobot, message) { }; $.connection.hub.start({ jsonp: true }).done(function () { $('#btn_P06P07').mouseover(function () {con.server.send(Nrobot, "LT");}); $('#btn_P01P02').mouseover(function () {con.server.send(Nrobot, "LTI");}); $('#btn_P03P04').mouseover(function () {con.server.send(Nrobot, "T") ;}); $('#btn_P05P06').mouseover(function () {con.server.send(Nrobot, "TI") ;}); $('#btn_P00P03').mouseover(function () {con.server.send(Nrobot, "RT") ;}); $('#btn_P02P05').mouseover(function () {con.server.send(Nrobot, "RTI");}); //MiddleRow $('#btn_P04P07').mouseover(function () {con.server.send(Nrobot, "L");}); $('#btn_P12P15').mouseover(function () {con.server.send(Nrobot, "LI");}); $('#btn_P10P13').mouseover(function () {con.server.send(Nrobot, "C");}); $('#btn_P06P11').mouseover(function () {con.server.send(Nrobot, "R");}); $('#btn_P13P14').mouseover(function () {con.server.send(Nrobot, "RI");}); //BottomRow $('#btn_P11P12').mouseover(function () {con.server.send(Nrobot, "LB");}); $('#btn_P07P10').mouseover(function () {con.server.send(Nrobot, "LBI");}); $('#btn_P00P01').mouseover(function () {con.server.send(Nrobot, "B");}); $('#btn_P02P03').mouseover(function () {con.server.send(Nrobot, "BI");}); $('#btn_P04P05').mouseover(function () {con.server.send(Nrobot, "RB");}); $('#btn_P14P15').mouseover(function () {con.server.send(Nrobot, "RBI");}); $('#btn_oClaw').mousedown(function () { con.server.send(Nrobot, "OClaw"); }); $('#btn_cClaw').mousedown(function () { con.server.send(Nrobot, "CClaw"); }); $('#btn_rBoom').mousedown(function () { con.server.send(Nrobot, "RBoom"); }); $('#btn_lBoom').mousedown(function () { con.server.send(Nrobot, "LBoom"); }); $('#btn_lPancam').mousedown(function () { con.server.send(Nrobot, "LPancam"); }); $('#btn_rPancam').mousedown(function () { con.server.send(Nrobot, "RPancam"); }); $('#btn_rElbow').mousedown(function () { con.server.send(Nrobot, "RElbow"); }); $('#btn_lElbow').mousedown(function () { con.server.send(Nrobot, "LElbow"); }); $('#btn_lFuture').mousedown(function () { con.server.send(Nrobot, "LFuture"); }); $('#btn_rFuture').mousedown(function () { con.server.send(Nrobot, "RFuture"); }); $('#btn_oClaw').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_cClaw').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_rBoom').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_lBoom').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_lPancam').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_rPancam').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_rElbow').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_lElbow').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_lFuture').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#btn_rFuture').mouseup(function () { con.server.send(Nrobot, "C"); }); $('#Joystick').mouseleave(function () { con.server.send(Nrobot, "C"); }); $(window).mouseleave(function () { con.server.send(Nrobot, "C"); }); }); }); 
 <!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" href="robotCSS.css" /> </head> <body> <table id=Joystick> <tr> <td> <input type="button" class="LTledClass" name="P06P07" id="btn_P06P07" style="height:50px;width:50px" /> <input type="button" class="LTledClass" name="P01P02" id="btn_P01P02" style="height:25px;width:25px" /> </td> <td> <input type="button" class="TledClass" name="P03P04" id="btn_P03P04" style="height:50px;width:50px" /> <input type="button" class="TledClass" name="P05P06" id="btn_P05P06" style="height:25px;width:50px" /> </td> <td> <input type="button" class="RTledClass" name="P00P03" id="btn_P00P03" style="height:50px;width:50px" /> <input type="button" class="RTledClass" name="P02P05" id="btn_P02P05" style="height:25px;width:25px" /> </td> </tr> <tr> <td> <input type="button" class="LledClass" name="P04P07" id="btn_P04P07" style="height:50px;width:50px" /> <input type="button" class="LledClass" name="P12P15" id="btn_P12P15" style="height:50px;width:25px" /> </td> <td> <input type="button" class="ledClass" name="P10P13" id="btn_P10P13" style="height:50px;width:50px" /> </td> <td> <input type="button" class="RledClass" name="P06P11" id="btn_P06P11" style="height:50px;width:50px" /> <input type="button" class="RledClass" name="P13P14" id="btn_P13P14" style="height:50px;width:25px" /> </td> </tr> <tr> <td> <input type="button" class="LBledClass" name="P11P12" id="btn_P11P12" style="height:50px;width:50px" /> <input type="button" class="LBledClass" name="P07P10" id="btn_P07P10" style="height:25px;width:25px" /> </td> <td> <input type="button" class="BledClass" name="P00P01" id="btn_P00P01" style="height:50px;width:50px" /> <input type="button" class="BledClass" name="P02P03" id="btn_P02P03" style="height:25px;width:50px" /> </td> <td> <input type="button" class="RBledClass" name="P04P05" id="btn_P04P05" style="height:50px;width:50px" /> <input type="button" class="RBledClass" name="P14P15" id="btn_P14P15" style="height:25px;width:25px" /> </td> </tr> </table> <table id="RightPanel" width="100" height="150" bgcolor="red" border="0"> <tr> <td> <p class="t">Claw</p> </td> <td> <p class="t">Boom</p> </td> </tr> <tr> <td> <input type="button" class="BTN" value="Open" name="OClaw" id="btn_oClaw" style="height:20px;width:50px" /> </td> <td> <input type="button" class="BTN" value="Raise" name="RBoom" id="btn_rBoom" style="height:20px;width:50px" /> </td> </tr> <tr> <td> <input type="button" class="BTN" value="Close" name="CClaw" id="btn_cClaw" style="height:20px;width:50px" /> </td> <td> <input type="button" class="BTN" value="Lower" name="LBoom" id="btn_lBoom" style="height:20px;width:50px" /> </td> </tr> <tr> <td colspan="2"> <p class="t">PanCam</p> </td> </tr> <tr> <td> <input type="button" class="BTN" value="Left" name="LPancam" id="btn_lPancam" style="height:20px;width:50px" /> </td> <td> <input type="button" class="BTN" value="Right" name="RPancam" id="btn_rPancam" style="height:20px;width:50px" /> </td> </tr> <tr> <td> <p class="t">Elbow</p> </td> <td> <p class="t">Future</p> </td> </tr> <tr> <td> <input type="button" class="BTN" value="Raise" name="RElbow" id="btn_rElbow" style="height:20px;width:50px" /> </td> <td> <input type="button" class="BTN" value="Left" name="LFuture" id="btn_lFuture" style="height:20px;width:50px" /> </td> </tr> <tr> <td> <input type="button" class="BTN" value="Lower" name="LElbow" id="btn_lElbow" style="height:20px;width:50px" /> </td> <td> <input type="button" class="BTN" value="Right" name="RFuture" id="btn_rFuture" style="height:20px;width:50px" /> </td> </tr> </table> <ul id="Tmess"></ul> <!--Script references. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.6.4.min.js"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.0.3.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="http://192.168.2.101:8081/signalr/hubs"></script> <!--Add script to update the page and send messages.--> <script type="text/javascript" src="Scripts/Centauri.js"></script> </body> </html> 

    static void Main(string[] args)
    {
        // This will *ONLY* bind to localhost, if you want to bind to all addresses
        // use http://*:8080 to bind to all addresses. 
        // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
        // for more information.
        string url = "http://+:8081/";
        using (WebApp.Start(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //app.UseCors(CorsOptions.AllowAll);

        app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {                       
                    EnableJSONP = true                      
                };                    
                map.RunSignalR(hubConfiguration);
            });

        //app.MapSignalR();
        //app.MapSignalR(new HubConfiguration() {EnableJSONP=true});

    }
}

Nothing worked.

Thanks a lot,

Best Regards,

Ok,

I found the solution.

I needed to edit the applicationhost.config file, for some reason the change in the url string did not take effect.

            <site name="JavascriptClient" id="7">
            <application path="/" applicationPool="Clr4IntegratedAppPool">
                <virtualDirectory path="/" physicalPath="C:\Users\oloyau\OneDrive\Freelance\1 - Completed\NI USB6501\CentauriBotServer\BotSeverSignalR\C#\JavascriptClient" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:31072:**localhost**" />
            </bindings>
        </site>

By changing the localhost part in the binding protocol parameter to

:31072:

It's italic but it is actually surrounded by * , I don't know how to show them instead of italic formatting, sorry.

Now it works from other computers on my network.

Hope this could help the next one in trouble...

Thanks

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