Compare commits
2 Commits
7ae0a01cf6
...
e99406ea63
| Author | SHA1 | Date |
|---|---|---|
|
|
e99406ea63 | |
|
|
c3c41a346c |
|
|
@ -23,23 +23,66 @@ function openWebSocket() {
|
||||||
function onOpen(evt) {
|
function onOpen(evt) {
|
||||||
console.log('Connected to ' + wsUri + '.');
|
console.log('Connected to ' + wsUri + '.');
|
||||||
|
|
||||||
let text = "Meow";
|
// Send init command containing chat ID and nickname
|
||||||
console.log('Sending "' + text + '".');
|
sendInit();
|
||||||
websocket.send(text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose(evt) {
|
function onClose(evt) {
|
||||||
console.log("Connection closed (code " + evt.code + "): ", evt);
|
console.log("Connection closed (code " + evt.code + ").");
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(evt) {
|
function onMessage(evt) {
|
||||||
console.log('Received: "' + evt.data + '".');
|
console.log('Received: "' + evt.data + '".');
|
||||||
|
parseMessage(evt.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(evt) {
|
function onError(evt) {
|
||||||
console.error('Connection error: ', evt);
|
console.error('Connection error: ', evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Command senders
|
||||||
|
function sendInit() {
|
||||||
|
// Define command as JSON object
|
||||||
|
let initObj = {
|
||||||
|
action: "init",
|
||||||
|
chat_id: "42",
|
||||||
|
nickname: "binaryDiv"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send command as JSON string
|
||||||
|
let initJson = JSON.stringify(initObj);
|
||||||
|
console.log('Sending init: ' + initJson);
|
||||||
|
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
|
// Run script after page is loaded
|
||||||
$(function() {
|
$(function() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue