All Downloads are FREE. Search and download functionalities are using the official Maven repository.

static.ws.js Maven / Gradle / Ivy

The newest version!
var output;
var input;
var button;
var ws;

function init() {
    output = document.querySelector('.output');
    input = document.querySelector('input');
    button = document.querySelector('button');

    input.onkeydown = function(e) {
    	if (event.keyCode === 13) {
    		submit();
    	}
    }

    button.onclick = function(e) {
		submit();
    };

    ws = new WebSocket('ws://localhost:8080/echo');

    ws.onopen = function(e) {
        log('WebSocket opened');
    };

    ws.onmessage = function(e) {
        log('RESPONSE: ' + e.data);
    };

    ws.onclose = function(e) {
        log('WebSocket closed (' + e.reason + ')');
    };

    ws.onerror = function(e) {
        log('ERROR: ' + e.data);
    };
}

function submit() {
	var message = input.value;
    log('SENT: ' + message);
    ws.send(message);
    input.value = '';
    input.focus();
}

function log(message) {
    var p = document.createElement('p');
    p.innerHTML = message;
    output.appendChild(p);
    output.scrollTop = output.scrollHeight;
}

window.addEventListener('load', init, false);




© 2015 - 2025 Weber Informatics LLC | Privacy Policy