Skip to content

Commit

Permalink
Merge pull request #76 from play3step/feat/websocket-connect
Browse files Browse the repository at this point in the history
feat: ์›น์†Œ์ผ“ ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ
  • Loading branch information
play3step authored Aug 23, 2024
2 parents 3d88c6d + 9d47795 commit 754abcc
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions src/pages/EditPage.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from 'styled-components';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useState } from 'react';
import { Canvas } from '@react-three/fiber';
import { FullScreen, useFullScreenHandle } from 'react-full-screen';
import { useParams } from 'react-router-dom';
Expand All @@ -25,6 +25,8 @@ import useHistory from '../hooks/EditPage/Handlers/useHistory';
import ControllerItem from '../components/EditPage/ItemListBox/3D/ControllerItem';
import { ProjectInfo } from '../store/projectState';

export const WEBSOCKET_URL = process.env.REACT_APP_WEBSOCKET_URL;

function EditPage() {
const {
selectedId,
Expand Down Expand Up @@ -57,6 +59,71 @@ function EditPage() {
const fullScreenHandle = useFullScreenHandle();
const isFullScreen = fullScreenHandle.active;

const [socket, setSocket] = useState(null);

useEffect(() => {
// WebSocket ์—ฐ๊ฒฐ
const ws = new WebSocket(WEBSOCKET_URL);

ws.onopen = () => {
console.log('Connected to WebSocket');

const enterMessage = JSON.stringify({
saveType: 'enter',
roomId: code,
});
ws.send(enterMessage);
};

ws.onmessage = (event) => {
const message = JSON.parse(event.data);
console.log('Received:', message);
};

ws.onerror = (error) => {
console.error('WebSocket Error:', error);
};

ws.onclose = () => {
console.log('WebSocket connection closed');
};

setSocket(ws);

// ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ธ๋งˆ์šดํŠธ๋  ๋•Œ WebSocket ๋‹ซ๊ธฐ
return () => {
ws.close();
};
}, []);

const sendRectangleData = () => {
if (socket && socket.readyState === WebSocket.OPEN) {
const rectangleData = {
saveType: 'saveRectangle',
editType: '0',
deleteType: '0',
roomId: code,
rectangle: {
projectId: 123,
pageId: 1,
id: 'rect1',
x: 100,
y: 150,
width: 200,
height: 100,
fill: '#ff0000',
type: 'rectangle',
},
};

// ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์ „์†ก
socket.send(JSON.stringify(rectangleData));
console.log('Rectangle data sent:', rectangleData);
} else {
console.error('WebSocket is not open');
}
};

const toggleFullScreen = () => {
fullScreenHandle.enter();
};
Expand Down Expand Up @@ -172,7 +239,9 @@ function EditPage() {
imgValue={imgValue}
addSlide={addSlide}
/>

<button type="button" onClick={sendRectangleData}>
๋ฒ„ํŠผ
</button>
<CanvasContainer>
<FullScreen handle={fullScreenHandle}>
<div>
Expand Down

0 comments on commit 754abcc

Please sign in to comment.