[Update] WebSocket: Refactor server availability checks and improve error handling

This commit is contained in:
misyaguziya
2025-05-20 13:12:31 +09:00
parent cfc26c4767
commit f310e6d6d5
2 changed files with 32 additions and 19 deletions

View File

@@ -1831,10 +1831,12 @@ class Controller:
} }
} }
else: else:
if model.checkWebSocketServerAlive() is True: if model.checkWebSocketServerAlive() is False:
config.WEBSOCKET_HOST = data
response = {"status":200, "result":config.WEBSOCKET_HOST}
else:
model.stopWebSocketServer() model.stopWebSocketServer()
if model.checkWebSocketServerAvailable() is True:
if model.checkWebSocketServerPortAvailable() is True:
model.startWebSocketServer(data, config.WEBSOCKET_PORT) model.startWebSocketServer(data, config.WEBSOCKET_PORT)
config.WEBSOCKET_HOST = data config.WEBSOCKET_HOST = data
response = {"status":200, "result":config.WEBSOCKET_HOST} response = {"status":200, "result":config.WEBSOCKET_HOST}
@@ -1846,6 +1848,7 @@ class Controller:
"data": config.WEBSOCKET_HOST "data": config.WEBSOCKET_HOST
} }
} }
return response return response
@staticmethod @staticmethod
@@ -1854,9 +1857,12 @@ class Controller:
@staticmethod @staticmethod
def setWebSocketPort(data, *args, **kwargs) -> dict: def setWebSocketPort(data, *args, **kwargs) -> dict:
if model.checkWebSocketServerAlive() is True: if model.checkWebSocketServerAlive() is False:
config.WEBSOCKET_PORT = int(data)
response = {"status":200, "result":config.WEBSOCKET_PORT}
else:
model.stopWebSocketServer() model.stopWebSocketServer()
if model.checkWebSocketServerPortAvailable() is True: if model.checkWebSocketServerAvailable() is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, int(data)) model.startWebSocketServer(config.WEBSOCKET_HOST, int(data))
config.WEBSOCKET_PORT = int(data) config.WEBSOCKET_PORT = int(data)
response = {"status":200, "result":config.WEBSOCKET_PORT} response = {"status":200, "result":config.WEBSOCKET_PORT}
@@ -1876,7 +1882,7 @@ class Controller:
@staticmethod @staticmethod
def setEnableWebSocketServer(*args, **kwargs) -> dict: def setEnableWebSocketServer(*args, **kwargs) -> dict:
if model.checkWebSocketServerPortAvailable() is True: if model.checkWebSocketServerAvailable() is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
config.WEBSOCKET_SERVER = True config.WEBSOCKET_SERVER = True
response = {"status":200, "result":config.WEBSOCKET_SERVER} response = {"status":200, "result":config.WEBSOCKET_SERVER}
@@ -1997,7 +2003,7 @@ class Controller:
printLog("Init WebSocket Server") printLog("Init WebSocket Server")
if config.WEBSOCKET_SERVER is True: if config.WEBSOCKET_SERVER is True:
if model.checkWebSocketServerPortAvailable() is True: if model.checkWebSocketServerAvailable() is True:
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
else: else:
config.WEBSOCKET_SERVER = False config.WEBSOCKET_SERVER = False

View File

@@ -840,20 +840,27 @@ class Model:
"""WebSocketメッセージ受信時の処理""" """WebSocketメッセージ受信時の処理"""
pass pass
def checkWebSocketServerPortAvailable(self): def checkWebSocketServerAvailable(self):
"""WebSocketサーバーのポートが使用中かどうかを確認する""" """WebSocketサーバーのポートが使用中かどうかを確認する"""
response = True response = True
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk: try:
try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
chk.bind((config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)) # SO_REUSEADDRを設定してソケットの再利用を許可
chk.shutdown(socket.SHUT_RDWR) chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
chk.close() try:
except OSError as e: chk.bind((config.WEBSOCKET_HOST, config.WEBSOCKET_PORT))
if e.errno == errno.EADDRINUSE: # シャットダウン前にリッスン状態にする必要はない
response = False chk.close()
else: except OSError as e:
errorLogging() if e.errno == errno.EADDRINUSE:
response = False response = False
else:
errorLogging()
response = False
except Exception:
errorLogging()
response = False
return response return response
def startWebSocketServer(self, host, port): def startWebSocketServer(self, host, port):