instantchat/public_html/js/client.js

48 lines
971 B
JavaScript

// Constants and variables
//const wsUri = "ws://localhost:8765";
const wsUri = "ws://chat.glitch-in.space:32715";
let websocket;
// Initialization
function init() {
console.log("Init...");
openWebSocket();
}
// Open WebSocket
function openWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
// WebSocket event handlers
function onOpen(evt) {
console.log('Connected to ' + wsUri + '.');
let text = "Meow";
console.log('Sending "' + text + '".');
websocket.send(text);
}
function onClose(evt) {
console.log("Connection closed.");
}
function onMessage(evt) {
console.log('Received: "' + evt.data + '".');
}
function onError(evt) {
console.error('Error: "' + evt.data + '".');
}
// Run script after page is loaded
$(function() {
init();
});