Проблема: я пытаюсь настроить сервер веб-сокетов на своем веб-сайте, но получаю следующее сообщение об ошибке: «Неустранимая ошибка: Uncaught RuntimeException: Failed» для прослушивания «tcp://0.0.0.0:8080»: адрес уже используется». Эта ошибка возникает в файле TcpServer.php в строке 184. Как устранить и исправить эту проблему, чтобы успешно настроить сервер веб-сокетов на моем веб-сайте?
Я пытаюсь сделать отправка данных в режиме реального времени между администратором Python и клиентами Python отправляет php-сервер
это скрипт администратора Python (Sender)
Код: Выделить всё
import asyncio
import websockets
import json
async def send_array():
uri = "ws://example.com:8080"
async with websockets.connect(uri, timeout=60.0) as websocket:
array_data = [1, 2, 3, "hello"]
await websocket.send(json.dumps(array_data))
asyncio.run(send_array())
вызов TimeoutError из exc_val TimeoutError
это файловый сервер PHP
Код: Выделить всё
require_once 'vendor/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class WebSocketServer implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$data = json_decode($msg, true);
if (is_array($data)) {
// Process the array data as needed
echo "Received array data: " . print_r($data, true) . "\n";
// Send the array data to all connected clients
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send(json_encode($data));
}
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Сообщение о фатальной ошибке: «Не удалось прослушать tcp://0.0.0.0: 8080: Адрес уже используется
и это клиент Python (Получатель)
Код: Выделить всё
import asyncio
import json
import websockets
# Replace this URL with the URL of your PHP server
url = "ws://example:8080"
async def receive_array():
async with websockets.connect(url) as websocket:
while True:
# Receive data from the server
data = await websocket.recv()
array_data = json.loads(data)
# Print the received data for demonstration purposes
print("Received data: ", array_data)
# Run the real-time client
asyncio.get_event_loop().run_until_complete(receive_array())
asyncio.get_event_loop().run_forever()
Я пытаюсь отправить данные в реальном времени между административным Python и клиентами Python, выдающими php-сервер
Подробнее здесь: https://stackoverflow.com/questions/782 ... -0-08080-a