add comment

This commit is contained in:
zizifn
2023-01-19 17:09:49 +00:00
committed by zizifn
parent 029a4f32aa
commit 92a0b42fff
2 changed files with 18 additions and 4 deletions

View File

@@ -86,7 +86,7 @@ vlessWServer.on('connection', async function connection(ws) {
new WritableStream({ new WritableStream({
async write(chunk: Buffer, controller) { async write(chunk: Buffer, controller) {
if (remoteConnection) { if (remoteConnection) {
await wsAsyncWrite(remoteConnection, chunk); await socketAsyncWrite(remoteConnection, chunk);
// remoteConnection.write(chunk); // remoteConnection.write(chunk);
return; return;
} }
@@ -150,7 +150,7 @@ vlessWServer.on('connection', async function connection(ws) {
} }
}, },
async write(chunk: Uint8Array, controller) { async write(chunk: Uint8Array, controller) {
ws.send(chunk); await wsAsyncWrite(ws, chunk);
}, },
close() { close() {
console.log( console.log(
@@ -206,9 +206,21 @@ async function connect2Remote(port, host, log: Function): Promise<Socket> {
}); });
} }
async function wsAsyncWrite(ws: Socket, chunk: Buffer) { async function socketAsyncWrite(ws: Socket, chunk: Buffer) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ws.write(Buffer.from(chunk), (error) => { ws.write(chunk, (error) => {
if (error) {
reject(error);
} else {
resolve('');
}
});
});
}
async function wsAsyncWrite(ws: WebSocket, chunk: Uint8Array) {
return new Promise((resolve, reject) => {
ws.send(chunk, (error) => {
if (error) { if (error) {
reject(error); reject(error);
} else { } else {

View File

@@ -176,6 +176,8 @@ export function makeReadableWebSocketStream(
// console.log('MESSAGE', vlessBuffer); // console.log('MESSAGE', vlessBuffer);
// console.log(`message is ${vlessBuffer.byteLength}`); // console.log(`message is ${vlessBuffer.byteLength}`);
// this is not backpressure, but backpressure is depends on underying websocket can pasue
// https://streams.spec.whatwg.org/#example-rs-push-backpressure
controller.enqueue(vlessBuffer); controller.enqueue(vlessBuffer);
}); });
ws.addEventListener('error', (e: any) => { ws.addEventListener('error', (e: any) => {