@@ -5,7 +5,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
5
5
6
6
import { prevent_timer_throttling } from "./scheduling" ;
7
7
import { Queue } from "./queue" ;
8
- import { ENVIRONMENT_IS_NODE , ENVIRONMENT_IS_SHELL , createPromiseController , loaderHelpers , mono_assert } from "./globals" ;
8
+ import { ENVIRONMENT_IS_NODE , ENVIRONMENT_IS_SHELL , createPromiseController , loaderHelpers , mono_assert , Module } from "./globals" ;
9
9
import { setI32 , localHeapViewU8 , forceThreadMemoryViewRefresh } from "./memory" ;
10
10
import { VoidPtr } from "./types/emscripten" ;
11
11
import { PromiseController } from "./types/internal" ;
@@ -51,7 +51,7 @@ export function ws_get_state (ws: WebSocketExtension): number {
51
51
const queued_events_count = receive_event_queue . getLength ( ) ;
52
52
if ( queued_events_count == 0 )
53
53
return ws . readyState ?? - 1 ;
54
- return WebSocket . OPEN ;
54
+ return ws [ wasm_ws_close_sent ] ? WebSocket . CLOSING : WebSocket . OPEN ;
55
55
}
56
56
57
57
export function ws_wasm_create ( uri : string , sub_protocols : string [ ] | null , receive_status_ptr : VoidPtr ) : WebSocketExtension {
@@ -116,13 +116,15 @@ export function ws_wasm_create (uri: string, sub_protocols: string[] | null, rec
116
116
}
117
117
118
118
// send close to any pending receivers, to wake them
119
- const receive_promise_queue = ws [ wasm_ws_pending_receive_promise_queue ] ;
120
- receive_promise_queue . drain ( ( receive_promise_control ) => {
121
- setI32 ( receive_status_ptr , 0 ) ; // count
122
- setI32 ( < any > receive_status_ptr + 4 , 2 ) ; // type:close
123
- setI32 ( < any > receive_status_ptr + 8 , 1 ) ; // end_of_message: true
124
- receive_promise_control . resolve ( ) ;
125
- } ) ;
119
+ Module . safeSetTimeout ( ( ) => {
120
+ const receive_promise_queue = ws [ wasm_ws_pending_receive_promise_queue ] ;
121
+ receive_promise_queue . drain ( ( receive_promise_control ) => {
122
+ setI32 ( receive_status_ptr , 0 ) ; // count
123
+ setI32 ( < any > receive_status_ptr + 4 , 2 ) ; // type:close
124
+ setI32 ( < any > receive_status_ptr + 8 , 1 ) ; // end_of_message: true
125
+ receive_promise_control . resolve ( ) ;
126
+ } ) ;
127
+ } , 0 ) ;
126
128
} catch ( error : any ) {
127
129
mono_log_warn ( "failed to propagate WebSocket close event: " + error . toString ( ) ) ;
128
130
}
0 commit comments