Client: parse JSON server messages
This commit is contained in:
parent
c3c41a346c
commit
e99406ea63
|
|
@ -33,6 +33,7 @@ function onClose(evt) {
|
|||
|
||||
function onMessage(evt) {
|
||||
console.log('Received: "' + evt.data + '".');
|
||||
parseMessage(evt.data);
|
||||
}
|
||||
|
||||
function onError(evt) {
|
||||
|
|
@ -54,6 +55,34 @@ function sendInit() {
|
|||
websocket.send(initJson);
|
||||
}
|
||||
|
||||
// Message parsing
|
||||
function parseMessage(msgString) {
|
||||
try {
|
||||
let msg = JSON.parse(msgString);
|
||||
|
||||
switch (msg.type) {
|
||||
// Response to 'init' command
|
||||
case 'init':
|
||||
// TODO
|
||||
console.log('Got init response: ', msg);
|
||||
break;
|
||||
|
||||
// Incoming chat message
|
||||
case 'message':
|
||||
// TODO
|
||||
console.log('Got message event: from "' + msg.from + '", text "' + msg.text + '"');
|
||||
break;
|
||||
|
||||
// TODO Topic change, user join/leave, error, ...
|
||||
|
||||
default:
|
||||
console.error('Unknown message type: "' + msg.type + '"');
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Error parsing message JSON: ' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Run script after page is loaded
|
||||
$(function() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue