Skip to content

Commit

Permalink
메시지 주고받는부분 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
coco1337 committed Aug 25, 2021
1 parent 143036e commit 801f993
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
16 changes: 10 additions & 6 deletions pages/dice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ export default function Dice() {
workRef.current = new Worker('../workers/SocketIoWorker.js');
workRef.current.onmessage = evt => {
console.log(`receive message from worker : ${evt.data}`);
unityContext.send('WebSocketManager', 'ReceiveWebMessage', evt.data);
const threadMessage = JSON.parse(evt.data);
const msg = {id: threadMessage.id, msg: JSON.stringify(threadMessage)};
console.log(`send message to unity: `);
console.log(msg);
unityContext.send('WebSocketManager', 'ReceiveWebMessage', JSON.stringify(msg));
};

unityContext.on('SendPacket', (id: number, str: string) => {
console.log(`packet id : ${id}, detail : ${str}`);
SendMessage(id, str);
unityContext.on('SendPacket', (str: string) => {
console.log(`packet ${str}`);
SendMessageToServer(str);
});

unityContext.on("progress", function (progression) {
Expand All @@ -60,8 +64,8 @@ export default function Dice() {
console.log("test button clicked - in index.tsx");
}

const SendMessage = useCallback(async (id: number, str: string) => {
workRef.current.postMessage({id: id, msg: str});
const SendMessageToServer = useCallback(async (str: string) => {
workRef.current.postMessage({msg: str});
}, []);

return (
Expand Down
3 changes: 0 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const Index = () => (
<div>
<Dice />
</div>
<div>
<Test />
</div>
</div>
)

Expand Down
10 changes: 5 additions & 5 deletions public/workers/SocketIoWorker.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
importScripts('https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.js');

const socket = io('https://pi.coco1337.xyz:41000', { transports: ['websocket']});
const socket = io('http://localhost:8080', { transports: ['websocket']});

socket.on('connect', () => {
console.log('connected');
});

socket.emit('test', {data: 'websocket test'});
// socket.emit('test', {data: 'websocket test'});

addEventListener('message', (e) => {
console.log(e);
console.log(`incoming message from main thread ${e}`);
socket.emit('message', {data: e.data.msg});
});

socket.on('message', (data) => {
console.log(data);
postMessage(JSON.stringify({id: data.id, msg: JSON.stringify(data)}));
console.log(`incoming message from server ${data}`);
postMessage(data);
});

0 comments on commit 801f993

Please sign in to comment.