| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>Centrifuge + Oauth2</title> |
| 6 |
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.min.js"></script> |
| 7 |
</head> |
| 8 |
<body> |
| 9 |
<div> |
| 10 |
Go to <a href="/account"> account page</a> |
| 11 |
</div> |
| 12 |
<div style="margin-top: 20px;"> |
| 13 |
<input type="text" id="input" /> |
| 14 |
</div> |
| 15 |
<script type="text/javascript"> |
| 16 |
const centrifuge = new Centrifuge('ws://localhost:3000/connection/websocket'); |
| 17 |
|
| 18 |
function drawText(text) { |
| 19 |
const div = document.createElement('div'); |
| 20 |
div.innerHTML = text; |
| 21 |
document.body.appendChild(div); |
| 22 |
} |
| 23 |
|
| 24 |
centrifuge.on('connect', function(ctx){ |
| 25 |
drawText('Connected over ' + ctx.transport + '<br>'); |
| 26 |
}); |
| 27 |
|
| 28 |
centrifuge.on('disconnect', function(ctx){ |
| 29 |
drawText('Disconnected: ' + ctx.reason + '<br>'); |
| 30 |
}); |
| 31 |
|
| 32 |
const sub = centrifuge.subscribe("chat", function(message) { |
| 33 |
drawText(message.info.user + ": " + message.data + '<br>'); |
| 34 |
}) |
| 35 |
|
| 36 |
const input = document.getElementById("input"); |
| 37 |
input.addEventListener('keyup', function(e) { |
| 38 |
if (e.keyCode == 13) { |
| 39 |
sub.publish(this.value); |
| 40 |
input.value = ''; |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
centrifuge.connect(); |
| 45 |
</script> |
| 46 |
</body> |
| 47 |
</html> |