Skip to content

Commit 00f2eb2

Browse files
authored
Merge pull request #39 from iazaran/feature/websocket
Feature/websocket
2 parents 30d927c + 1ad3019 commit 00f2eb2

File tree

15 files changed

+422
-45
lines changed

15 files changed

+422
-45
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> This project tries to cover some PHP features in a simple MVC structure with minimum installed composer packages. Then developers can use packages for specific requirements. Please add your ideas in Discussions, ask features or report bugs in issues.
66
7-
💡 TODO: SEO & WebSocket
7+
💡 TODO: SPL
88

99
#### Features:
1010
**List of features related with structure**
@@ -16,6 +16,10 @@ Assets can contain your media files like images, audios & videos.
1616
Contains the styles & scripts _(After changes on these files, you can use minifier script to update minified versions, just run `docker-compose exec php-mvc-app php minifier.php`)_
1717
- **public/feed**
1818
There is a RSS generator in here and runs after creation or updating a post.
19+
- **grpc**
20+
A simple router for distribute the requests to different services. It is not working yet and I created an issue for it ⚠ and if you have an idea or a solution, only by PHP for both server and client sides, please add your solution in there.
21+
- **websocket**
22+
A WebSocket sample. You can open /websocket route in 2 tabs and test the websocket connection.
1923
- **src**
2024
Contains migrations for a DB and routes.
2125
- **src/App**

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"ext-iconv": "*",
3333
"ext-memcached": "*",
3434
"ext-grpc": "*",
35+
"ext-sockets": "*",
3536
"phpmailer/phpmailer": "6.5.4",
3637
"grpc/grpc": "1.42.0",
3738
"google/protobuf": "3.19.4"

composer.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ services:
2323
networks:
2424
- php-mvc-network
2525

26+
php-mvc-websocket-server:
27+
image: nginx:alpine
28+
container_name: php-mvc-websocket-server
29+
volumes:
30+
- ./:/var/www
31+
- ./docker/nginx_websocket:/etc/nginx/conf.d/
32+
ports:
33+
- '9090:80'
34+
networks:
35+
- php-mvc-network
36+
2637
php-mvc-app:
2738
build:
2839
context: .
@@ -50,6 +61,19 @@ services:
5061
networks:
5162
- php-mvc-network
5263

64+
php-mvc-websocket:
65+
build:
66+
context: .
67+
dockerfile: docker/Dockerfile
68+
container_name: php-mvc-websocket
69+
user: www
70+
command: php /var/www/websocket/index.php
71+
volumes:
72+
- ./:/var/www
73+
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.in
74+
networks:
75+
- php-mvc-network
76+
5377
php-mvc-db:
5478
image: mysql
5579
container_name: php-mvc-db

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1111
&& rm -rf /var/lib/apt/lists/*
1212

1313
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
14-
RUN docker-php-ext-install mysqli pdo pdo_mysql gd
14+
RUN docker-php-ext-install mysqli pdo pdo_mysql gd sockets
1515
RUN pecl install -o -f memcached \
1616
&& docker-php-ext-enable memcached
1717
RUN pecl install grpc \

docker/nginx_http1/default.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server {
1717
fastcgi_pass php-mvc-app:9000;
1818
fastcgi_index index.php;
1919
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
20-
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
20+
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/app_php_errors.log";
2121
fastcgi_param PATH_INFO $fastcgi_path_info;
2222
}
2323
}

docker/nginx_http2/default.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server {
1717
fastcgi_pass php-mvc-app:9000;
1818
fastcgi_index index.php;
1919
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
20-
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
20+
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/grpc_php_errors.log";
2121
fastcgi_param PATH_INFO $fastcgi_path_info;
2222
}
2323
}

docker/nginx_websocket/default.conf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
map $http_upgrade $connection_upgrade {
2+
default upgrade;
3+
'' close;
4+
}
5+
6+
upstream websocket {
7+
server php-mvc-websocket:9090;
8+
}
9+
10+
server {
11+
listen 80;
12+
client_max_body_size 108M;
13+
access_log /var/log/nginx/websocket.access.log;
14+
error_log /var/log/nginx/websocket.error.log;
15+
16+
location / {
17+
proxy_pass http://websocket;
18+
proxy_http_version 1.1;
19+
proxy_set_header Upgrade $http_upgrade;
20+
proxy_set_header Connection $connection_upgrade;
21+
proxy_set_header Host $host;
22+
}
23+
}

public/js/main.js

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,80 @@
1+
/**
2+
* Set cookie
3+
*
4+
* @param name
5+
* @param value
6+
* @param expiresDay
7+
*/
8+
function setCookie(name, value, expiresDay) {
9+
const d = new Date();
10+
d.setTime(d.getTime() + (expiresDay * 24 * 60 * 60 * 1000));
11+
let expires = "expires=" + d.toUTCString();
12+
13+
document.cookie = name + "=" + value + ";" + expires + ";path=/";
14+
}
15+
16+
/**
17+
* Get cookie
18+
*
19+
* @param name
20+
* @returns {string}
21+
*/
22+
function getCookie(name) {
23+
let cookieName = name + "=";
24+
let ca = document.cookie.split(";");
25+
for (let i = 0; i < ca.length; i++) {
26+
let c = ca[i];
27+
while (c.charAt(0) === " ") {
28+
c = c.substring(1);
29+
}
30+
if (c.indexOf(cookieName) === 0) {
31+
return c.substring(cookieName.length, c.length);
32+
}
33+
}
34+
35+
return "";
36+
}
37+
38+
/**
39+
* Add message to existing ones on chat
40+
*
41+
* @param messageHTML
42+
*/
43+
function showMessage(messageHTML) {
44+
$("#output").append(messageHTML);
45+
}
46+
47+
function handleChat() {
48+
let webSocket = new WebSocket("ws://localhost:9090");
49+
50+
webSocket.onopen = function (event) {
51+
showMessage("<small class='text-success'>Successfully entered the room...</small>");
52+
};
53+
54+
webSocket.onmessage = function (event) {
55+
let data = JSON.parse(event.data);
56+
showMessage("<p>" + data.message + "</p>");
57+
$("#message").val("");
58+
};
59+
60+
webSocket.onerror = function (event) {
61+
showMessage("<small class='text-danger'>Problem due to some Error!</p>");
62+
};
63+
64+
webSocket.onclose = function (event) {
65+
showMessage("<small class='text-success'>Connection Closed</small>");
66+
};
67+
68+
$("#chat-submit").on("click", function (event) {
69+
event.preventDefault();
70+
let messageJSON = {
71+
name: $("#client-name").val(),
72+
message: $("#message").val()
73+
};
74+
webSocket.send(JSON.stringify(messageJSON));
75+
});
76+
}
77+
178
$(document).ready(function () {
279
/**
380
* Start summernote if needed
@@ -44,7 +121,7 @@ $(document).ready(function () {
44121
* for form and blog-create-submit for it"s button. Form"s buttons
45122
* need to have constant form-button class.
46123
*/
47-
body.on("click", ".form-button", function(event) {
124+
body.on("click", ".form-button", function (event) {
48125
let elementId = $(this).attr("id");
49126
elementId = elementId.replace("-submit", "");
50127

@@ -139,41 +216,8 @@ $(document).ready(function () {
139216
}
140217
});
141218
});
142-
});
143-
144-
/**
145-
* Set cookie
146-
*
147-
* @param name
148-
* @param value
149-
* @param expiresDay
150-
*/
151-
function setCookie(name, value, expiresDay) {
152-
const d = new Date();
153-
d.setTime(d.getTime() + (expiresDay * 24 * 60 * 60 * 1000));
154-
let expires = "expires="+d.toUTCString();
155-
156-
document.cookie = name + "=" + value + ";" + expires + ";path=/";
157-
}
158219

159-
/**
160-
* Get cookie
161-
*
162-
* @param name
163-
* @returns {string}
164-
*/
165-
function getCookie(name) {
166-
let cookieName = name + "=";
167-
let ca = document.cookie.split(";");
168-
for(let i = 0; i < ca.length; i++) {
169-
let c = ca[i];
170-
while (c.charAt(0) === " ") {
171-
c = c.substring(1);
172-
}
173-
if (c.indexOf(cookieName) === 0) {
174-
return c.substring(cookieName.length, c.length);
175-
}
220+
if (window.location.pathname === "/websocket") {
221+
handleChat();
176222
}
177-
178-
return "";
179-
}
223+
});

public/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)