diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 5819906da6..b47dbef3e1 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -758,9 +758,14 @@ pub async fn op_ws_close( return Ok(()); }; + const EMPTY_PAYLOAD: &[u8] = &[]; + let frame = reason .map(|reason| Frame::close(code.unwrap_or(1005), reason.as_bytes())) - .unwrap_or_else(|| Frame::close_raw(vec![].into())); + .unwrap_or_else(|| match code { + Some(code) => Frame::close(code, EMPTY_PAYLOAD), + _ => Frame::close_raw(EMPTY_PAYLOAD.into()), + }); resource.closed.set(true); let lock = resource.reserve_lock(); diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index ebb372418e..5e4db3d3e4 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -2663,7 +2663,6 @@ async fn websocket_server_multi_field_connection_header() { let message = socket.read_frame().await.unwrap(); assert_eq!(message.opcode, fastwebsockets::OpCode::Close); - assert!(message.payload.is_empty()); socket .write_frame(fastwebsockets::Frame::close_raw(vec![].into())) .await diff --git a/tests/unit/websocket_test.ts b/tests/unit/websocket_test.ts index 1c8c4581bf..e5e4b1a7a7 100644 --- a/tests/unit/websocket_test.ts +++ b/tests/unit/websocket_test.ts @@ -262,7 +262,7 @@ Deno.test({ socket.onopen = () => socket.send("Hello"); socket.onmessage = () => { socket.send("Bye"); - socket.close(); + socket.close(1000); }; socket.onclose = () => ac.abort(); socket.onerror = () => fail(); @@ -288,7 +288,8 @@ Deno.test({ seenBye = true; } }; - ws.onclose = () => { + ws.onclose = (e) => { + assertEquals(e.code, 1000); deferred.resolve(); }; await Promise.all([deferred.promise, server.finished]);