首页 > 解决方案 > 当前应用程序配置不支持 WebSockets

问题描述

尝试使用 SignalR 时出现 websocket 错误。

完全错误:

当前应用程序配置中不支持 WebSockets 要启用此功能,请在 Web.config 中设置以下配置开关:
<system.web> </system.web> 有关详细信息,请参阅http://go.microsoft.com/fwlink/ ?LinkId=252465。.
在 System.Web.Util.SynchronizationContextUtil.ValidateMode(SynchronizationContextMode currentMode, SynchronizationContextMode requiredMode, String specificErrorMessage)
在 System.Web.HttpContext.AcceptWebSocketRequest(Func 2 userFunc, AspNetWebSocketOptions options) at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func2 回调)
在 Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(HostContext context, String groupsToken)
在 Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2,T3,TResult] (Func`4 func, T1 arg1, T2 arg2, T3 arg3)

我在 Stackoverflow 上查看过类似的帖子,但我的问题似乎有所不同。因为,我与 SignalR 建立了连接。

  <script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery.signalR-2.2.2.js"></script>
<script src="/signalr/hubs"></script>

<script type="text/javascript">

    $(function () {

        var asset1 = document.getElementById('MainContent_MainContent_Asset1Hidden').value;
        var asset2 = document.getElementById('MainContent_MainContent_Asset2Hidden').value;

        var socket = $.connection.marketHub;

        socket.client.refreshSellOrders = function (msg) {
            document.getElementById("MainContent_MainContent_sellOrders").innerHTML = msg;
        };

        socket.client.refreshBuyOrders = function (msg) {
            document.getElementById("MainContent_MainContent_buyOrders").innerHTML = msg;
        };

        socket.client.refreshCompletedOrders = function (msg) {
            document.getElementById("MainContent_MainContent_completedOrders").innerHTML = msg;
        };

        $.connection.hub.qs = { 'asset1': asset1, 'asset2': asset2 };
        $.connection.hub.start();
    });

</script>

我启用了 WebSockets。 配置

您可能需要的附加代码:

 public class MarketHub : Hub
{

    public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();


    static MarketHub()
    {
    }

    public override System.Threading.Tasks.Task OnConnected()
    {
        string mAsset1 = Context.QueryString["asset1"];
        string mAsset2 = Context.QueryString["asset2"];

        string body = "{0}/{1}";
        string roomName = string.Format(body, mAsset1, mAsset2);


        JoinRoom(roomName);

        return base.OnConnected();

    }
    public System.Threading.Tasks.Task JoinRoom(string roomName)
    {
        System.Threading.Tasks.Task result = Groups.Add(Context.ConnectionId, roomName);
        return result;
    }

}

启动时

protected void Application_Start(object sender, EventArgs e, IAppBuilder app)
    {
        app.MapSignalR();

        GlobalConfiguration.Configure(config =>
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/v2/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        });

    }

网页配置

<dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="1234525324532" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>

数据没问题。SignalR 得到正确的数据。它只是不会自动更新。

标签: c#websocketwebformssignalr

解决方案


最后,我意识到我安装了 SignalR 2.4.1 参考并且我使用的是 2.2.2 .js 文件

<script src="/Scripts/jquery.signalR-2.2.2.js"></script>

将其替换为

<script src="/Scripts/jquery.signalR-2.4.1.js"></script>

推荐阅读