-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7718572
commit f13e086
Showing
11 changed files
with
319 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"max-client": 5 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import socket, json, time | ||
import src.gui | ||
|
||
client = None | ||
play = False | ||
|
||
url = '' | ||
seek = 0 | ||
|
||
def playinfo(): | ||
try: | ||
data = json.dumps({"type": "req-play-info"}) | ||
client.send(data.encode('utf-8')) | ||
except Exception as e: | ||
return None | ||
|
||
def start_client(server_ip): | ||
global client, play, url, seek | ||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
client.connect((server_ip, 12377)) | ||
try: | ||
playinfo() | ||
while True: | ||
time.sleep(2) | ||
response = client.recv(1024) | ||
try: | ||
message_data = json.loads(response.decode('utf-8').split("}{")[0] + "}") | ||
except Exception as e: | ||
message_data = json.loads(response.decode('utf-8')) | ||
type = message_data.get('type') | ||
print(message_data) | ||
if type == "play-info": | ||
play = True | ||
url = message_data.get('playurl') | ||
seek = message_data.get('seek') | ||
if type == "play-wait": | ||
play = False | ||
except KeyboardInterrupt: | ||
pass | ||
finally: | ||
client.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import socket | ||
import threading | ||
import json | ||
import src.socket.setting as setting | ||
import src.socket.user as user | ||
|
||
playurl = '' | ||
seek = 0 | ||
clients = [] | ||
|
||
def handle_client(client_socket, client_address, clients): | ||
try: | ||
while True: | ||
data = client_socket.recv(1024) | ||
if not data: | ||
break | ||
|
||
try: | ||
message_data = json.loads(data.decode('utf-8')) | ||
message_type = message_data.get('type') | ||
if message_type == "req-play-info": | ||
if playurl != '': | ||
response = {"type": "play-info", "playurl": playurl, "seek": seek} | ||
else: | ||
response = {"type": "play-wait"} | ||
client_socket.send(json.dumps(response).encode('utf-8')) | ||
except json.JSONDecodeError: | ||
pass | ||
|
||
except Exception as e: | ||
pass | ||
finally: | ||
if client_socket in clients: | ||
clients.remove(client_socket) | ||
client_socket.close() | ||
|
||
def broadcast_message(message): | ||
for client in clients[:]: | ||
try: | ||
data = json.dumps(message) | ||
client.send(data.encode('utf-8')) | ||
except Exception as e: | ||
print(f"클라이언트에게 메시지 전송 중 오류 발생: {e}") | ||
if client in clients: | ||
clients.remove(client) | ||
|
||
def start_server(): | ||
global clients | ||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
server.bind(('0.0.0.0', 12377)) | ||
server.listen(setting.max_client) | ||
print(f"SERVER START (ADDRESS) : {user.get_internal_ip()} / {user.get_external_ip()} [NEED PORT FORWARDING -> :12377]") | ||
|
||
while True: | ||
client_socket, client_address = server.accept() | ||
clients.append(client_socket) | ||
client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address, clients)) | ||
client_thread.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import src.utils.json as json | ||
json_file_path = "./server.json" | ||
max_client = 0 | ||
def init_file(): | ||
data = {} | ||
data['max-client'] = 5 | ||
return data | ||
def change_setting_data(key:str,value): | ||
data = json.read(json_file_path) | ||
if data == None: | ||
data = init_file() | ||
data[key] = value | ||
json.write(json_file_path,data) | ||
reload_setting_file() | ||
def reload_setting_file(): | ||
global max_client | ||
data = json.read(json_file_path) | ||
if data == None: | ||
data = init_file() | ||
json.write(json_file_path,data) | ||
max_client = data['max-client'] | ||
##################### | ||
reload_setting_file() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import socket, requests | ||
def get_external_ip(): | ||
try: | ||
response = requests.get("https://api.ipify.org?format=json") | ||
if response.status_code == 200: | ||
return response.json()['ip'] | ||
else: | ||
return '?:?:?:?' | ||
except requests.RequestException as e: | ||
return '?:?:?:?' | ||
|
||
def get_internal_ip(): | ||
try: | ||
hostname = socket.gethostname() | ||
internal_ip = socket.gethostbyname(hostname) | ||
return internal_ip | ||
except socket.error as e: | ||
return '?:?:?:?' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import src.socket.server | ||
import src.socket.client | ||
import threading | ||
|
||
client = False | ||
server = False | ||
|
||
def Start_Server(): | ||
global server | ||
server = True | ||
socket_thread = threading.Thread(target=src.socket.server.start_server) | ||
socket_thread.daemon = True | ||
socket_thread.start() | ||
|
||
def Start_Client(server_ip): | ||
global client | ||
client = True | ||
socket_thread = threading.Thread(target=src.socket.client.start_client, args=(server_ip,)) | ||
socket_thread.daemon = True | ||
socket_thread.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python ./main.py --with-play-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python ./main.py --with-play-server |