@@ -12,14 +12,41 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
1212
1313import RNTesterText from '../../components/RNTesterText' ;
1414import * as React from 'react' ;
15+ import { useEffect , useState } from 'react' ;
1516import { StyleSheet , View } from 'react-native' ;
1617
18+ // Per the HTML spec, no 'message' event should fire once close() has been called.
19+ // echo.websocket.org greets each connection and echoes what it receives. The
20+ // websocket here is immediately closed, so replies arrive while the socket is
21+ // no longer in the OPEN state.
22+ //
23+ // On React-Native, the following code logs the greeting and echoed hello after close.
24+ // On browsers, the equivalent code won't log any messages.
25+
1726function Playground ( ) {
27+ const [ log , setLog ] = useState < Array < string >> ( [ ] ) ;
28+
29+ useEffect ( ( ) => {
30+ const add = ( s : string ) => {
31+ console . log ( s ) ;
32+ setLog ( l => [ ...l , s ] ) ;
33+ } ;
34+ const ws = new WebSocket ( 'wss://echo.websocket.org' ) ;
35+ ws . onopen = ( ) => {
36+ ws . send ( 'hello' ) ;
37+ ws . close ( ) ;
38+ add ( `close() called, readyState=${ ws . readyState } ` ) ;
39+ } ;
40+ ws . onmessage = e =>
41+ add (
42+ `message after close(): "${ String ( e . data ) } ", readyState=${ ws . readyState } ` ,
43+ ) ;
44+ ws . onclose = e => add ( `onclose code=${ String ( e . code ) } ` ) ;
45+ } , [ ] ) ;
46+
1847 return (
1948 < View style = { styles . container } >
20- < RNTesterText >
21- Edit "RNTesterPlayground.js" to change this file
22- </ RNTesterText >
49+ < RNTesterText > { log . join ( '\n' ) } </ RNTesterText >
2350 </ View >
2451 ) ;
2552}
0 commit comments