Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support beforeSendRequest for WebSocket #568

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

feat: support beforeSendRequest for WebSocket #568

wants to merge 2 commits into from

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