In this example we will develop interface to control the LEDs on different IP addresses. We will build from Example 05. First we just rename example05.js and example05.html file to example06.js and example06.html. We should change the reference in .js file from "example05.html" to "example06.html".
We should rename socket in .html (example06.html) file to ws1 and add additional ws2. Notice different IPs, one is .149, the other .205 (use yours accordingly):
let ws1 = new WebSocket("ws://192.168.254.149:8888"); // // create first socket - connect to it
let ws2 = new WebSocket("ws://192.168.1.205:8888"); // // create second socket - connect to it
We should add the buttons to the .html file, four additional buttons should be added. Here the code for all eight buttons is shown, you should use only the bold part:
<button id="buttonOn1" onClick="on1()">On1</button>
<button id="buttonOff1" onClick="off1()">Off1</button>
<br>
<button id="buttonOn2" onClick="on2()">On2</button>
<button id="buttonOff2" onClick="off2()">Off2</button>
<p></p>
<button id="buttonOn3" onClick="on3()">On3</button>
<button id="buttonOff3" onClick="off3()">Off3</button>
<br>
<button id="buttonOn4" onClick="on4()">On4</button>
<button id="buttonOff4" onClick="off4()">Off4</button>
If we try the application the following GUI should be shown in the browser:
Next, we should send the message with parameter to different IP addresses dependent on the button pressed:
function on1() { // when button "on" is pressed we
ws1.send("1"); // send number 1 via websocket
}
function off1() { // when button "off" is pressed we
ws1.send("0"); // send number 0 via websocket
}
function on2() { // when button "on" is pressed we
ws1.send("3"); // send number 3 via websocket
}
function off2() { // when button "off" is pressed we
ws1.send("2"); // send number 2 via websocket
}
function on3() { // when button "on" is pressed we
ws2.send("1"); // send number 1 via websocket
}
function off3() { // when button "off" is pressed we
ws2.send("0"); // send number 0 via websocket
}
function on4() { // when button "on" is pressed we
ws2.send("3"); // send number 3 via websocket
}
function off4() { // when button "off" is pressed we
ws2.send("2"); // send number 2 via websocket
}
The code of the server side remains the same.