Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Домашнее задание №6 - реализация чата на сокетах #1205

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions chat/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Anna\Chat\App;

require __DIR__ . '/vendor/autoload.php';

try {
$app = new App();
foreach ($app->run() as $message) {
echo $message;
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
19 changes: 19 additions & 0 deletions chat/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "anna/chat",
"type": "project",
"autoload": {
"psr-4": {
"Anna\\Chat\\": "src/"
}
},
"authors": [
{
"name": "Anton Shishak",
"email": "ashishak@awardwallet.com"
}
],
"require": {
"ext-sockets": "*",
"ext-mbstring": "*"
}
}
18 changes: 18 additions & 0 deletions chat/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions chat/src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Anna\Chat;

use Exception;

class App
{
private array $sockets;

public function __construct()
{
$this->sockets = [
'server' => new ServerSocket(),
'client' => new ClientSocket()
];
}

/**
* @throws Exception
*/
public function run(): iterable
{
if (!isset($_SERVER['argv'][1])) {
throw new Exception('Empty command.');
}
$socket = $_SERVER['argv'][1];
if (!isset($this->sockets[$socket])) {
throw new Exception('Unknown command.');
}
return $this->setSocket($socket)->run();
}

private function setSocket($socket): Socket
{
return new $this->sockets[$socket]();
}
}
21 changes: 21 additions & 0 deletions chat/src/ClientSocket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Anna\Chat;

class ClientSocket extends Socket
{
public function run()
{
while (true) {
yield 'Введите сообщение: ';
$message = trim(fgets(STDIN)) . PHP_EOL;
$this->createSocket();
socket_connect($this->socket, $this->host);
socket_write($this->socket, $message);

if (($message = socket_read($this->socket, 1024)) !== false) {
yield $message;
}
}
}
}
43 changes: 43 additions & 0 deletions chat/src/ServerSocket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Anna\Chat;

use Exception;

class ServerSocket extends Socket
{
public function run()
{
$this->checkExists();
$this->createSocket();
$this->socketBind();
socket_listen($this->socket, 3);

while (true) {
if (($connect = socket_accept($this->socket)) === false) {
continue;
}
if (($message = socket_read($connect, 1024)) !== false) {
yield $message;
socket_write($connect, 'Received ' . mb_strlen($message, '8bit') . ' bytes' . PHP_EOL);
}
}
}

private function checkExists()
{
if (file_exists($this->host)) {
unlink($this->host);
}
}

/**
* @throws Exception
*/
protected function socketBind(): void
{
if (!socket_bind($this->socket, $this->host)) {
throw new Exception("Unable to bind to 'chat.sock' reason: " . socket_strerror(socket_last_error($this->socket)));
}
}
}
22 changes: 22 additions & 0 deletions chat/src/Socket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Anna\Chat;

use Exception;

abstract class Socket
{
public $socket;
public string $host = 'chat.sock';

/**
* @throws Exception
*/
public function createSocket()
{
$this->socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($this->socket === false) {
throw new Exception('Unable create socket.');
}
}
}
32 changes: 32 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3'

services:
server:
build:
context: ./php
dockerfile: Dockerfile
image: server
container_name: server
volumes:
- ./../chat:/var/www
- ./phpsocket:/var/run/socket
command: "tail -f /dev/null"
networks:
- otus-network

client:
build:
context: ./php
dockerfile: Dockerfile
image: client
container_name: client
volumes:
- ./../chat:/var/www
- ./phpsocket:/var/run/socket
command: "tail -f /dev/null"
networks:
- otus-network

networks:
otus-network:
driver: bridge
9 changes: 9 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM php:8.1.22-fpm-alpine

RUN docker-php-ext-install sockets

COPY --from=composer/composer:latest-bin /composer /usr/bin/composer

WORKDIR /var/www

VOLUME /var/www
Loading