-
Notifications
You must be signed in to change notification settings - Fork 7
Examples
In order to get a reference to the API, you will have to discover the API. You can use the dashboard to allow the user pick the service for you, but you can also do this manually in code (see the API description for the dashboard use).
##Service Discovery This is the list of URIs used to declare this API's features, for use in the widget config.xml and as identifier for service type in service discovery functionality. For each URI, the list of functions covered is provided.
webinos.discovery.findServices(new ServiceType('http://webinos.org/api/app2app'),
{onFound: function (service) {
//Stores the Service in a local variable
foundService = service
}
From this point we will refer to the found service as if it was stored in the foundService variable.
##Bind Before using a service, you need to bind to it. Binding is similar on all services. The following code shows how to bind to the App2App API. Bind to the found and select a service and use. This is required to make calls possible on the webinos service that we created.
service.bindService({
onBind: function () {
app2app = service;
}
});
Create channel and ask user to accept connections, app2appService is the app2app messaging service, discovered through the webinos Discovery API, that has been selected by the user. Callback invoked when a client want to connect to the channel. we allow all clients to connect (we could also for example check some application- specific information in the request.requestInfo to make a decision)
app2app.createChannel(
config,
function(request) {
return confirm("Do you allow the client to connect?");
},
// callback invoked to receive messages
function(message) {
alert("The channel creator received a message: " + message.contents);
},
function(channelProxy) {
creatorChannelProxy = channelProxy;
},
function(error) {
alert("Could not create channel: " + error.message);
}
);
});
Send message to all connected clients (in our example this is only one)
clientChannelProxy.send(
"client.ping",
// callback invoked when the message is accepted for processing
function(success) {
// ok, but no action needed in our example
},
function(error) {
alert("Could not send message: " + error.message);
}
);
});
The namespace to search for (can include a wildcard "*" instead of "example" to search for all channels with prefix "org-webinos")No other zones need to be searched, only its own personal zone. Callback invoked on each channel found, we expect it to be called at most once because we do not use a wildcard. we directly request to connect to the channel callback invoked when the search query is accepted for processing is ok, but no action needed in our example
app2app.searchForChannels(
$("#txtNamespace").val(),
[],
function(channelProxy) {
connectToChannel(channelProxy);
},
function(success) {
},
function(error) {
alert("Could not search for channel: " + error.message);
}
);
});
});
We can include application-specific information to the connect request. callback invoked to receive messages, only after successful connect. we directly reply to the message, and here we send a message to the sender only (i.e. the creator in this example). Callback invoked when the client is successfully connected (i.e. authorized by the creator)
function connectToChannel(channelProxy) {
var requestInfo = {};
channelProxy.connect(
requestInfo,
function(message) {
alert("Client received message from creator: " + message.contents);
sendMessageTo(message.from, "client.pong", channelProxy);
},
function(success) {
// make the proxy available now that we are successfully connected
clientChannelProxy = channelProxy;
},
function(error) {
alert("Could not connect to channel: " + error.message);
}
);
}
Send message to a single client only alternatively you can send to all clients on the channel using channelProxy.send, callback invoked when the message is accepted for processing
function sendMessageTo(client, message, channelProxy) {
channelProxy.sendTo(
client,
message,
function(success) {
// ok, but no action needed in our example
},
function(error) {
alert("Could not send message: " + error.message);
}
);