Skip to content

Illisible data in websocket #297

Open
@AxelROGET

Description

@AxelROGET

I'm attempting to intercept data from WebSocket, but the data appears to be unreadable (it's a buffer). When I attempt to use toString(), I receive something like ���mlL�� and the WebSocket just doesn't work when I use socket.setEncoding().

I've tried to retrieve the socket from proxyReq, socket, server.on("upgrade") but the result remains the same (even is the Buffer are differents).

const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");


const app = express();

// Configuration :
const PORT = 3000;
const HOST = "localhost";
const API_SERVICE_URL = "http://192.168.1.197:8123";

const proxy = createProxyMiddleware({
    target: API_SERVICE_URL,
    changeOrigin: true,
    pathRewrite: {
        [`^/`]: '',
    },
    ws: true,
    logLevel: 'debug',
    onProxyReqWs: (proxyReq, req, socket) => {
        proxyReq.on('upgrade', (req, socket, head) => {
            proxy.upgrade(req, socket, head);

            // socket.setEncoding('utf-8')

            socket.on('data', data => {
                console.log(data);
                const buff = Buffer.from(data);
                console.log(buff.toString())
            })
        })
    }

})

// Proxy endpoints
app.use('/', proxy);

// Start the Proxy
const server = app.listen(PORT, HOST, () => {
    console.log(`Starting Proxy at ${HOST}:${PORT}`);
});

Do you have any advice on what I should do?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions