๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธํ”„๋ก ํŠธ์—”๋“œ/React.js

๋ฆฌ์•กํŠธ - ์›น ์†Œ์ผ“(ws) ํ†ต์‹  + useEffect์—์„œ ์›น ์†Œ์ผ“(ws) ์‚ฌ์šฉ with useRef

by Janger 2022. 11. 7.
728x90
๋ฐ˜์‘ํ˜•

 

ws-server.js (์›น ์†Œ์ผ“ ์„œ๋ฒ„์ธก)
const WebSocketServer = require('ws').Server;

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function message(data) {
    console.log('received: %s', data);
  });

  ws.send('something');
});

 

index.js (๋ฆฌ์•กํŠธ)
import React, { useState ,useEffect, useRef} from 'react';


const [socketConnted, setSocketConnected] = useState(false);
const ws = useRef(null);

useEffect( ()=>{ 
    ws.current = new WebSocket('ws://localhost:8080/ws'); // ์›น ์†Œ์ผ“ ์—ฐ๊ฒฐ

    ws.current.onopen = ()=>{
      console.log('์›น ์†Œ์ผ“ ์—ฐ๊ฒฐ ์„ฑ๊ณต!');
      setSocketConnected(true);
    }
    ws.current.onclose = (error)=>{
      console.log('์›น ์†Œ์ผ“ ์—ฐ๊ฒฐ ์ข…๋ฃŒ.');
      console.log('error: ' + error);
    }
    ws.current.onerror = (error)=>{
      console.log('์›น ์†Œ์ผ“ ์—๋Ÿฌ ๋ฐœ์ƒ.')
      console.log('error: ' + error);
    }
    ws.current.onmessage = (msg)=>{
      console.log('์ƒ๋Œ€ ๋ฉ”์‹œ์ง€: ' +msg.data );
      setTranslate( msg.data );
    }

    return () =>{
      ws.current.close();
    }
}, []);    
    

// ๋‹ค๋ฅธ useEffect์—์„œ ws ์‚ฌ์šฉ
useEffect( ()=>{
    if(socketConnted)
      ws.current.send(finalTranscript);
}, [object] )

 

 

์ฐธ๊ณ 

 

https://sunnykim91.tistory.com/139

 

์„ธ์ƒ ์‰ฌ์šด React๋กœ WebSocketํ†ต์‹ ํ•ด๋ณด๊ธฐ server,client ๋‹ค ์žˆ์Œ!(with TypeScript)

๋จผ์ € , WebSocket์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ๋‹ค. (React+Typescript ๊ธฐ๋ฐ˜์ด๋‹ค.) WebSocket์€ ์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์ด ๊ฐ•์ ์ธ ๊ธฐ์ˆ ์ด๋‹ค. ํ”ํžˆ ์ฑ„ํŒ…์„ ์˜ˆ์ œ๋กœ ๋งŽ์ด๋“ค ๋“ ๋‹ค. ๊ฑฐ๋‘์ ˆ๋ฏธํ•˜๊ณ , ์ผ๋‹จ ์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์ด๋ผ ํ–ˆ์œผ๋‹ˆ, Client์™€ S

sunnykim91.tistory.com

 

https://jcon.tistory.com/186

 

react ์ปดํฌ๋„ŒํŠธ์— websocket ์ ์šฉํ•˜๊ธฐ

์ฝ”๋“œ ver 0.2 import React, { useEffect, useState, useRef } from "react"; const SocketTest = () => { const [socketConnected, setSocketConnected] = useState(false); const [sendMsg, setSendMsg] = useState(false); const [items, setItems] = useState([]); cons

jcon.tistory.com

 

[์›น์†Œ์ผ“ ์ธ์Šคํ„ด์Šค์˜ scope ๋ฌธ์ œ - useEffect hook ์•ˆ์—์„œ ์ •์˜๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ์›น์†Œ์ผ“ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ์•ˆ์œผ๋กœ useRef hook์„ ์‚ฌ์šฉ]

https://velog.io/@ysung327/react-native%EC%9B%B9%EC%86%8C%EC%BC%93-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

 

[react native]์›น์†Œ์ผ“ ์ ์šฉํ•˜๊ธฐ

๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ์—์„œ ์›น์†Œ์ผ“ ํ†ต์‹ ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ WebSocket์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๊ถŒ์žฅํ•œ๋‹ค. ํŽ˜์ด์Šค๋ถ ๊ณต์‹๋ฌธ์„œ - \[https://reactnative.dev/docs/networkํ˜„์žฌ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ฒ˜์Œ์— ์ปดํฌ

velog.io

 

[useRef ์‚ฌ์šฉ๋ฒ• ๋ฐ ์˜ˆ์ œ]

https://itprogramming119.tistory.com/entry/React-useRef-%EC%82%AC%EC%9A%A9%EB%B2%95-%EB%B0%8F-%EC%98%88%EC%A0%9C

 

[React] useRef ์‚ฌ์šฉ๋ฒ• ๋ฐ ์˜ˆ์ œ

[useRef] useRef๋Š” ์ €์žฅ๊ณต๊ฐ„(๋ณ€์ˆ˜ ๊ด€๋ฆฌ), DOM ์š”์†Œ์— ์ ‘๊ทผ์„ ์œ„ํ•ด ์‚ฌ์šฉ์ด ๋˜๋Š” React hooks์ž…๋‹ˆ๋‹ค. Ref๋Š” 'reference'์˜ ์•ฝ์ž๋กœ, '์ฐธ์กฐ'๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. DOM ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ด์„œ ์ด๋ ‡๊ฒŒ ์ง€์—ˆ์„๊นŒ์š”? 1. ์ €์žฅ๊ณต๊ฐ„(๋ณ€์ˆ˜

itprogramming119.tistory.com

 

728x90
๋ฐ˜์‘ํ˜•