42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
|
//+------------------------------------------------------------------+
|
||
|
//| wsecho_client.js |
|
||
|
//| Copyright 2022, MetaQuotes Ltd. |
|
||
|
//| https://www.mql5.com |
|
||
|
//+------------------------------------------------------------------+
|
||
|
|
||
|
const proto = window.location.protocol.startsWith('http') ?
|
||
|
window.location.protocol.replace('http', 'ws') : 'ws:';
|
||
|
const ws = new WebSocket(proto + '//' + window.location.hostname + ':9000');
|
||
|
|
||
|
ws.onopen = function()
|
||
|
{
|
||
|
console.log('Connected');
|
||
|
};
|
||
|
|
||
|
ws.onmessage = function(message)
|
||
|
{
|
||
|
console.log('Message: %s', message.data);
|
||
|
document.getElementById('echo').value = message.data;
|
||
|
};
|
||
|
|
||
|
const button = document.querySelectorAll('button'); // get all button-tags
|
||
|
|
||
|
// submit button
|
||
|
button[0].addEventListener('click', (event) =>
|
||
|
{
|
||
|
const x = document.getElementById('message').value;
|
||
|
if(x) ws.send(x);
|
||
|
});
|
||
|
|
||
|
// close button
|
||
|
button[1].addEventListener('click', (event) =>
|
||
|
{
|
||
|
ws.close();
|
||
|
document.getElementById('echo').value = 'disconnected';
|
||
|
Array.from(document.getElementsByTagName('button')).forEach((e) =>
|
||
|
{
|
||
|
e.disabled = true;
|
||
|
});
|
||
|
});
|
||
|
//+------------------------------------------------------------------+
|