SignalR Self Host only localhost javascript客户端可以工作



我有一个带有javascript(jquery)客户端的SignalR自托管应用程序。它在本地运行良好,但我还没有找到从远程计算机连接的方法。我总是收到一个错误的请求-无效的主机名/HTTP错误400

我以管理员身份运行Visual,我禁用了防火墙,正如您在下面的代码中看到的,我将服务器端url设置为url = "http://+:8081/";,并在jquery连接中设置了我的IP地址和对自动生成的signalR hub脚本的引用。

$(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});
    }
}

什么都没用。

非常感谢

此致,

好的,

我找到了解决办法。

我需要编辑applicationhost.config文件,由于某些原因,url字符串中的更改没有生效。

            <site name="JavascriptClient" id="7">
            <application path="/" applicationPool="Clr4IntegratedAppPool">
                <virtualDirectory path="/" physicalPath="C:UsersoloyauOneDriveFreelance1 - CompletedNI USB6501CentauriBotServerBotSeverSignalRC#JavascriptClient" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:31072:**localhost**" />
            </bindings>
        </site>

通过将绑定协议参数中的localhost部分更改为

:31072:

它是斜体,但实际上被*包围了,对不起,我不知道如何显示它们而不是斜体格式。

现在它可以在我网络上的其他计算机上工作。

希望这能帮助下一个陷入困境的人。。。

感谢

最新更新