- From: Anton Vayvod via GitHub <sysbot+gh@w3.org>
- Date: Fri, 05 Jun 2015 18:19:12 +0000
- To: public-secondscreen@w3.org
How about the following spec change for the multiple connections? It's
based on the assumption that only one controller can connect to the
presentation page at a time. If the page is slow handling each
controller, the UA can simply queue them and resolve the next call to
```getNextController``` when needed.
```idl
partial interface NavigatorPresentation {
Promise<PresentationSession> getNextController(); // replaces
navigator.presentation.session
}
```
Then a simple presentation page supporting one controller would need
to write something like this:
```javascript
navigator.presentation.getNextController().then(handleController);
var handleController = function(controller) {
controller.onstatechange = function(e) {
if (e.state == "connected") {
controller.onmessage = function(e) {
showMessage("Received message " + e.msg);
};
} else if (e.state == "disconnected") {
// wait for the controller to connect back
navigator.presentation.getNextController().then(handleController);
} else if (e.state == "terminated") {
// nothing to do, the page is being closed, for the sake
of completeness
}
};
});
```
And code for the page supporting multiple controllers would look like
this:
```javascript
// controllers
var mControllers = [];
// maximum number of controllers to join the presentation
var MAX_CONTROLLERS = 5;
// announce that the page is ready to become a presentation and
receive its first controller
// assume that only one controller can start the presentation or
connect to it at the time
navigator.presentation.getNextController().then(handleControllerAdded);
var handleControllerAdded = function(controller) {
showMessage("Controller added: " + controller.id);
// assert controller.state == "connecting";
controller.onstatechange = function(e) {
if (e.state == "connected") {
handleControllerConnection(this)
} else if (e.state == "disconnected" || e.state ==
"terminated") {
handleControllerDisconnection(this);
}
};
};
var handleControllerConnection = function(controller) {
if (mControllers.empty()) {
setUp();
}
mControllers.add(controller);
controller.onmessage = function(e) {
showMessage("Received message " + e.msg + " from " + this.id);
};
if (mControllers.length < MAX_CONTROLLERS) {
navigator.presentation.getNextController(handleController);
}
};
var handleControllerDisconnection = function(controller) {
mControllers.remove(controller);
if (mControllers.empty()) {
shutDown();
} else if (mControllers.length < MAX_CONTROLLERS) {
navigator.presentation.getNextController(handleController);
}
};
var setUp = function() {
showMessage("The presentation is connected");
};
var shutDown = function() {
showMessage("No controllers, please connect.");
navigator.presentation.getNextController(handleController);
};
```
The semantic of the method is as following:
1. If there's no active connection and no pending promise returned by
```getNextController()``` - the page is not ready to become a
presentation yet (relevant to issue #32).
2. If there's no active connection but a promise returned by
```getNextController()``` is pending, the page designates itself as a
presentation page and is ready to accept controllers connecting
(relevant to issue #32 too).
3. If there's an active connection but no pending promise returned by
```getNextController()``` (meaning one or more
```getNextController()``` promises have resolved before, at least one
of the controllers is in the connected state, the page doesn't want
more controllers), the presentation can't be connected to anymore and
can be replaced when something else starts a presentation to the same
screen (or starting the presentation can be rejected by the UA that
tried to connect).
4. If there's an active connection and a promise pending from a
previous ```getNextController()```, the page expects more controllers
to join and the promise will be resolved as soon as another controller
joins the presentation.
--
GitHub Notif of comment by avayvod
See
https://github.com/w3c/presentation-api/issues/19#issuecomment-109386548
Received on Friday, 5 June 2015 18:19:14 UTC