【发布时间】:2014-12-23 02:14:52
【问题描述】:
是否有任何示例项目展示了如何将 SignalR 与 WinJS Windows 8.1 项目一起使用?不是 windows phone 8.1,只是使用 WinJS 的 Windows 8.1。
我正在专门寻找一个示例项目或代码来展示如何将消息推送到所有 WinJS 客户端。
我已生成 hubs.js 文件并将其包含在我的 winjs 项目中。我将 $.connection.hub.url 设置为正确的 url。我可以在提琴手中看到连接已启动。
但是,由于某种原因,当我运行代码向所有客户端广播消息时,什么也没有发生。就好像服务器不认为有任何客户端连接,所以它不广播任何东西。
这是我的 Web Api 项目中的服务器代码:
Startup.cs
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the cors middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true,
EnableJavaScriptProxies = false
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch is already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
CommandsHub.cs
[HubName("remoteCommands")]
public class CommandsHub : Hub
{
}
ClientCommand.cs
public class ClientCommand
{
public string ClientType { get; set; }
public string CommandName { get; set; }
public StringDictionary CommandParameters { get; set; }
}
CommandsController.cs(用于允许管理员向客户端广播的 ApiController)
// POST api/values
public void Post([FromBody]ClientCommand command)
{
var cmd = command ?? Notifier.GetTestMessageCommand();
var notifier = new Notifier();
notifier.PushCommand(cmd);
}
Notifier.cs
public class Notifier
{
public Notifier()
{
_context = GlobalHost.ConnectionManager.GetHubContext<CommandsHub>();
}
public static ClientCommand GetTestMessageCommand()
{
var parameters = new StringDictionary();
parameters.Add("title", "Message Title");
parameters.Add("body", "Message Body");
return new ClientCommand
{
ClientType = "XboxOne",
CommandName = "ShowMessage",
CommandParameters = parameters
};
}
private IHubContext _context;
public void PushCommand(ClientCommand command)
{
if (_context == null)
{
_context = GlobalHost.ConnectionManager.GetHubContext<CommandsHub>();
}
_context.Clients.All.executeRemoteCommand(command);
}
}
以下行被执行,但似乎没有发生任何事情。 Fiddler 不显示任何网络活动,并且具有匹配名称的客户端方法不会运行。
_context.Clients.All.executeRemoteCommand(command);
这是来自客户端项目的代码:
通过 Nuget,我安装了 Microsoft ASP.NET SignalR Javascript 客户端
我也在用命令
signalr.exe ghp /o:scripts\\hubs.js
生成我的 hubs.js 文件,并将其包含在 WinJS 项目中。
我在 default.html 中添加了对以下内容的引用:
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.1.2.js"></script>
<script src="js/hubs.js" defer="defer"></script>
<script src="js/SignalRConfig.js" defer="defer"></script>
SignalRConfig.js
(函数信号RInit() { “使用严格”;
WinJS.Namespace.define("Starz", {
RemoteCommands: WinJS.Class.define(
function ctor() {
$.connection.url = "http://starzsignlalr.test:19046/signalr"
$.connection.hub.url = "http://starzsignlalr.test:19046/signalr"; //TODO: Change "http://starzsignlalr.test:19046/" to a valid website host name setup on your dev box that is running the SignalRMessageService web project.
$.connection.hub.start().done(this._init);
},
{
_init: function () {
var hub = $.connection.remoteCommands;
hub.client.executeRemoteCommand = Starz.RemoteCommands.executeRemoteCommand;
}
},
{
//add static properties and methods here.
executeRemoteCommand: function (command) {
var clientId = command.clientId;
var commandName = command.commandName;
var commandParameters = command.commandParameters;
}
}),
});
})();
在default.js中,在onactivated事件之后,我调用:
var pushSystem = new Starz.RemoteCommands();
这会初始化 SignalR 连接。
【问题讨论】:
-
要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。我很确定 out there 将构成一个场外资源。
-
显示服务器和客户端代码以及 fiddler 输出,突出显示问题,您可能会得到更多帮助。
-
好的,我用你需要的所有代码更新了问题,以重新创建我所拥有的。