[Update] WebSocket: Implement availability check for WebSocket server and refactor related logic

This commit is contained in:
misyaguziya
2025-05-26 16:15:04 +09:00
parent ed27a8c7ba
commit 88c27a9c70
3 changed files with 31 additions and 32 deletions

View File

@@ -6,7 +6,7 @@ import re
from device_manager import device_manager
from config import config
from model import model
from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress
from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress, isAvailableWebSocketServer
class Controller:
def __init__(self) -> None:
@@ -1835,8 +1835,10 @@ class Controller:
config.WEBSOCKET_HOST = data
response = {"status":200, "result":config.WEBSOCKET_HOST}
else:
if data == config.WEBSOCKET_HOST:
response = {"status":200, "result":config.WEBSOCKET_HOST}
elif isAvailableWebSocketServer(data, config.WEBSOCKET_PORT):
model.stopWebSocketServer()
if model.checkWebSocketServerAvailable() is True:
model.startWebSocketServer(data, config.WEBSOCKET_PORT)
config.WEBSOCKET_HOST = data
response = {"status":200, "result":config.WEBSOCKET_HOST}
@@ -1861,8 +1863,10 @@ class Controller:
config.WEBSOCKET_PORT = int(data)
response = {"status":200, "result":config.WEBSOCKET_PORT}
else:
if int(data) == config.WEBSOCKET_PORT:
return {"status":200, "result":config.WEBSOCKET_PORT}
elif isAvailableWebSocketServer(config.WEBSOCKET_HOST, int(data)) is True:
model.stopWebSocketServer()
if model.checkWebSocketServerAvailable() is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, int(data))
config.WEBSOCKET_PORT = int(data)
response = {"status":200, "result":config.WEBSOCKET_PORT}
@@ -1882,7 +1886,7 @@ class Controller:
@staticmethod
def setEnableWebSocketServer(*args, **kwargs) -> dict:
if model.checkWebSocketServerAvailable() is True:
if isAvailableWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
config.WEBSOCKET_SERVER = True
response = {"status":200, "result":config.WEBSOCKET_SERVER}
@@ -2003,7 +2007,7 @@ class Controller:
printLog("Init WebSocket Server")
if config.WEBSOCKET_SERVER is True:
if model.checkWebSocketServerAvailable() is True:
if isAvailableWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
else:
config.WEBSOCKET_SERVER = False

View File

@@ -1,8 +1,6 @@
import copy
import gc
import asyncio
import socket
import errno
import json
from subprocess import Popen
from os import makedirs as os_makedirs
@@ -840,29 +838,6 @@ class Model:
"""WebSocketメッセージ受信時の処理"""
pass
def checkWebSocketServerAvailable(self):
"""WebSocketサーバーのポートが使用中かどうかを確認する"""
response = True
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
# SO_REUSEADDRを設定してソケットの再利用を許可
chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
chk.bind((config.WEBSOCKET_HOST, config.WEBSOCKET_PORT))
# シャットダウン前にリッスン状態にする必要はない
chk.close()
except OSError as e:
if e.errno == errno.EADDRINUSE:
response = False
else:
errorLogging()
response = False
except Exception:
errorLogging()
response = False
return response
def startWebSocketServer(self, host, port):
"""WebSocketサーバーを起動し、別スレッドで実行する"""
if self.websocket_server_alive is True:

View File

@@ -8,6 +8,7 @@ from logging.handlers import RotatingFileHandler
from ctranslate2 import get_supported_compute_types
import requests
import ipaddress
import socket
def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
try:
@@ -16,6 +17,25 @@ def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
except requests.RequestException:
return False
def isAvailableWebSocketServer(host:str, port:int) -> bool:
"""WebSocketサーバーのポートが使用中かどうかを確認する"""
response = True
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
try:
# SO_REUSEADDRを設定してソケットの再利用を許可
chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
chk.bind((host, port))
# シャットダウン前にリッスン状態にする必要はない
chk.close()
except Exception:
response = False
except Exception:
errorLogging()
response = False
return response
def isValidIpAddress(ip_address: str) -> bool:
try:
ipaddress.ip_address(ip_address)