Universal App 中基于“StreamSocketListener”的服务器

`StreamSocketListener`-based server in Universal App

众所周知,Windows 8.1 和 Windows Phone 8.1 对第三方应用程序的后台活动进行了很多限制。例如,如果不使用 DisplayRequest.

保持屏幕始终打开,就不可能在 Store App 中开发类似服务器的功能(例如 BitTorrent)

根据最新的文档,我感觉可以使用 EnableTransferOwnership of StreamSocketListener to delegate the usual request processing code to a background task. The problem is that I don't know how IBackgroundTask should work in this case. Normally, they are activated by some conditions/triggers such as "Internet becomes available". The only sensible trigger in this case seems to be ControlChannelTrigger 方法,但显然,文档暗示它不能与 StreamSocketListener 一起使用,因为应用程序需要是那个谁创建并注册了 StreamSocket,而不是等待某个连接的客户端创建套接字。

现在是否可以在 Windows 10 通用应用程序平台中实施基于 StreamSocketListener 的服务器?如果是,我该怎么做?

使用它来激活套接字上的后台任务activity,例如在应用程序未激活时接收数据,例如:

var socketTaskBuilder = new BackgroundTaskBuilder();
socketTaskBuilder.Name = "SocketActivityBackgroundTask";
socketTaskBuilder.TaskEntryPoint = "SocketActivityBackgroundTask.SocketActivityTask";
var trigger = new SocketActivityTrigger();
socketTaskBuilder.SetTrigger(trigger);
var task = socketTaskBuilder.Register();

socket = new StreamSocket();
socket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.Wake);

有关完整示例,请查看 Socket Activity sample or the documentation