๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
Typescript ๋ก socket ์ฌ์ฉํ๊ธฐ (server, client) ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Typescript
Typescript ๋ก socket ์ฌ์ฉํ๊ธฐ (server, client)
์ง์ง์ํ์นด 2023. 5. 29. 01:29728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ memi Dev ์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> แแ ฆแแ ตแแ ด แแ ขแแ กแฏแแ กแจแแ ญ 19 TCPแแ ฉแผแแ ตแซ แแ ตแแ ขแแ กแแ ต
๐งธ ์์ผ (Socket)
- ๋คํธ์ํฌ ํ๊ฒฝ์ ์ฐ๊ฒฐํ ์ ์๊ฒ ๋ง๋ค์ด์ง ์ฐ๊ฒฐ๋ถ
- ํ๋กํ ์ฝ, ip์ฃผ์, ํฌํธ๋๋ฒ
- ๋จ์ด์ ธ ์๋ ๋ ํธ์คํธ๋ฅผ ์ฐ๊ฒฐํด์ฃผ๋ ๋๊ตฌ = ์ธํฐํ์ด์ค
- ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ ๊ตฌ์กฐ์ฒด๋ก ์์ผ์ ํตํด ๋ฐ์ดํฐ ํต๋ก๊ฐ ๋ง๋ค์ด์ง
- ์์ผ์ ์ญํ ์ ๋ฐ๋ผ client ์์ผ, server ์์ผ์ผ๋ก ๊ตฌ๋ถ๋จ
https://alpaca-gt.tistory.com/273 ์ฐธ๊ณ
๐งธ 1. socket ์ผ๋ก client ์ server ์ฐ๊ฒฐํ๊ธฐ
๐ชดclient
import net from "node:net"
const client = net.createConnection({port: 3000})
client
.on("ready", () => console.log("ready"))
.on("close", () => console.log("close"))
.on("error", (e) => console.log("err", e))
๐ชดserver
import net from "node:net"
const server = net.createServer(socket => {
console.log("greeting")
})
server.listen(3000)
๐งธ 2. socket ์ผ๋ก data ๋ณด๋ด๊ธฐ
๐ชดclient
import { Socket } from "node:dgram"
import net from "node:net"
const client = net.createConnection({port: 3000})
client
.on("ready", () => {
console.log("ready")
client.write("hello?")
})
.on("close", () => console.log("close"))
.on("error", (e) => console.log("err", e))
.on("data", (d) => {
console.log(d)
console.log(d.toString())
})
๐ชดserver
import net from "node:net"
const server = net.createServer(socket => {
console.log("greeting")
socket
.on("ready", () => console.log("ready"))
.on("close", () => console.log("close"))
.on("error", (e) => console.log("err", e))
.on("data", (d) => {
console.log(d)
console.log(d.toString())
socket.write("ok good")
})
})
server.listen(3000)
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Typescript ๋ก ํํ์ด์ง ๋ง๋ค์ด๋ณด๊ธฐ (2) | 2023.05.31 |
---|---|
Typescript๋ก express ๋ฒ์ ์ html แแ ฉแแ ขแแ ต (0) | 2023.05.31 |
Typescript ๋ก express แแ ฅแจแแ ญแผแแ กแแ ต (0) | 2023.05.31 |
TypeScript ์ฒ์ ์ฌ์ฉํด๋ณด๊ธฐ (์ค์นํ๊ธฐ, ๊ธฐ๋ณธ์ ์ธ ํ์ ๋ช ๋ น) (0) | 2023.05.20 |
Comments