Skip to content

Conversation

@roland-reed
Copy link

Add support for forwarding WebSocket connections in beforeSendRequest, but modifying WebSocket message is NOT supported.

e.g.

// rule.js
module.exports = {
  async beforeSendRequest(requestDetail) {
    const {
      protocol,
      requestOptions: { headers },
    } = requestDetail;

    // This example will forward every WebSocket to 127.0.0.1:8800
    // Replace with your own conditions
    if (protocol === 'ws' || protocol === 'wss') {
      return {
        protocol: 'ws',
        requestOptions: {
          hostname: '127.0.0.1',
          port: '8800',
          path: '/',
          headers,
        },
      };
    }

    return requestDetail;
  },
  async beforeDealHttpsRequest(requestDetail) {
    return true;
  },
};
// ws.js
const WebSocket = require('ws');

const server = new WebSocket.Server({
  port: 8800,
});

server.on('connection', ws => {
  ws.on('message', msg => {
    ws.send(msg);
  });
});

feat: support beforeSendRequest for WebSocket

feat: support beforeSendRequest for WebSocket
@CLAassistant
Copy link

CLAassistant commented Aug 17, 2020

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants