ย
ย
ย
1. ์๋ฒ
- ํจํค์ง ์ค์น๋ ์๋์ ๋ช ๋ น์ด๋ก ํ๋ค.
pip3 install Flask-SocketIO==5.1.2 python-engineio==4.3.2 python-socketio==5.6.0 eventlet
- ์๋ฒ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
from flask import Flask from flask_socketio import SocketIO import datetime import eventlet app = Flask(__name__) socketio = SocketIO(app) socketio.init_app(app, cors_allowed_origins="*") @app.route('/') def hello(): return '<h1>This is site</h1>' def send_count_in_intervals(): count = 0 while True: socketio.sleep(1) socketio.emit('message', { 'time': datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), 'count': count }) count += 1 if __name__ == '__main__': thread = socketio.start_background_task(send_count_in_intervals) eventlet.wsgi.server(eventlet.listen(('', 9999)), app)
- ์ฝ๋๋ฅผ ์คํํ๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ๋ก๊ทธ๋ฅผ ๊ธฐ๋กํ๋ค.

ย
ย
2. ํด๋ผ์ด์ธํธ
- ํจํค์ง ์ค์น๋ ์๋์ ๋ช ๋ น์ด๋ก ํ๋ค.
pip3 install python-socketio
- ํด๋ผ์ด์ธํธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
import socketio sio = socketio.Client() sio.connect('ws://127.0.0.1:9999') @sio.on('message') def receive_message(message): print('message', message)
- ์ฝ๋๋ฅผ ์คํํ๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ๋ก๊ทธ๋ฅผ ๊ธฐ๋กํ๋ค.

ย
ย
3. ํฌ์คํธ๋งจ ํ ์คํธ
- WebSocket Request๋ฅผ ์์ฑํ๋ค.

- Raw โ Socket.IO๋ก ๋ณ๊ฒฝํ๋ค.

- ์ ์ ์ฃผ์๋ ws ํน์ wss ํ๋กํ ์ฝ๋ก ์ ์ ํ ์ฃผ์๋ฅผ ์ ๋ ฅํ๋ค.

- Events์ ์ ์ ํ ์ด๋ฆ์ ์ ๋ ฅํ๊ณ , Listen on connect๋ฅผ ํ์ฑํํ๋ค.

- ์ค์ ์ด ์๋ฃ๋ ํ, ์ฐ๊ฒฐ์ ํด์ ์ ์์ ํ ์คํธํด๋ณธ๋ค.

ย
ย