forked from g105b/text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.php
More file actions
26 lines (22 loc) · 745 Bytes
/
ws.php
File metadata and controls
26 lines (22 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
chdir(__DIR__);
require("server.php");
require("state.php");
require("canvas.php");
$db = new PDO("sqlite:text.db");
if(empty($db->query("select `name` FROM `sqlite_schema` where `type` = 'table'")->fetchAll())) {
$db->exec(file_get_contents("db.sql"));
echo "Database created.", PHP_EOL;
}
$ws = new Server("0.0.0.0", 10500);
$sendFunction = fn(Socket $client, object|string $data)
=> $ws->send($client, $data);
$canvas = new Canvas();
$state = new State($db, $canvas);
$ws->loop(
onConnect: fn(Socket $socket)
=> $state->clientConnection($socket, $canvas, $sendFunction),
onData: fn(Socket $socket, string $data)
=> $state->clientData($socket, $data),
getData: fn(?int $timestamp = null) => $state->getData($timestamp),
);