【问题标题】:SignalR Self-Host only localhost javascript clients worksSignalR Self-Host only localhost javascript 客户端有效
【发布时间】:2016-05-05 02:42:15
【问题描述】:

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

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

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

    }
}

没有任何效果。

非常感谢,

最好的问候,

【问题讨论】:

  • 我猜是string url = "http://+:8081/"; using(WebApp.Start(url))的问题。我认为它仍然只听localhost
  • 您好 MaKCbimKo,感谢您的快速回答,那我该如何更改呢?
  • 您是否尝试过使用“http://*:8081/”而不是“http://+:8081/”?
  • 是的,我也是。
  • 如果你想在这里准确地指定你的IP地址怎么办?喜欢http://192.168.2.101:8081

标签: c# jquery signalr


【解决方案1】:

好的,

我找到了解决办法。

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

            <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>

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

:31072:

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

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

希望这可以帮助下一个遇到麻烦的人......

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多