forked from napengam/phpWebSocketServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocketPhp.php
More file actions
82 lines (68 loc) · 2.67 KB
/
websocketPhp.php
File metadata and controls
82 lines (68 loc) · 2.67 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
include __DIR__ . "/websocketCore.php";
class websocketPhp extends websocketCore {
public $uuid = '', $connected = false, $chunkSize = 0; // 6 * 1024;
//private $socketMaster;
function __construct($Address, $myIdent = '') {
if (parent::__construct($Address, $myIdent)) {
$buff = fread($this->socketMaster, 1024); // wait for ACK
$buff = $this->decodeFromServer($buff);
$json = json_decode($buff);
if ($json->opcode != 'ready') {
$this->connected = false;
return;
}
$this->fromUUID = $json->uuid; // assigned by server to this script
$this->ident = $myIdent; // ident of other client
}
}
final function broadcast($message) {
$this->talk(['opcode' => 'broadcast', 'message' => $message]);
}
final function feedback($message, $otherIdent = '') {
if ($this->uuid || $otherIdent != '') { // send to client identfied by UUDI or $otherIdent
$this->talk([
'opcode' => 'feedback',
'ident' => $otherIdent, // ident of another client
'uuid' => $this->uuid, // uuid of anotehr client
'message' => $message,
'fromUUID' => $this->fromUUID]);
}
}
final function talk($msg) {
if ($this->connected === false) {
return;
}
$json = json_encode((object) $msg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$len = mb_strlen($json);
if ($len > $this->chunkSize && $this->chunkSize > 0) {
$nChunks = floor($len / $this->chunkSize);
if ($this->writeWait('bufferON')) {
for ($i = 0, $j = 0; $i < $nChunks; $i++, $j += $this->chunkSize) {
if ($this->writeWait(mb_substr($json, $j, $j + $this->chunkSize)) === false) {
break;
}
}
}
if ($len % $this->chunkSize > 0) {
$this->writeWait(mb_substr($json, $j, $j + $len % $this->chunkSize));
}
$this->writeWait('bufferOFF');
} else {
$this->writeWait($json);
}
}
final function writeWait($m) {
if ($this->connected === false) {
return false;
}
$this->writeSocket($m);
$buff = $this->readSocket(); // wait for ACK
$ack = json_decode($buff);
if ($ack->opcode != 'next') {
$this->silent();
return false;
}
return true;
}
}