根据标题,SignalR将是网页中使用的一般Ajax(例如jQuery Ajax)更新的合适替代品.
谢谢
在SignalR Hub类中,您可以呼叫Context.ConnectionId用户.我希望将这些存储Dictionary<string, string>在一起,以便将用户连接在一起.将其他用户的clientid返回给用户的客户端是否存在风险或安全漏洞?
我有以下JS工作:
var chat = $.connection.appHub;
Run Code Online (Sandbox Code Playgroud)
我的应用程序有一个集线器,AppHub可处理两种类型的通知 - Chat和Other.我正在使用单个集线器,因为我需要始终访问所有连接.
我需要能够OnConnected通过以下内容告诉它是哪种类型:
[Authorize]
public class AppHub : Hub {
private readonly static ConnectionMapping<string> _chatConnections =
new ConnectionMapping<string>();
private readonly static ConnectionMapping<string> _navbarConnections =
new ConnectionMapping<string>();
public override Task OnConnected(bool isChat) { // here
string user = Context.User.Identity.Name;
if (isChat){
_chatConnections.Add(user, Context.ConnectionId);
_navbarConnections.Add(user, Context.ConnectionId);
} else{
_navbarConnections.Add(user, Context.ConnectionId);
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法最好是这样的:
var chat = $.connection.appHub(true);
Run Code Online (Sandbox Code Playgroud)
如何从javascript将该参数传递到集线器?
更新:
发信息:
// will have another for OtherMessage
public void …Run Code Online (Sandbox Code Playgroud) 我有很多依赖于HttpContext.Current的代码,我注意到来自SignalR集线器的请求有HttpContext.Current == null,所以我的代码断了,例如:
HttpContext.Current.Request.IsAuthenticated
Run Code Online (Sandbox Code Playgroud)
所以我想出了以下内容:
public static class UnifiedHttpContext
{
private static HubCallerContext SignalRContext { get; set; }
private static int SignalRUserId
{
get { return WebSecurity.GetUserId(SignalRContext.User.Identity.Name); }
}
private static bool IsSignalRRequest
{
get { return SignalRContext != null; }
}
public static void SetSignalRContext(HubCallerContext context)
{
SignalRContext = context;
}
public static bool IsAuthenticated
{
get
{
if (!IsSignalRRequest)
{
return System.Web.HttpContext.Current.Request.IsAuthenticated;
}
else
{
return SignalRContext.User.Identity.IsAuthenticated;
}
}
}
public static int UserId
{
get …Run Code Online (Sandbox Code Playgroud) 我开发了一个基于ASP.NET 4.5和Owin的示例signalR应用程序,
我在IIS 7.5上托管了该应用程序
一切都很好,但我怎么能处理Owin的异常?
请考虑以下代码:
[HubName("SampleHub")]
public class SampleHub : Hub
{
public SampleHub()
{
throw new InvalidOperationException("?!");
}
}
Run Code Online (Sandbox Code Playgroud)
这个异常不会调用Application_Error(这是我的问题)
我在哪里可以获得owin的所有异常用于日志记录和调试目的
应用程序错误 ?
我对这样的事情不感兴趣:
app.UseErrorPage(new ErrorPageOptions()
{
ShowCookies = true,
ShowEnvironment = true,
ShowExceptionDetails = true,
ShowHeaders = true,
ShowQuery = true,
ShowSourceCode = true
});
Run Code Online (Sandbox Code Playgroud)
这对于高级场景来说完全没用,比如asp.net web api和asp.net mvc
用于覆盖目的的OnException方法的动作过滤器要好得多.
提前致谢.
我在我的应用程序中有套接字通知,当我在本地运行时工作得很好,但是当我部署到我的VM时,它会抱怨给出以下消息.
Error: Error during negotiation request.
at Object.signalR._.error (jquery.signalR.js:178)
at signalR.fn.signalR.start.onFailed (jquery.signalR.js:644)
at Object.signalR.fn.signalR.start.connection._.negotiateRequest.signalR.transports._logic.ajax.error (jquery.signalR.js:664)
at n.Callbacks.j (jquery.js:3094)
at Object.n.Callbacks.k.fireWith [as rejectWith] (jquery.js:3206)
at x (jquery.js:8261)
at XMLHttpRequest.n.ajaxTransport.k.cors.a.crossDomain.send.b (jquery.js:8600)
Run Code Online (Sandbox Code Playgroud)
它跨越域名,但我相信我已经超越了CORS问题,但我可能错了.
编辑
以下是我知道会被问到的其他信息.
请求:
GET http://path.to.my.vm/api/notifications/emmit/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22testemitter%22%7D%5D&_=1432140840826 HTTP/1.1
Host: path.to.my.vm
Connection: keep-alive
Accept: text/plain, */*; q=0.01
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,fr;q=0.8,en-US;q=0.6
Run Code Online (Sandbox Code Playgroud)
响应:
HTTP/1.1 500 Internal Server Error
Date: Wed, 20 May 2015 …Run Code Online (Sandbox Code Playgroud) 我的Startup.SignalR.cs中有以下代码:
using Microsoft.AspNet.SignalR;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin
{
public partial class Startup
{
public void ConfigureSignalR(IAppBuilder app)
{
var Config = new HubConfiguration()
{
EnableDetailedErrors = false,
Resolver = new DefaultDependencyResolver()
};
#if DEBUG
Config.EnableDetailedErrors = true;
#endif
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(600);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在应用程序的初始加载期间,一切都很顺利,但是当允许线程休眠(离开约10分钟)时,AppPool被回收并且线程卡在此行上:
app.MapSignalR();
Run Code Online (Sandbox Code Playgroud)
IDE被绿色箭头和This is the next statement to execute when this thread …
我试图了解如何最好地设计基于IIS/ASP.NET的websocket应用程序,特别是关于并发限制.
我已经阅读了有关"并发Websocket连接"的IIS/ASP.NET的所有文献以及如何调整各种值 - 但是,在谈到websockets时,"并发"的定义是什么?如果我打开了一个websocket并且它处于空闲状态,那是"使用"连接吗?空闲的websockets是否计入连接使用总数,或仅在发送/接收消息时计算?
我希望在任何时候都能打开一个非常高(100个)的网页数量,但是会发送很少的消息,也许每分钟几个,它们将永远是服务器 - >客户端(并且一个,特定的客户,而不是广播).这种安排是否/应该引导我进入任何特定的实施路线?
似乎SignalR集线器可能过度,我不需要不支持websockets的客户端的后备,我只需要维护每个客户端连接的句柄,这样当我的系统"决定"向特定客户端发送消息时,它可以适当地路由它.
我正在引用的文档:
谢谢
我正在使用带有Signalr 1.0.0软件包的VS2012"Fall"更新.调用服务器端功能正常.但是不调用客户端功能.当onBroadcastMessage()调用(见下文)时似乎没有任何事情发生.
问题:
谢谢!
服务器代码:
using Microsoft.AspNet.SignalR;
namespace KPMain
{
public class RealtimeConnectionHub : Hub
{
public void BroadcastMessage(string name, string message) {
Clients.All.onBroadcastMessage(name, message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
路线登记:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes) {
HubConfiguration hubConfig = new HubConfiguration();
#if DEBUG
hubConfig.EnableDetailedErrors = true;
#endif
routes.MapHubs(hubConfig);
...
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码(简化):
var rtcom = new RealtimeConnection();
rtcom.init({debug: true}, function () {
rtcom.subscribe(function (sender, message) {
if (message) {
alert("message");
}
});
}); …Run Code Online (Sandbox Code Playgroud) 我正在使用带有表单身份验证的MVC4 C#Web应用程序的SignalR 1.我在JavaScript的布局页面中有一个代码:
$(documnet).ready(function(){
connect to hub code ...
})
Run Code Online (Sandbox Code Playgroud)
我想断开用户从集线器断开连接并在他登录后再次开始连接并验证确定.我想从我的帐户控制器和方法中的服务器端执行此操作:
public ActionResult LogOn(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
....here , disconnect from hub
....to make the user reconnect
}
Run Code Online (Sandbox Code Playgroud)
我想这样做的原因是,如果用户在登录后更改为经过身份验证并且连接仍然存在,则SignalR会抛出错误.错误是:
连接ID格式不正确.
signalr ×10
asp.net ×3
c# ×3
signalr-hub ×3
asp.net-mvc ×2
ajax ×1
httpcontext ×1
jquery ×1
owin ×1
websocket ×1