Skip to content

Latest commit

ย 

History

History
381 lines (304 loc) ยท 12 KB

README.md

File metadata and controls

381 lines (304 loc) ยท 12 KB

Socket

TCP/IP Socket๊ณผ Socket io ์‚ฌ์šฉ๋ฒ•

์ž‘์„ฑ์ž : ์†ก์ง„์šฐ

Present Time : 2018-09-28

0. ์ฐธ๊ณ  ์ž๋ฃŒ

1. Socket ์ด๋ž€

๋‘ ํ”„๋กœ๊ทธ๋žจ์ด ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์„œ๋กœ ํ†ต์‹ ์„ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์–‘์ชฝ์— ์ƒ์„ฑ๋˜๋Š” ๋งํฌ์˜ ๋‹จ์ž์ž…๋‹ˆ๋‹ค.

  • ๋ฐ์ดํ„ฐ๋ฅผ ์บก์Šํ™”ํ•˜์—ฌ ์ „๋‹ฌ ๊ฐ€๋Šฅ
  • UNIX์—์„œ์˜ ์ž…์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ์˜ ํ‘œ์ค€์ธ ๊ฐœ๋ฐฉ/์ฝ๊ธฐ/์“ฐ๊ธฐ/๋‹ซ๊ธฐ ๋ฉ”์ปค๋‹ˆ์ฆ˜

Socket ์ด๋ฏธ์ง€

2. TCP/IP Socket

TCP ๋Š” ๋‘ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ํ†ต์‹ ์ด ์ฒ˜์Œ ์‹œ์ž‘๋  ๋•Œ๋ถ€ํ„ฐ ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์—ฐ๊ฒฐ์„ ์œ ์ง€ํ•˜๋Š” ์—ฐ๊ฒฐ์ง€ํ–ฅ(Connection oriented) ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

  • ์ŠคํŠธ๋ฆผ ์†Œ์ผ“ ๋ฐฉ์‹
  • ์–‘์ชฝ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋‘ ๋ฐ์ดํ„ฐ ์ฃผ๊ณ  ๋ฐ›๊ธฐ ๊ฐ€๋Šฅ
  • ํ๋ฆ„์ œ์–ด๋“ฑ์„ ๋ณด์žฅํ•ด ์ฃผ๋ฉฐ ์†ก์‹ ๋œ ์ˆœ์„œ์— ๋”ฐ๋ฅธ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹  ๊ฐ€๋Šฅ
  • IP์™€ ํฌํŠธ ๋ฒˆํ˜ธ๋กœ ์†Œ์ผ“์„ ์—ฐ๊ฒฐํ•˜๋ฉด ํ†ต์‹  ์‹œ์ž‘
  • byte ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒ„

2.1 ํ†ต์‹  ์ ˆ์ฐจ

ํ†ต์‹  ์ ˆ์ฐจ

  • TCP Client
  1. ์†Œ์ผ“์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  2. ์„œ๋ฒ„๋กœ connect() ํ•ฉ๋‹ˆ๋‹ค.
  3. ์ ‘์†์ด ์„ฑ๊ณต๋๋‹ค๋ฉด read ๋ฐ write ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ์ฃผ๊ณ  ๋ฐ›์Šต๋‹ˆ๋‹ค.
  4. ์‚ฌ์šฉ์„ ๋งˆ์น˜๋ฉด close๋กœ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.
  • TCP Server
  1. ๋“ฃ๊ธฐ ์†Œ์ผ“์„ ์ƒ์„ ํ•ฉ๋‹ˆ๋‹ค.
  2. bindํ•ฉ๋‹ˆ๋‹ค. (๋‚ด์„  ๋ถ€์—ฌ)
  3. listenํ•ฉ๋‹ˆ๋‹ค. (๋‚ด์„  ์—ฐ๊ฒฐ)
  4. accept() ํด๋ผ์ด์–ธํŠธ๊ฐ€ connectํ•  ๊ฒฝ์šฐ ์†Œ์ผ“์„ ์ƒ์„ฑ ํ•˜๊ณ  ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
  5. read์™€ write ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ  ๋ฐ›์Šต๋‹ˆ๋‹ค.
  6. ์‚ฌ์šฉ๋œ ์—ฐ๊ฒฐ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.
  7. ์‚ฌ์šฉ์„ ๋งˆ์ณค์„ ๊ฒฝ์šฐ ๋“ฃ๊ธฐ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.

2.2 ํด๋ผ์ด์–ธํŠธ ํ†ต์‹  ๊ณผ์ • - JAVA

1. ์†Œ์ผ“์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • Socket ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
 private static Socket socket;
 socket = new Socket();

2. ์„œ๋ฒ„๋กœ connect() ํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„์˜ ์•„์ดํ”ผ์™€ ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ์ ๊ณ  connect()๋กœ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
 socket.connect(new InetSocketAddress("localhost", 'ํฌํŠธ๋ฒˆํ˜ธ'));

3. ์ ‘์†์ด ์„ฑ๊ณตํ–ˆ๋‹ค๋ฉด read ๋ฐ write ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ์ฃผ๊ณ  ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • ์ŠคํŠธ๋ฆผ ์†Œ์ผ“์œผ๋กœ ํ†ต์‹ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— InputStream๊ณผ OutputStream์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ๋˜ํ•œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ์ฃผ๊ณ  ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • OutputStream์— write() ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์Šต๋‹ˆ๋‹ค.
  • InputStream์— read() ๋กœ ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ Charset์œผ๋กœ UTF ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
private static InputStream is;
private static OutputStream os;

is = socket.getInputStream();
os = socket.getOutputStream();

byte[] byteArr = null;
String msg = "Hello Server";

byteArr = msg.getBytes("UTF-8");
os.write(byteArr);
os.flush();
System.out.println("Data Transmitted OK!");

byteArr = new byte[512];
int readByteCount = is.read();

if(readByteCount == -1)
    throw new IOException();

msg = new String(byteArr, 0, readByteCount, "UTF-8");
System.out.println("Data Received OK!");
System.out.println("Message : " + msg);

4. ์‚ฌ์šฉ์„ ๋งˆ์น˜๋ฉด close๋กœ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.

  • ํ†ต์‹ ์ด ๋Š๋‚˜๋ฉด ์ŠคํŠธ๋ฆผ ์†Œ์ผ“๊ณผ ์†Œ์ผ“์„ close() ํ•จ์ˆ˜๋กœ ๋‹ซ์Šต๋‹ˆ๋‹ค.
is.close();
os.close();

socket.close();

2.3 (์ฐธ๊ณ ์šฉ) ์„œ๋ฒ„ ํ†ต์‹  ๊ณผ์ • - JAVA

1. ๋“ฃ๊ธฐ ์†Œ์ผ“์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

private static ServerSocket serverSocket;
serverSocket = new ServerSocket();

2. bindํ•ฉ๋‹ˆ๋‹ค. (๋‚ด์„  ๋ถ€์—ฌ)

serverSocket.bind(new InetSocketAddress(3880));

3. listenํ•ฉ๋‹ˆ๋‹ค. (๋‚ด์„  ์—ฐ๊ฒฐ)

ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญ์ด ์˜ฌ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค!!!!

4. accept() ํด๋ผ์ด์–ธํŠธ๊ฐ€ connectํ•  ๊ฒฝ์šฐ ์†Œ์ผ“์„ ์ƒ์„ฑ ํ•˜๊ณ  ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

private static Socket socket;

socket = serverSocket.accept();

5. read์™€ write ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ  ๋ฐ›์Šต๋‹ˆ๋‹ค.

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

byte[] byteArr = new byte[512];
String msg = null;

int readByteCount = is.read(byteArr);

if(readByteCount == -1)
    throw new IOException();

msg = new String(byteArr, 0, readByteCount, "UTF-8");
System.out.println("Data Received OK!");
System.out.println("Message : " + msg);

msg = "Hello Client";
byteArr = msg.getBytes("UTF-8");
os.write(byteArr);
System.out.println("Data Transmitted OK!");
os.flush();

6. ์‚ฌ์šฉ๋œ ์—ฐ๊ฒฐ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.

is.close();
os.close();
socket.close();

7. ์‚ฌ์šฉ์„ ๋งˆ์ณค์„ ๊ฒฝ์šฐ ๋“ฃ๊ธฐ ์†Œ์ผ“์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.

if(!serverSocket.isClosed()) {
    try {
        serverSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

3. Socket.io

  • ์‹ค์‹œ๊ฐ„ ์ด๋ฒคํŠธ ์„œ๋ฒ„๋ฅผ ๊ฐœ๋ฐœ ํ•  ์ˆ˜ ์žˆ๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.
  • WebSocket์„ ๊ธฐ๋ฐ˜์œผ๋กœ FlashSocket, AJAX Long Polling๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์˜ ์‹ค์‹œ๊ฐ„ ์›น ๊ธฐ์ˆ ๋“ค์„ ํ•˜๋‚˜์˜ API๋กœ ์ถ”์ƒํ™”ํ•œ node.js ๋ชจ๋“ˆ(MIT ๋ผ์ด์„ผ์Šค ์˜คํ”ˆ์†Œ์Šค)
  • ๋ฉ€ํ‹ฐ ๋””๋ฐ”์ด์Šค(web, android, ios, windows)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

Socket.io

3.1 Socket.io ์˜ ํŠน์ง•

์žฅ์ 

  • ํ†ต์‹  ๊ตฌํ˜„์‹œ ์ฝ”๋“œ๊ฐ€ ๋งค์šฐ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.
  • Socket์˜ ์„ฑ๊ฒฉ๊ณผ ๊ฐ™์ด ์‹ค์‹œ๊ฐ„ ํ†ต์‹  ๊ฐ€๋Šฅ
  • http๋ฅผ ํ†ตํ•ด ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๊ณ  JsonObject๋‚˜ ์ผ๋ฐ˜ Object๋กœ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €์™€ ์›น ์„œ๋ฒ„์˜ ์ข…๋ฅ˜์™€ ๋ฒ„์ „์„ ํŒŒ์•…ํ•˜์—ฌ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๊ธฐ์ˆ ์„ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋ผ์šฐ์ €์˜ ์ข…๋ฅ˜์™€ ์ƒ๊ด€์—†์ด ์‹ค์‹œ๊ฐ„ ์›น ๊ตฌํ˜„ ๊ฐ€๋Šฅ

๋‹จ์ 

  • Socket.io๋Š” Javascript์— ์ดˆ์ ์ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.
  • ๋‹ค์–‘ํ•œ ์–ธ์–ด์˜ ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ํ•˜๊ธฐ์—๋Š” ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

3.2 Socket.io ํด๋ผ์ด์–ธํŠธ - JAVA

1. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

  • Gradle์— socket.io๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
dependencies {
  	...์ƒ๋žต 
    implementation ('com.github.nkzawa:socket.io-client:1.0.0'){
        exclude group: 'org.json', module: 'json'
    }
}

2. ์†Œ์ผ“์„ ์„ ์–ธํ•˜๊ณ  ์ดˆ๊ธฐํ™”

  • TCP/IP์™€ ๋™์ผํ•˜๊ฒŒ ์†Œ์ผ“์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • http๋กœ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์„œ๋ฒ„์˜ ์ฃผ์†Œ์™€ ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด์ค๋‹ˆ๋‹ค.
  • URI ์ดˆ๊ธฐํ™” ๊ณผ์ •์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
private Socket socket;
{
    try{
        socket = IO.socket("http://***.***.***.***:***");
    } catch (URISyntaxException ue) {
        ue.printStackTrace();
    }
}

3 ์†Œ์ผ“์„ ์—ฐ๊ฒฐ

  • connect() ํ•จ์ˆ˜๋กœ ์†Œ์ผ“์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
socket.connect();

4(1). ์„œ๋ฒ„ ์ธก์— ์ด๋ฒคํŠธ ์†ก์‹ 

  • ํด๋ผ์ด์–ธํŠธ๋Š” ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ๋ฅผ ์„œ๋ฒ„๋กœ ์†ก์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • emit() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๋˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” ์ด๋ฅผ ์ด๋ฒคํŠธ์˜ ์ด๋ฆ„์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.
socket.emit("EVENT_NANE", DATA);

4(2). ์„œ๋ฒ„ ์ธก์˜ ์ด๋ฒคํŠธ ์ˆ˜์‹ 

  • ์„œ๋ฒ„๋Š” ๋‹ค๋ฅธ ์™ธ๋ถ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด๋‚˜ ์„œ๋ฒ„์˜ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ํด๋ผ์ด์–ธํŠธ์— ์ด๋ฒคํŠธ๋ฅผ ์†ก์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • on() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ด๋ฒคํŠธ ๋ช…์„ ์ด์šฉํ•ด ๊ตฌ๋ถ„ํ•˜๊ณ  ์„œ๋ฒ„์˜ emit์˜ ๋ฐ˜์‘ํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฆฌ์Šค๋„ˆ ์•ˆ์˜ call ํ•จ์ˆ˜ ์•ˆ์—๋Š” ์ด๋ฒคํŠธ ์ˆ˜์‹  ํ›„ ์‹คํ–‰ํ•  ๋‚ด์šฉ์„ ๋‹ด์Šต๋‹ˆ๋‹ค.
socket.on("EVENT_NAME", '๋ฆฌ์Šค๋„ˆ ์ต๋ช…๊ตฌํ˜„ ๊ฐ์ฒด');
Emitter.Listener '๋ฆฌ์Šค๋„ˆ ์ต๋ช…๊ตฌํ˜„ ๊ฐ์ฒด' = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // ์ด๋ฒคํŠธ ์ˆ˜์‹  ์‹œ ์‹คํ–‰ํ•  ๋‚ด์šฉ๋“ค
                }
            });

5. ์‚ฌ์šฉ ํ›„ ์—ฐ๊ฒฐ ํ—ค์ œ

  • ์„œ๋ฒ„์™€์˜ ํ†ต์‹ ์ด ๋” ์ด์ƒ ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ disconnect() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด connect๋ฅผ ๋Š์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  on ์‹œ์ผœ ๋‘์—ˆ๋˜ ์†Œ์ผ“๋„ off() ํ•จ์ˆ˜๋กœ ๋‹ซ์Šต๋‹ˆ๋‹ค.
protected void onDestroy() {
        super.onDestroy();
        socket.disconnect();
        socket.off("EVENT_NAME", '๋ฆฌ์Šค๋„ˆ ์ต๋ช…๊ตฌํ˜„ ๊ฐ์ฒด');
    }

3.2 (์ฐธ๊ณ ์šฉ) Socket.io ์„œ๋ฒ„ - Node.js

var app = require('express')();
var server = require('http').createServer(app);

var io = require('socket.io')(server);

io.on('connection', function(socket) {
  console.log("user connect");

  socket.on('๋ฐ›์€ ์ด๋ฒคํŠธ ๋ช…', function(){
    console.log("๋ฐ›์€ ์ด๋ฒคํŠธ ๋ช…");
    io.emit('๋ณด๋‚ผ ์ด๋ฒคํŠธ ๋ช…', "๋ฉ”์‹œ์ง€๋‚˜ ๋ฐ์ดํ„ฐ");
  });

});

http.listen('ํฌํŠธ ๋ฒˆํ˜ธ!', function(){
  console.log("server on ํฌํŠธ๋ฒˆํ˜ธ");
});

4. ์†Œ์ผ“ ํ†ต์‹ ์‹œ ์ฃผ์˜ํ•  ์ 

  • TCP/IP์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ byte๋ฐฐ์—ด๋กœ ํ•ด์•ผํ•˜๊ณ  Charset์„ ํ•ด์•ผ ๋ฐ์ดํ„ฐ๊ฐ€ ๊บ ์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์†Œ์ผ“์„ ์ƒ์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉ์„ ํ•˜๊ณ  ๋‚˜๋ฉด ๊ผญ ์ •์ƒ์ ์œผ๋กœ ์†Œ์ผ“์˜ ์—ฐ๊ฒฐ์„ ๋Š๊ณ  ์†Œ์ผ“์„ ๋‹ซ์•„์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์†Œ์ผ“๋„ ํ†ต์‹ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž‘์—…์„ ํ•ด์ค˜์•ผ ์„ฑ๋Šฅ ๊ฐœ์„  ๋ฐ ๋กœ์ง์ด ์—‰ํ‚ค๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Socket.io๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด๋ฒคํŠธ ๋ช…์ด ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋„๋ก ์ž˜ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ๊ฐ€ ์—‡๊ฐˆ๋ฆด ๊ฒฝ์šฐ ์ผ์ผ์ด ํ™•์ธํ•ด์•ผํ•˜๋Š” ๋ถˆ์ƒ์‚ฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

5. Kotlin์— ์ ์šฉํ•ด๋ณด๊ธฐ

5.1 Java์™€ ๋‹ค๋ฅธ ์ 

  • static์ด ์•„๋‹ˆ๋ผ Kotlin์—์„œ๋Š” companion object๋กœ socket์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
companion object {
    private lateinit var socket : Socket
    fun get(): Socket {
        try {
            socket = IO.socket("http://127.0.0.0:3000")
            } catch (e: URISyntaxException) {
                e.printStackTrace()
            }
                return socket
            }
        }
  • ๊ทธ ์™ธ ๋žŒ๋‹คํ‘œํ˜„์‹์„ ์ข€ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์  ์™ธ์—๋Š” ๋‹ค๋ฅธ ์ ์ด ์—†์Šต๋‹ˆ๋‹ค.

5.2 emit์„ ๋ณด๋‚ด๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

class MainActivity : AppCompatActivity() {

    lateinit var Text : TextView
    lateinit var lighton_btn: Button
    lateinit var lightoff_btn: Button
    lateinit var socket: Socket

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        lighton_btn = findViewById(R.id.lighton_button)
        lightoff_btn = findViewById(R.id.lightoff_button)
        Text = findViewById(R.id.message)

        socket = SocketApplication.get()

        Text.setText("์†Œ์ผ“ ์ƒ์„ฑ")

        socket.connect()

        lighton_btn.setOnClickListener { v ->
            socket.emit("lightOn")
            Text.setText("Light on Emit ์„ฑ๊ณต")
        }

        lightoff_btn.setOnClickListener { v ->
            socket.emit("lightOff")
            Text.setText("Light off Emit ์„ฑ๊ณต")
        }
    }
}

5.3 on์„ ๋ฐ›๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

class MainActivity : AppCompatActivity() {

    lateinit var Text : TextView
    lateinit var Receive_Text: TextView
    lateinit var socket: Socket

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Text = findViewById(R.id.message)
        Receive_Text = findViewById(R.id.receive_Text)

        socket = SocketApplication.get()

        socket.on("lightOn",light_on)
        socket.on("lightOff", light_off)
        socket.connect()
    }

    var light_on = Emitter.Listener { args ->
        runOnUiThread({
            Text.setText("์†Œ์ผ“ on ์„ฑ๊ณต")
            Receive_Text.setText(args[0].toString())
        })
    }

    var light_off = Emitter.Listener { args ->
        runOnUiThread({
            Text.setText("์†Œ์ผ“ on ์„ฑ๊ณต")
            Receive_Text.setText(args[0].toString())
        })
    }
}