c# - 当前应用程序配置不支持 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(Func2 userFunc, AspNetWebSocketOptions options) at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func
2 回调)
在 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 得到正确的数据。它只是不会自动更新。
解决方案
最后,我意识到我安装了 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>
推荐阅读
- python - 如何表示用于神经网络的音频文件?
- excel - 将数据从一张纸转移到另一张纸
- python - 使用“tarfile”时跳过损坏的档案 (.tar.gz)
- python - 如何解决串行通信中的绘图问题?
- c++ - 不能将变量声明为类的抽象
- python - 为什么可以使用类型在 Python 上的 int 类型变量上存储 str 值
- ios - React-Native Audio 无法在 IOS 模拟器上播放(OSStatus 错误 2003334207)
- javascript - 使用Axios CDN在Vue Template中展示
- android - 如何使用 Android pjsua2 拨打电话
- laravel - 在 Laravel 中使用 guzzle 不会返回响应