ws icon indicating copy to clipboard operation
ws copied to clipboard

Customise how upgrade requests are made

Open DanielStarkey opened this issue 3 years ago • 4 comments

Is there an existing issue for this?

  • [X] I've searched for any related issues and avoided creating a duplicate issue.

Description

I'm attempting to connect to a websocket server that wants the upgrade request be a POST that is multipart/mixed. Is this possible in the WS library? Can't seem to find any documentation for it. TIA 🙂

ws version

No response

Node.js Version

No response

System

No response

Expected result

No response

Actual result

No response

Attachments

No response

DanielStarkey avatar Dec 20 '22 16:12 DanielStarkey

No, that is not possible. From https://www.rfc-editor.org/rfc/rfc6455#section-4.1

The method of the request MUST be GET, and the HTTP version MUST be at least 1.1.

lpinca avatar Dec 20 '22 17:12 lpinca

Okay thanks @lpinca ... I guess the next question then is, is it possible if I perform the upgrade request outside of the ws library (I have code working already for this), can I then pass that socket to ws so that I can use the ws library for the data frames logic?

My current code for performing the upgrade request looks like this (had to strip some sensitive info out):

import http from "node:http";
import https from "node:https";

const body = someInternalFunctionForCreatingTheBody(); // returns a buffer
const options: http.RequestOptions = {
   host: 'customurl.com',
   headers: {
       host: 'customurl.com'.
       'content-type': `multipart/mixed; boundary=${boundaryFromInternalFunction()}`
       // some websocket headers
   },
   method: 'POST',
   path: '/some/path'
};
const request = https.request(options);
request.write(body);
request.end();

request.on('upgrade', (res, socket, upgradeHead) => {
   console.log('request upgraded successfully');
   // is it possible somewhere here to start using the ws library to start sending messages to the websocket server?
})

DanielStarkey avatar Dec 20 '22 17:12 DanielStarkey

I guess the next question then is, is it possible if I perform the upgrade request outside of the ws library (I have code working already for this), can I then pass that socket to ws so that I can use the ws library for the data frames logic?

Perhaps, but the nonce and subprotocol validation is skipped and you have to use private properties and methods that might disappear in future versions of ws.

const ws = new WebSocket(null);
ws._isServer = false;
ws.setSocket(socket, upgradeHead);

Also, the 'open' event is emitted synchronously by websocket.setSocket() so there is no way to listen for that event.

lpinca avatar Dec 20 '22 17:12 lpinca

I guess the next question then is, is it possible if I perform the upgrade request outside of the ws library (I have code working already for this), can I then pass that socket to ws so that I can use the ws library for the data frames logic?

Perhaps, but the nonce and subprotocol validation is skipped and you have to use private properties and methods that might disappear in future versions of ws.

const ws = new WebSocket(null);
ws._isServer = false;
ws.setSocket(socket, upgradeHead);

Also, the 'open' event is emitted synchronously by websocket.setSocket() so there is no way to listen for that event.

Thanks @lpinca . This solution appears to be working for sending messages to the target. Just don't seem to be receiving anything back, doing a bit more digging because this could just be an issue with the target server.

DanielStarkey avatar Dec 21 '22 09:12 DanielStarkey