| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title></title> |
| 6 |
<style type="text/css"> |
| 7 |
input[type="text"] { width: 300px; } |
| 8 |
.muted {color: #CCCCCC; font-size: 10px;} |
| 9 |
</style> |
| 10 |
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.min.js"></script> |
| 11 |
<script type="text/javascript"> |
| 12 |
// helper functions to work with escaping html. |
| 13 |
const tagsToReplace = {'&': '&', '<': '<', '>': '>'}; |
| 14 |
function replaceTag(tag) {return tagsToReplace[tag] || tag;} |
| 15 |
function safeTagsReplace(str) {return str.replace(/[&<>]/g, replaceTag);} |
| 16 |
|
| 17 |
const channel = "chat:index"; |
| 18 |
|
| 19 |
window.addEventListener('load', function() { |
| 20 |
const input = document.getElementById("input"); |
| 21 |
const container = document.getElementById('messages'); |
| 22 |
|
| 23 |
const centrifuge = new Centrifuge('ws://localhost:3333'); |
| 24 |
|
| 25 |
// bind listeners on centrifuge object instance events. |
| 26 |
centrifuge.on('connect', function(ctx){ |
| 27 |
drawText('Connected with client ID ' + ctx.client + ' over ' + ctx.transport + ' with data: ' + JSON.stringify(ctx.data)); |
| 28 |
input.removeAttribute('disabled'); |
| 29 |
}); |
| 30 |
|
| 31 |
centrifuge.on('message', function(data) { |
| 32 |
drawText(JSON.stringify(data)); |
| 33 |
// echo data back to server. |
| 34 |
centrifuge.send(data); |
| 35 |
}); |
| 36 |
|
| 37 |
centrifuge.on('disconnect', function(ctx){ |
| 38 |
drawText('Disconnected: ' + ctx.reason + (ctx.reconnect?", will try to reconnect":", won't try to reconnect")); |
| 39 |
input.setAttribute('disabled', 'true'); |
| 40 |
}); |
| 41 |
|
| 42 |
// subscribe on channel and bind various event listeners. Actual |
| 43 |
// subscription request will be sent after client connects to |
| 44 |
// a server. |
| 45 |
const sub = centrifuge.subscribe(channel, handleMessage) |
| 46 |
.on("join", handleJoin) |
| 47 |
.on("leave", handleLeave) |
| 48 |
.on("unsubscribe", handleUnsubscribe) |
| 49 |
.on("subscribe", handleSubscribe) |
| 50 |
.on("error", handleSubscribeError); |
| 51 |
|
| 52 |
// Trigger actual connection establishing with a server. |
| 53 |
// At this moment actual client work starts - i.e. subscriptions |
| 54 |
// defined start subscribing etc. |
| 55 |
centrifuge.connect(); |
| 56 |
|
| 57 |
function handleSubscribe(ctx) { |
| 58 |
drawText('Subscribed on channel ' + ctx.channel + ' (resubscribed: ' + ctx.isResubscribe + ', recovered: ' + ctx.recovered + ')'); |
| 59 |
} |
| 60 |
|
| 61 |
function handleSubscribeError(err) { |
| 62 |
drawText('Error subscribing on channel ' + err.channel + ': ' + err.message); |
| 63 |
} |
| 64 |
|
| 65 |
function handleMessage(message) { |
| 66 |
let clientID; |
| 67 |
if (message.info){ |
| 68 |
clientID = message.info.client; |
| 69 |
} else { |
| 70 |
clientID = null; |
| 71 |
} |
| 72 |
const inputText = message.data["input"].toString(); |
| 73 |
const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>'; |
| 74 |
drawText(text); |
| 75 |
} |
| 76 |
|
| 77 |
function handleJoin(message) { |
| 78 |
drawText('Client joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')'); |
| 79 |
} |
| 80 |
|
| 81 |
function handleLeave(message) { |
| 82 |
drawText('Client left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')'); |
| 83 |
} |
| 84 |
|
| 85 |
function handleUnsubscribe(sub) { |
| 86 |
drawText('Unsubscribed from channel ' + sub.channel); |
| 87 |
} |
| 88 |
|
| 89 |
function drawText(text) { |
| 90 |
let e = document.createElement('li'); |
| 91 |
e.innerHTML = [(new Date()).toString(), ' ' + text].join(':'); |
| 92 |
container.insertBefore(e, container.firstChild); |
| 93 |
} |
| 94 |
|
| 95 |
document.getElementById('form').addEventListener('submit', function(event) { |
| 96 |
event.preventDefault(); |
| 97 |
sub.publish({"input": input.value}).then(function() { |
| 98 |
// console.log('message accepted by server'); |
| 99 |
}, function(err) { |
| 100 |
// console.log('error publishing message', err); |
| 101 |
}); |
| 102 |
input.value = ''; |
| 103 |
}); |
| 104 |
}); |
| 105 |
</script> |
| 106 |
</head> |
| 107 |
<body> |
| 108 |
<form id="form"> |
| 109 |
<input type="text" id="input" autocomplete="off" /> |
| 110 |
<input type="submit" id="submit" value="ยป"> |
| 111 |
</form> |
| 112 |
<ul id="messages"></ul> |
| 113 |
</body> |
| 114 |
</html> |