| 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 src="https://unpkg.com/[email protected]/lib/encoding-indexes.js"></script> |
| 11 |
<script src="https://unpkg.com/[email protected]/lib/encoding.js"></script> |
| 12 |
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/centrifugal/centrifuge-js@master/dist/centrifuge.protobuf.min.js"></script> |
| 13 |
<script type="text/javascript"> |
| 14 |
// helper functions to work with escaping html. |
| 15 |
const tagsToReplace = {'&': '&', '<': '<', '>': '>'}; |
| 16 |
function replaceTag(tag) {return tagsToReplace[tag] || tag;} |
| 17 |
function safeTagsReplace(str) {return str.replace(/[&<>]/g, replaceTag);} |
| 18 |
|
| 19 |
const channel = "chat:index"; |
| 20 |
|
| 21 |
window.addEventListener('load', function() { |
| 22 |
const input = document.getElementById("input"); |
| 23 |
const container = document.getElementById('messages'); |
| 24 |
|
| 25 |
const centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket?format=protobuf'); |
| 26 |
|
| 27 |
// bind listeners on centrifuge object instance events. |
| 28 |
centrifuge.on('connect', function(ctx){ |
| 29 |
drawText('Connected with client ID ' + ctx.client + ' over ' + ctx.transport); |
| 30 |
input.removeAttribute('disabled'); |
| 31 |
}); |
| 32 |
|
| 33 |
centrifuge.on('publish', function(ctx) { |
| 34 |
let str = new TextDecoder("utf-8").decode(ctx.data); |
| 35 |
drawText('Server-side publication from channel ' + ctx.channel + ": " + str); |
| 36 |
}); |
| 37 |
|
| 38 |
centrifuge.on('join', function(ctx) { |
| 39 |
drawText('Server-side join from channel ' + ctx.channel + ": " + JSON.stringify(ctx.info)); |
| 40 |
}); |
| 41 |
|
| 42 |
centrifuge.on('leave', function(ctx) { |
| 43 |
drawText('Server-side leave from channel ' + ctx.channel + ": " + JSON.stringify(ctx.info)); |
| 44 |
}); |
| 45 |
|
| 46 |
centrifuge.on('subscribe', function(ctx) { |
| 47 |
drawText('Subscribe to server-side channel ' + ctx.channel + ' (resubscribed: ' + ctx.isResubscribe + ', recovered: ' + ctx.recovered + ')'); |
| 48 |
}); |
| 49 |
|
| 50 |
centrifuge.on('unsubscribe', function(ctx) { |
| 51 |
drawText('Unsubscribe from server-side channel ' + ctx.channel); |
| 52 |
}); |
| 53 |
|
| 54 |
centrifuge.on('message', function(data) { |
| 55 |
const str = new TextDecoder("utf-8").decode(data); |
| 56 |
drawText(str); |
| 57 |
// Echo data back to server with 'ack' prefix. |
| 58 |
const echoData = new TextEncoder("utf-8").encode("ack " + str); |
| 59 |
centrifuge.send(echoData); |
| 60 |
}); |
| 61 |
|
| 62 |
centrifuge.on('disconnect', function(ctx){ |
| 63 |
drawText('Disconnected: ' + ctx.reason + (ctx.reconnect?", will try to reconnect":", won't try to reconnect")); |
| 64 |
input.removeAttribute('disabled'); |
| 65 |
}); |
| 66 |
|
| 67 |
// subscribe on channel and bind various event listeners. Actual |
| 68 |
// subscription request will be sent after client connects to |
| 69 |
// a server. |
| 70 |
const sub = centrifuge.subscribe(channel, handleMessage) |
| 71 |
.on("join", handleJoin) |
| 72 |
.on("leave", handleLeave) |
| 73 |
.on("unsubscribe", handleUnsubscribe) |
| 74 |
.on("subscribe", handleSubscribe) |
| 75 |
.on("error", handleSubscribeError); |
| 76 |
|
| 77 |
// Trigger actual connection establishing with a server. |
| 78 |
// At this moment actual client work starts - i.e. subscriptions |
| 79 |
// defined start subscribing etc. |
| 80 |
centrifuge.connect(); |
| 81 |
|
| 82 |
function handleSubscribe(ctx) { |
| 83 |
drawText('Subscribed on channel ' + ctx.channel); |
| 84 |
|
| 85 |
const rpcRequest = {"method": "getCurrentYear"} |
| 86 |
const binary = new TextEncoder("utf-8").encode(JSON.stringify(rpcRequest)); |
| 87 |
|
| 88 |
centrifuge.rpc(binary).then(function(result){ |
| 89 |
const str = new TextDecoder("utf-8").decode(result.data); |
| 90 |
drawText("RPC response data: " + str); |
| 91 |
}, function(err) { |
| 92 |
drawText("RPC error: " + JSON.stringify(err)); |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
function handleSubscribeError(err) { |
| 97 |
drawText('Error subscribing on channel ' + err.channel + ': ' + err.message); |
| 98 |
} |
| 99 |
|
| 100 |
function handleMessage(message) { |
| 101 |
const str = new TextDecoder("utf-8").decode(message.data); |
| 102 |
const data = JSON.parse(str); |
| 103 |
let clientID; |
| 104 |
if (message.info){ |
| 105 |
clientID = message.info.client; |
| 106 |
} else { |
| 107 |
clientID = null; |
| 108 |
} |
| 109 |
const inputText = data["input"].toString(); |
| 110 |
const text = safeTagsReplace(inputText) + ' <span class="muted">from ' + clientID + '</span>'; |
| 111 |
drawText(text); |
| 112 |
} |
| 113 |
|
| 114 |
function handleJoin(message) { |
| 115 |
drawText('Someone joined channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')'); |
| 116 |
} |
| 117 |
|
| 118 |
function handleLeave(message) { |
| 119 |
drawText('Someone left channel ' + this.channel + ' (uid ' + message.info["client"] + ', user '+ message.info["user"] +')'); |
| 120 |
} |
| 121 |
|
| 122 |
function handleUnsubscribe(sub) { |
| 123 |
drawText('Unsubscribed from channel ' + sub.channel); |
| 124 |
} |
| 125 |
|
| 126 |
function drawText(text) { |
| 127 |
let e = document.createElement('li'); |
| 128 |
e.innerHTML = [(new Date()).toString(), ' ' + text].join(':'); |
| 129 |
container.insertBefore(e, container.firstChild); |
| 130 |
} |
| 131 |
|
| 132 |
document.getElementById('form').addEventListener('submit', function(event) { |
| 133 |
event.preventDefault(); |
| 134 |
const data = {"input": input.value}; |
| 135 |
const binaryData = new TextEncoder("utf-8").encode(JSON.stringify(data)); |
| 136 |
sub.publish(binaryData).then(function() { |
| 137 |
console.log('message accepted by server'); |
| 138 |
}, function(err) { |
| 139 |
drawText("Publish error: " + err.code + ' ' + err.message); |
| 140 |
console.log('error publishing message', err); |
| 141 |
}); |
| 142 |
input.value = ''; |
| 143 |
}); |
| 144 |
}); |
| 145 |
</script> |
| 146 |
</head> |
| 147 |
<body> |
| 148 |
<form id="form"> |
| 149 |
<input type="text" id="input" autocomplete="off" /> |
| 150 |
<input type="submit" id="submit" value="ยป"> |
| 151 |
</form> |
| 152 |
<ul id="messages"></ul> |
| 153 |
</body> |
| 154 |
</html> |