Skip to content

Commit

Permalink
20190322
Browse files Browse the repository at this point in the history
  • Loading branch information
phhpro committed Mar 22, 2019
1 parent 93e9087 commit 3aff28f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 13 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ Project: [PHP Atomchat](https://github.com/phhpro/atomchat)
- Bell works on sender but doesn't get pushed to receiver.
- Superuser screen doesn't allow selecting emojis from hover menu.

## [20190322] - 2019-03-22
### Changed
- Replaced AJAX with SSE. Polling option still available but deactivated.
- Fix: Access token condition was broken on login screen.

## [20190321] - 2019-03-21
### Changed
- Moved copy/paste trigger from symbol to timestamp.

##Added
- Simple query string check to restrict access, primarily aiming at bots.
- Optional access token to restrict access, primarily aiming at bots.

## [20190315] - 2019-03-15
### Changed
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
- One-click text select for easy copy/paste
- Absolutely no database required

### News

As of v20190322 previous AJAX long polling has been replaced with SSE. The polling option is still available but deactivated.

### Configuration

The script generates a default configuration in `config.php` when first run or in case the file has been accidentally deleted. All relevant settings can be configured from the superuser screen and are usually effective immediately. Simply refresh the page if in doubt. You can also enable a timeout option to automatically logout inactive users after a given time. The default is to leave session handling to PHP settings.
Expand Down Expand Up @@ -54,14 +58,8 @@ As an example, consider a native speaker of Japanese on vacation in Italy on a p

### Limitations And Issues

The script uses a very crude AJAX call to perform a pseudo push using some sort of long polling. While this works OK for the target audience running the occasinal P2P session, it still means the script is creating extra overhead on the server and data on the user end; depending the log size.

Sockets are clearly the better approach, but require running a daemon on the server; which may be well beyond the usual hobbyist user; or outright impossible on shared or free hosting. The primary intention of this script was to keep it as simple as possible with next to zero dependencies and minimal user efforts. It says PHP 5 in the header, but actually runs on obsolete 4x machines all the same.

Where JavaScript is not available, or when using a text-mode browser, the page needs manual refreshing to execute the selected action or to update the log. In this case neither character counter, nor emoji hover menu, nor auto select will have any effect. You can still input emojis by typing the assigned text token as illustrated on the settings screen. Text is auto cut after reaching the character limit.

Setting a non-Western default as the page language META produces strange rendering. In mild cases it's just black and white emojis, in more extreme scenarios effectively placing the hover menu out of reach and hence making it completely unusable. Hard-linking a Western locale fixes the issue. Translations are not affected and work as expected.

The only possible caveat lies in setting the refresh rate too small. Default are 15 seconds. In general, the smaller the value, the more bandwidth and user data. Plus, there's a good chance it will freeze the browser; or even hang the whole system!
Setting a non-Western default as the page language META produces strange rendering. In mild cases it's just black and white emojis, in more extreme scenarios effectively placing the hover menu out of reach and hence making it completely unusable. Hard-linking a Western locale fixes the issue. Translations are not affected.

That all said, happy Atomchatting.
14 changes: 14 additions & 0 deletions chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ function selectID(id)
selectText(id);
}


/*
* As of v20190322 AJAX has been replaced with SSE, making the below
* obsolete. It is kept here for the time being as a fallback in case
* your main audience uses MSIE.
*
* MSIE has very poor support for server sent events and may throw an
* error. Uncomment the below section and edit the "chatlog screen"
* section in index.php to revert to polling.
*/


/*
var http = null;
function ajax()
Expand Down Expand Up @@ -101,3 +114,4 @@ function push()
}
push();
*/
50 changes: 45 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* MA 02110-1301, USA.
*/

$ver = "20190321";
$ver = "20190322";

/**
***********************************************************************
Expand Down Expand Up @@ -1701,11 +1701,51 @@ function go($tag)

echo " <article class=\"block\" id=\"push\">\n";

if (is_file($log)) {
include $log;
} else {
if (!is_file($log)) {
file_put_contents($log, $init);
include $log;
} else {

/*
* As of v20190322 AJAX has been replaced with SSE. MSIE
* has very poor support for server sent events and may
* throw an error. Uncomment the below include and comment
* out the entire script echo block to revert to polling.
*/

//** include $log;

echo " <script>\n" .
" if (typeof(EventSource) " .
"!== 'undefined') {\n" .
" var src " .
"= new EventSource('sse.php?src=$log');\n" .
" var sel " .
"= document.querySelector('article');\n" .
" console." .
"log(src.withCredentials);\n" .
" console." .
"log(src.readyState);\n" .
" console." .
"log(src.url);\n\n" .
" src.onopen = function() {\n" .
" console." .
"log('Connection established');\n" .
" };\n\n" .
" src.onclose = function() {\n" .
" console." .
"log('Connection closed');\n" .
" };\n\n" .
" src.onerror = function() {\n" .
" console." .
"log('EventSource failed!');\n" .
" };\n\n" .
" src.onmessage = function(e) {\n" .
" sel.innerHTML = e.data;\n" .
" };\n" .
" } else {\n" .
" alert('SSE not supprted!');\n" .
" }\n" .
" </script>\n";
}

echo " </article>\n";
Expand Down
17 changes: 17 additions & 0 deletions sse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* PHP Version 5 and above
*
* SSE helper
*
* @category PHP_Chat
* @package PHP_Atomchat
* @author P H Claus <[email protected]>
* @copyright 2015 - 2019 P H Claus
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPLv3
* @version GIT: Latest
* @link https://github.com/phhpro/atomchat
*/
header("Content-Type: text/event-stream");
$src = str_replace("\n", "", file_get_contents($_GET['src']));
echo "data: $src\n\n";

0 comments on commit 3aff28f

Please sign in to comment.