Differences
This shows you the differences between two versions of the page.
en:comet:faq:js-api-subscription [2017/11/02 04:06] levhav |
en:comet:faq:js-api-subscription [2019/12/18 02:08] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | <rst>EN::12-Examples::001-How to take a message out of the channel in JavaScript</rst> | ||
- | <rst>Header: How to take a message out of the channel in JavaScript</rst> | ||
- | ====== How to take a message out of the channel in JavaScript? ====== | ||
- | |||
- | |||
- | <file html index.html> | ||
- | <!DOCTYPE HTML> | ||
- | <html> | ||
- | <head> | ||
- | <!-- Add libs --> | ||
- | <meta charset=utf-8> | ||
- | <script src="//comet-server.com/CometServerApi.js" type="text/javascript"></script> | ||
- | <script src="jquery.min.js" type="text/javascript"></script> | ||
- | </head> | ||
- | <body> | ||
- | | ||
- | <input id="pipeName" type="text" placeholder="Custom channel name" > | ||
- | <input type="button" value="Subscribe" onclick="subscription();" > | ||
- | <input type="button" value="Clear message list" onclick="$('#msgHolder').html('');" > | ||
- | | ||
- | <div id="pipeNames"></div> | ||
- | | ||
- | <div id="msgHolder"></div> | ||
- | |||
- | <script type="text/javascript"> | ||
- | | ||
- | $(document).ready(function() | ||
- | { | ||
- | /** | ||
- | * Connection to the comet server. For the ability to take commands. | ||
- | * dev_id is your developer's public identifier | ||
- | */ | ||
- | CometServer().start({dev_id:15 }) | ||
- | }) | ||
- | | ||
- | function subscription() | ||
- | { | ||
- | var pipeName = $("#pipeName").val(); | ||
- | $("#pipeName").val(''); | ||
- | $("#pipeNames").html( pipeName + "<br>" + $("#pipeNames").html()) | ||
- | | ||
- | /** | ||
- | * We are subscribed to receive a message from pipe `pipeName` | ||
- | */ | ||
- | CometServer().subscription(pipeName, function(event) | ||
- | { | ||
- | console.log(["event", event]) | ||
- | $("#msgHolder").html( JSON.stringify(event) + "<hr>" + $("#msgHolder").html()) | ||
- | }) | ||
- | } | ||
- | | ||
- | |||
- | </script> | ||
- | </body> | ||
- | </html> | ||
- | </file> |