Skip to content

Commit

Permalink
added pubsub support for gadgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Hofmann committed Jul 25, 2011
1 parent fa7c1e9 commit ce16ce4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
42 changes: 42 additions & 0 deletions gadgets/sample-pubsub-publisher.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<Module>
<ModulePrefs title="Sample PubSub Publisher"
height="250">
<Require feature="pubsub"/>
</ModulePrefs>
<Content type="html">
<![CDATA[
<script>
function publish() {
var message = Math.random();
gadgets.pubsub.publish("random-number", message);
document.getElementById("output").innerHTML = message;
}
</script>
<div>
<input type="button" value="Publish a random number" onclick="publish()"/>
</div>
<div id="output">
</div>
]]>
</Content>
</Module>
52 changes: 52 additions & 0 deletions gadgets/sample-pubsub-subscriber.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-->
<Module>
<ModulePrefs title="Sample PubSub Subscriber"
height="250">
<Require feature="pubsub"/>
</ModulePrefs>
<Content type="html">
<![CDATA[
<script>
function callback(sender, message) {
document.getElementById("output").innerHTML =
"message : " + gadgets.util.escapeString(message + "") + "<br/>" +
"sender : " + gadgets.util.escapeString(sender);
}
function subscribe() {
gadgets.pubsub.subscribe("random-number", callback);
}
function unsubscribe() {
gadgets.pubsub.unsubscribe("random-number");
document.getElementById("output").innerHTML = "";
}
</script>
<div>
<input type="button" value="Subscribe" onclick="subscribe()"/>
<input type="button" value="Unsubscribe" onclick="unsubscribe()"/>
</div>
<div id="output">
</div>
]]>
</Content>
</Module>
25 changes: 24 additions & 1 deletion js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ var oauth2 = {
var container = {};

container.gadgetSpecUrls = [
'http://localhost:8062/statusnet_js_client/gadgets/hello_world.xml'
'http://localhost:8062/statusnet_js_client/gadgets/hello_world.xml',
'http://localhost:8062/statusnet_js_client/gadgets/sample-pubsub-publisher.xml',
'http://localhost:8062/statusnet_js_client/gadgets/sample-pubsub-subscriber.xml'
];

container.LayoutManager = function() {
Expand All @@ -150,6 +152,27 @@ container.LayoutManager.prototype.getGadgetChrome = function(gadget) {
};

container.init = function() {
gadgets.pubsubrouter.init(function(id) {
var gadgetId = shindig.container.gadgetService.getGadgetIdFromModuleId(id);
var gadget = shindig.container.getGadget(gadgetId);
return gadget.specUrl;
}, {
onSubscribe: function(sender, channel) {
console.log(sender + " subscribes to channel '" + channel + "'");
// return true to reject the request.
return false;
},
onUnsubscribe: function(sender, channel) {
console.log(sender + " unsubscribes from channel '" + channel + "'");
// return true to reject the request.
return false;
},
onPublish: function(sender, channel, message) {
console.log(sender + " publishes '" + message + "' to channel '" + channel + "'");
// return true to reject the request.
return false;
}
});
shindig.container.layoutManager = new container.LayoutManager();
};

Expand Down
2 changes: 2 additions & 0 deletions js/templates/widget.ms
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{{widget}}}
<hr />
<div id="gadget-chrome-0"></div>
<div id="gadget-chrome-1"></div>
<div id="gadget-chrome-2"></div>
<hr />
<a href="#/">Feed</a>

0 comments on commit ce16ce4

Please sign in to comment.