我想将数据发送到与我的"someHub"连接的控制台应用程序.我尝试按照链接中的示例所描述但没有结果.服务器端代码:
[HubName("somehub")]
public class SomeHub : Hub
{
public override Task OnConnected()
{
//Here I want to send "hello" on my sonsole application
Clients.Caller.sendSomeData("hello");
return base.OnConnected();
}
}
Run Code Online (Sandbox Code Playgroud)
客户端代码:
public class Provider
{
protected HubConnection Connection;
private IHubProxy _someHub;
public Provider()
{
Connection = new HubConnection("http://localhost:4702/");
_someHub = Connection.CreateHubProxy("somehub");
Init();
}
private void Init()
{
_someHub.On<string>("sendSomeData", s =>
{
//This code is not reachable
Console.WriteLine("Some data from server({0})", s);
});
Connection.Start().Wait();
}
}
Run Code Online (Sandbox Code Playgroud)
实现这个的最佳解决方案是什么?为什么我无法调用客户端方法?
我有一个来自Hub的Chat类.我想知道他们是否可以通过URLHelper构建URL,如:Url.Action("action","Controller").
因为我可以从2个抽象类(Hub,Controller)派生类我不知道是否其他方式构建完整的URls而不是硬代码.
我正在尝试调试SignalR连接问题,虽然我知道如何启用客户端日志记录,但我不知道如何启用服务器端日志记录.它似乎没有记录任何东西到控制台.
我在HubConfiguration中尝试使用EnableDetailedErrors = true,但似乎没有做任何事情.
我是SignalR的新手,阅读API并玩弄它.对Hub及其Context有点困惑.
也就是说,Hub.Context不是HubContext.
HubContext 我可以得到 GlobalHost.ConnectionManager.GetHubContext<THub>()
并Hub.Context给我一个HubCallerContext我不知道如何使用.
他们的关系是什么?我怎么能得到HubContext from Hub或Hub from HubContext?
我正在用一个简单的博客发布者试用signalR,但我不确定实现它的最佳实践。
第一个解决方案:让客户端调用服务器上的post方法,后者将依次将数据提供给相关的Web api或其他上传数据的方法。我打算在这里使用已经打开的连接。
public class BlogHub : Hub
{
public void Post(string text)
{
//Internal Webapi call / other method of DB Update.
Clients.All.BroadcastPost(text);
}
}
Run Code Online (Sandbox Code Playgroud)
第二种解决方案:让客户端对Web api进行ajax调用,然后再调用post方法并将其广播回客户端。
public void PostPost(string text) //May have to call this method something different...
{
db.posts.add(new PostModel(text));
db.SaveChanges();
Post(string Text);
}
Run Code Online (Sandbox Code Playgroud)
第三种解决方案:我没想到过的另一种(可能更好)的方法。
第四种解决方案:我可怕地滥用应如何使用signalR。
提前致谢!
我刚刚开始使用SignalR,并为SignalR创建了一个自定义解析器,因此我可以使用Castle Windsor通过集线器构造器注入依赖项.
我有点假设我只需要注册依赖项,但我发现在我的应用程序工作之前还必须自己注册集线器.这是预期的吗?如果是这样,我应该用什么寿命用于集线器?
我正在使用以下代码创建Signal R自托管服务器:
internal class Config
{
internal static string serverurl = null;
internal static Microsoft.AspNet.SignalR.HubConfiguration hubconfiguration = null;
internal static SignalRHub Hub { get; set; }
internal static void StartServer()
{
serverurl = "http://localhost:8080";
// In this method, a web application of type Startup is started at the specified URL (http://localhost:8080).
{
Microsoft.Owin.Hosting.WebApp.Start<Startup>(serverurl);
Log.AddMessage("Server running on " + serverurl);
}
catch(Exception ex)
{
Log.AddMessage("An error occurred when starting Server: " + ex);
}
}
}
class Startup
{
// the …Run Code Online (Sandbox Code Playgroud) 我有一个Hub类,它有一个长时间运行的方法,我需要在它工作时显示一个进度条.
我读过这篇文章,我认为可以在异步方法中使用IProgress接口来发送长时间运行的操作状态.
我写了一个像这样的方法:
public async Task<string> GetServerTime(IProgress<int> prog)
{
await Task.Run(() => {
for (int i = 0; i < 10; i++)
{
prog.Report(i * 10);
System.Threading.Thread.Sleep(200);
}
});
return DateTime.Now.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我尝试调用这样的方法:
var appHub = $.connection.appHub;
$.connection.hub.start();
appHub.server.getServerTime()
.done(function (time) {
alert(time);
});
Run Code Online (Sandbox Code Playgroud)
但我不知道如何获得进度报告.
我最近听说过SignalR,我想在ASP vNext中实现它.
我已经尝试了一些ASP MVC 5的指南,但我无法让它工作.有人能指出我正确的方向吗?
我有一个带静态方法的集线器,它向当前所有用户广播通知.
public class NotificationHub : Hub
{
private static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
[HubMethodName("sendNotifications")]
public static void SendNotifications()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All.updateNotifications();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要从前端javascript中获取一些文本框输入,例如10038,这是userID并将其发送到服务器类,例如
public class NotificationRepository
{
private readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Notifications> GetNotifications(int userID)
{
var notifications = new List<Notifications>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(String.Format(@"SELECT [ID], [UserID], [Message] FROM [dbo].[Notification] WHERE [UserID] = {0}", userID), connection))
{
command.Notification = null;
var dependency = new …Run Code Online (Sandbox Code Playgroud)