WebSocket server echoes messages to wscat but not to JavaScript client

I’m working on a project where a C-based WebSocket server needs to communicate with a JavaScript client to change the color of an SVG element. The server should send messages to the JavaScript client when it receives commands from wscat. However, I’m facing an issue where the server echoes the messages back to wscat instead of the JavaScript client.

When I open index.html in a browser, I see the message “WebSocket is open now” in the console, indicating that the WebSocket connection is established. But when I send a command from wscat to the WebSocket server, the server only echoes the message back to wscat and not to the HTML page.

server.c

#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>

static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
                       void *user, void *in, size_t len) {
    switch (reason) {
        case LWS_CALLBACK_ESTABLISHED:
            printf("Connection establishedn");
            break;
        case LWS_CALLBACK_RECEIVE:
            printf("%dn", (int)len);
            char buffer[1024];
            if (len < sizeof(buffer) - 1) {
                memcpy(buffer, in, len);
                buffer[len] = ''; // Add null terminator at the end
                printf("Message received: %sn", buffer);
                
                // Echo the message back to the client
                unsigned char message[LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING];
                memcpy(&message[LWS_SEND_BUFFER_PRE_PADDING], buffer, len);
                lws_write(wsi, &message[LWS_SEND_BUFFER_PRE_PADDING], len, LWS_WRITE_TEXT);
            }
            break;
        default:
            printf("Unhandled callback reason: %dn", reason);
            break;
    }
    return 0;
}

static struct lws_protocols protocols[] = {
    { "ws", callback_ws, 0, 1024 },
    { NULL, NULL, 0, 0 } // Terminator
};

int main(void) {
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof info);
    info.port = 8080;
    info.protocols = protocols;
    struct lws_context *context = lws_create_context(&info);

    if (context == NULL) {
        fprintf(stderr, "lws init failedn");
        return -1;
    }

    printf("Starting server...n");
    while (1) {
        lws_service(context, 1000); // 1000 ms = 1 second
    }

    lws_context_destroy(context);
    return 0;
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket SVG Circle Color Change</title>
    <style>
        #mySVG {
            width: 200px;
            height: 200px;
        }
    </style>
</head>
<body>
    <svg id="mySVG" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
        <circle id="myCircle" cx="50" cy="50" r="20" fill="blue" />
    </svg>
    <p>Received message: <span id="messageDisplay"></span></p>

    <script>
        const socket = new WebSocket("ws://localhost:8080");

        socket.onopen = function(event) {
            console.log("WebSocket is open now.");
        };

        socket.onmessage = function(event) {
            console.log("WebSocket message received:", event.data);
            const messageDisplay = document.getElementById("messageDisplay");
            messageDisplay.textContent = event.data;

            const circle = document.getElementById("myCircle");

            // Assuming the message received is a color name or hex code
            circle.setAttribute("fill", event.data);
        };

        socket.onclose = function(event) {
            console.log("WebSocket is closed now.");
        };

        socket.onerror = function(error) {
            console.error("WebSocket error observed:", error);
        };
    </script>
</body>
</html>

I’ve ensured that the WebSocket server runs on ws://localhost:8080 and the JavaScript client connects to the same address. The server successfully receives messages and prints them, but it only echoes them back to wscat.

How can I ensure that the messages are sent to the JavaScript client as well? What am I missing in the setup or implementation?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật