|
| 1 | +<style> |
| 2 | +pre { display : block; float: left; width: 300px; border-right: 1px solid #aaa; } |
| 3 | +#pub, #sub, #err, #hst { padding: 10px; font-size: 8px; } |
| 4 | +#pub { color: green } |
| 5 | +#sub { color: blue } |
| 6 | +#err { color: red } |
| 7 | +#hst { color: orange } |
| 8 | +</style> |
| 9 | + |
| 10 | +<h1>Publish/Subscribe/History Test</h1> |
| 11 | +<h3>History loads 5 seconds after all messages are received from Publish</h3> |
| 12 | +<h5>Published messages will retry automatically on failure in this test.</h5> |
| 13 | +<div> |
| 14 | + <pre id=pub>PUBLISH:</pre> |
| 15 | + <pre id=sub>SUBSCRIBE:</pre> |
| 16 | + <pre id=hst>HISTORY (5 seconds after messages received):</pre> |
| 17 | + <pre id=err>ERROR:</pre> |
| 18 | +</div> |
| 19 | + |
| 20 | +<script src="http://cdn.pubnub.com/pubnub.min.js"></script> |
| 21 | +<script>(function(){ |
| 22 | + |
| 23 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 24 | +// Main |
| 25 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 26 | +(function main() { |
| 27 | + |
| 28 | + var channel = Math.random() + "-chan" |
| 29 | + , pub = PUBNUB.$('pub') |
| 30 | + , sub = PUBNUB.$('sub') |
| 31 | + , hst = PUBNUB.$('hst') |
| 32 | + , err = PUBNUB.$('err') |
| 33 | + , current_ct = 0 |
| 34 | + , test_ct = 50; |
| 35 | + |
| 36 | + PUBNUB.subscribe({ channel : channel, message : function(msg) { |
| 37 | + log( sub, msg ); |
| 38 | + if (test_ct <= ++current_ct) { |
| 39 | + setTimeout( function() { history(channel) }, 5000 ); |
| 40 | + } |
| 41 | + }, connect : function() { |
| 42 | + times( test_ct, function( _, time ) { |
| 43 | + publish( time+'', channel ); |
| 44 | + } ); |
| 45 | + } }); |
| 46 | + |
| 47 | +})(); |
| 48 | + |
| 49 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 50 | +// Loop X Times |
| 51 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 52 | +function times( count, cb ) { |
| 53 | + PUBNUB.each( |
| 54 | + Array(count).join(',').split(','), |
| 55 | + function( a, b ) { cb( a, b ) } |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 60 | +// Load History |
| 61 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 62 | +function history(channel) { |
| 63 | + PUBNUB.history({ |
| 64 | + channel : channel, |
| 65 | + limit : 100, |
| 66 | + callback : function(msgs) { |
| 67 | + PUBNUB.each( msgs[0], function(msg) { |
| 68 | + log( hst, msg ); |
| 69 | + } ); |
| 70 | + } |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 75 | +// Log in a PRE |
| 76 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 77 | +function log( pre, msg ) { |
| 78 | + pre.innerHTML += "\n" + JSON.stringify(msg); |
| 79 | + console.log(JSON.stringify(msg)); |
| 80 | +} |
| 81 | + |
| 82 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 83 | +// Publish with Retry |
| 84 | +// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 85 | +function publish( message, channel ) { |
| 86 | + PUBNUB.publish({ |
| 87 | + channel : channel, |
| 88 | + message : message, |
| 89 | + callback : function(info) { |
| 90 | + if (!info[0]) { |
| 91 | + pubnub( message, channel ); |
| 92 | + log( err, [message, info] ); |
| 93 | + } |
| 94 | + else { |
| 95 | + log( pub, [message, info] ); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +})();</script> |
0 commit comments