| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title></title> |
| 6 |
<style type="text/css"> |
| 7 |
input[type="text"] { width: 300px; } |
| 8 |
</style> |
| 9 |
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.min.js"></script> |
| 10 |
<script type="text/javascript"> |
| 11 |
window.addEventListener('load', function() { |
| 12 |
const container = document.getElementById('messages'); |
| 13 |
|
| 14 |
const centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket', { |
| 15 |
onRefresh: function(ctx, cb) { |
| 16 |
cb({"status": 200, "data": {"token": "I am 12"}}); |
| 17 |
} |
| 18 |
}); |
| 19 |
centrifuge.setToken("I am 12"); |
| 20 |
|
| 21 |
// bind listeners on centrifuge object instance events. |
| 22 |
centrifuge.on('connect', function(ctx){ |
| 23 |
drawText('Connected with client ID ' + ctx.client); |
| 24 |
}); |
| 25 |
|
| 26 |
centrifuge.on('disconnect', function(ctx){ |
| 27 |
drawText('Disconnected: ' + ctx.reason + (ctx.reconnect?", will try to reconnect":", won't try to reconnect")); |
| 28 |
}); |
| 29 |
|
| 30 |
function drawText(text) { |
| 31 |
let e = document.createElement('li'); |
| 32 |
e.innerHTML = [(new Date()).toString(), ' ' + text].join(':'); |
| 33 |
container.insertBefore(e, container.firstChild); |
| 34 |
} |
| 35 |
|
| 36 |
// Trigger actual connection establishing with a server. |
| 37 |
// At this moment actual client work starts - i.e. subscriptions |
| 38 |
// defined start subscribing etc. |
| 39 |
centrifuge.connect(); |
| 40 |
}); |
| 41 |
</script> |
| 42 |
</head> |
| 43 |
<body> |
| 44 |
<ul id="messages"></ul> |
| 45 |
</body> |
| 46 |
</html> |