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