@@ -5,70 +5,33 @@ import MessageBus from "./MessageBus.js";
5
5
import Player from "./Player.js" ;
6
6
import Map2d from "./map/Map2d.jsx" ;
7
7
import InfoPanel from "./panel/InfoPanel.jsx" ;
8
- import '../libs/wasm_exec.js' ;
8
+ // import '../libs/wasm_exec.js';
9
9
import './protos/Message_pb.js'
10
10
import DemoContext from "../context.js"
11
+ // import workerScript from "./worker.js";
11
12
12
13
export function PlayerApp ( ) {
13
14
const demoData = useContext ( DemoContext ) ;
15
+ const worker = new Worker ( "src/libs/worker.js" ) ;
14
16
15
17
const [ messageBus ] = useState ( new MessageBus ( ) )
16
18
const [ player ] = useState ( new Player ( messageBus ) )
17
19
const [ serverHost ] = useState ( window . location . host . includes ( "localhost" ) ? "http://localhost:8080" : "" ) ;
18
20
const [ isWasmLoaded , setIsWasmLoaded ] = useState ( false )
19
21
20
- useEffect ( ( ) => {
21
- console . log ( "run run run" , demoData )
22
-
23
- if ( ! isWasmLoaded ) {
24
- loadWasm ( ) ;
25
- return
26
- }
22
+ worker . onmessage = ( e ) => {
23
+ console . log ( "Message received from worker" , e ) ;
24
+ const msg = proto . Message . deserializeBinary ( e . data ) . toObject ( )
25
+ messageBus . emit ( msg )
26
+ } ;
27
27
28
+ useEffect ( ( ) => {
28
29
if ( demoData . demoData ) {
29
- window . testt ( demoData . demoData , async function ( data ) {
30
- if ( data instanceof Uint8Array ) {
31
- const msg = proto . Message . deserializeBinary ( data ) . toObject ( )
32
- messageBus . emit ( msg )
33
- } else {
34
- console . log ( "[message] text data received from server, this is weird. We're using protobufs ?!?!?" , data ) ;
35
- messageBus . emit ( JSON . parse ( data ) )
36
- }
37
- } )
38
- demoData . setDemoData ( null )
30
+ setTimeout ( ( ) => worker . postMessage ( demoData . demoData ) , 1000 )
39
31
}
40
-
41
- // const urlParams = new URLSearchParams(window.location.search);
42
- // const channel = new BroadcastChannel(urlParams.get("uuid"));
43
- // channel.onmessage = async (event) => {
44
- // console.log("received", event, isWasmLoaded)
45
- // await window.testt(event.data, function (data) {
46
- // if (data instanceof Uint8Array) {
47
- // const msg = proto.Message.deserializeBinary(data).toObject()
48
- // setMessage("123")
49
- // console.log("huha?")
50
- // messageBus.emit(msg)
51
- // } else {
52
- // console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data);
53
- // messageBus.emit(JSON.parse(data))
54
- // }
55
- // })
56
- // };
57
32
messageBus . listen ( [ 13 ] , function ( msg ) {
58
33
alert ( msg . message )
59
- // window.testt(byteArray)
60
34
} )
61
- // Connect(this.messageBus)
62
-
63
- async function loadWasm ( ) {
64
- const go = new window . Go ( ) ;
65
- WebAssembly . instantiateStreaming ( fetch ( serverHost + "/wasm" ) , go . importObject )
66
- . then ( ( result ) => {
67
- go . run ( result . instance ) ;
68
- console . log ( "should be loaded now" )
69
- setIsWasmLoaded ( true )
70
- } ) ;
71
- }
72
35
} , [ isWasmLoaded ] )
73
36
74
37
return (
0 commit comments