我在尝试使用 SignalR 时遇到 websocket 错误。
完全错误:
WebSockets is unsupported in the current application configuration To
enable this, set the following configuration switch in Web.config:
<system.web>
<httpRuntime targetFramework="4.5" />
</system.web>
For more information, see http://go.microsoft.com/fwlink/?LinkId=252465..
at System.Web.Util.SynchronizationContextUtil.ValidateMode(SynchronizationContextMode currentMode,
SynchronizationContextMode requiredMode, String specificErrorMessage)
at System.Web.HttpContext.AcceptWebSocketRequest(Func`2 userFunc, AspNetWebSocketOptions options)
at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func`2 callback)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(HostContext context,
String groupsToken)
at 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>
我启用了 WebSocket。
您可能需要的其他代码:
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 }
);
});
}
Web.config
<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>