1

I am planning for queue system application using WebSocket ,PHP ,MySQL ,HTML to send console.log from one client to another to play sound for next queue , now I want to check in clinet2 if console.log value which I receive it client ='Hello From Client1!' paly sound or if = 'another message' do action.

    Client1
    <button onclick="sendMessage()">Send Msg</button>
</body>
<script>
    // Create WebSocket connection.
    const socket = new WebSocket('ws://localhost:3000');

    // Connection opened
    socket.addEventListener('open', function (event) {
        console.log('Connected to WS Server')
    });

    // Listen for messages
    socket.addEventListener('message', function (event) {
        console.log('Message from server ', event.data);
    });

    const sendMessage = () => {
        socket.send('Hello From Client1!');
    }
</script>

    Client2
    <button onclick="sendMessage()">Send Msg</button>
</body>
<script>
    // Create WebSocket connection.
    const socket = new WebSocket('ws://localhost:3000');

    // Connection opened
    socket.addEventListener('open', function (event) {
        console.log('Connected to WS Server')
    });

    // Listen for messages
    socket.addEventListener('message', function (event) {
        console.log('Message from server ', event.data);
    });

    const sendMessage = () => {
        socket.send('Hello From Client2!');
    }
</script>

1 Answers1

0

Thanks All,

I think I got some code can help me. I add in Client 1

<script>
var connection = new WebSocket ('ws://localhost:3000');
connection.onmessage = function (event){
document.getElementById("sul").value=event.data;
};
    const sendMessage = () => {
        socket.send("test");

    }
</script>
<output type="text" id="sul" value="" readonly></output>

and client 2

    socket.addEventListener('message', function (event) {
        console.log('Message from server ', event.data);
        if (event.data =='sul 1'){
         console.log('Here i can put my play sound');
         playsound();
        }
    });
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – lemon Jul 10 '22 at 23:24