2

I have a PHP application running on Apache 2.4 and PHP 5.6.13 which uses https protocol. The internal IP of the server is 10.0.4.160.

I installed ratchet websocket to communicate between a client and the server via a websocket.

I followed the instructions from the Ratchet documentation to set up the server. I run my server and then opened up 2 dos windows and finally was able to sent messages between the 2 windows.

php bin/chat-server.php
telnet localhost 8080
telnet localhost 8080

Now I am trying to send messages from a client to the server on port 8080. I followed the instruction to the very bottom of the instruction page "Next Steps " section. when I try to connect to the server from Google Chrome Javascript's Console I never get a message saying "Connection established!"

var conn = new WebSocket('ws://10.0.4.160:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

What am I doing wrong to connect to the server?

Junior
  • 11,602
  • 27
  • 106
  • 212

1 Answers1

0

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <title>Chat</title>
    <style>
        body {
            margin: 0 auto;
            max-width: 1200px;
            padding: 0 20px;
        }

        .container {
            border: 3px solid #6b757d;
            background-color: #6b757d57;
            border-radius: 5px;
            padding: 10px;
            margin: 10px 0;
        }

        .getMessagecontainer {
            border: 2px solid red !important;
            background-color: lightcoral !important;
            border-radius: 5px;
            padding: 10px;
            margin: 10px 0;
        }

        .sendMessagecontainer {
            border: 2px solid limegreen !important;
            background-color: lightgreen !important;
            border-radius: 5px;
            padding: 10px;
            margin: 10px 0;
            text-align: end;
        }

        .darker {
            border-color: #f1f1f1;
            background-color: #f1f1f1;
        }

        .container::after {
            content: "";
            clear: both;
            display: table;
        }

        .container img {
            float: left;
            max-width: 60px;
            width: 100%;
            margin-right: 20px;
            border-radius: 50%;
        }

        .container img.right {
            float: right;
            margin-left: 20px;
            margin-right: 0;
        }

        .time-right {
            /*float: right;*/
            color: black;
        }

        .time-left {
            /*float: left;*/
            color: black;
        }

        .form-control{
            border: 1px solid #6b757d;
        }
    </style>
</head>
<body>
<div class="container py-4 ">
    <div id="chat-msg">
        <div class="container">
            <div class="d-flex align-items-center justify-content-between">
                <p class="mb-0">Welcome To The Chat</p>
                <p class="mb-0" id="status"></p>
            </div>
        </div>
    </div>
</div>
<div class="container py-4 ">
    <div class="mb-3">
        <label for="message" class="form-label">Type your message ... </label>
        <input type="text" class="form-control" id="message" placeholder="Type your message ...">
    </div>
    <div class="mb-3 d-grid gap-2 col-6 mx-auto">
        <button type="button" class="btn btn-secondary btn-outline" id="send">Send</button>
    </div>
</div>
</body>
<script>

    var currentdate = new Date();
    let socket = new WebSocket("ws://localhost:8080/chat");
    var getMessageData;
    var getMessage;

    socket.onopen = function (e) {
        $("#status").text('open');
    };

    socket.onmessage = function (event) {

        getMessageData = JSON.parse(event.data);
        console.debug(getMessageData);
        getMessage = getMessageData.message;

        $("#chat-msg").append(
            '<div class="getMessagecontainer">' +
            '<div class="d-flex align-items-center justify-content-between">'+
            '<p>' + getMessage + '</p>' +
            '</div>'+
            '<span class="time-left">' +
            currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds() +
            '</span>' +
            '</div>'
        );
    };

    socket.onclose = function (event) {
        if (event.wasClean) {
            $("#status").text(event.code);
        } else {
            $("#status").text('close');
        }
    };

    socket.onerror = function (error) {
        $("#status").text(error.message);
    };

    $("#send").click(function () {
        var msg = $("#message").val();
        if (msg != '') {
            const message = {
                message: msg,
            };
            socket.send(JSON.stringify(message));

            $("#chat-msg").append('<div class="sendMessagecontainer">' +
                '<p>' + $("#message").val() + '</p>' +
                '<span class="time-right">' +
                currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds() +
                '</span>' +
                '</div>');
            $("#message").val('');
        }
    });

</script>
</html>

please do not forget to the change socket URL

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 18 '23 at 04:37