|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Examples; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon;use PHPDaemon\DNode\DNode;use PHPDaemon\HTTPRequest\Generic; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Examples |
|
8
|
|
|
* @subpackage WebSocket |
|
9
|
|
|
* |
|
10
|
|
|
* @author Vasily Zorin <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ExampleDNode extends \PHPDaemon\Core\AppInstance |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Called when the worker is ready to go. |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
|
|
public function onReady() |
|
19
|
|
|
{ |
|
20
|
|
|
$appInstance = $this; // a reference to this application instance for ExampleWebSocketRoute |
|
21
|
|
|
// URI /exampleApp should be handled by ExampleWebSocketRoute |
|
22
|
|
|
\PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('exampleDNode', |
|
23
|
|
|
function ($client) use ($appInstance) { |
|
24
|
|
|
return new ExampleDNodeRoute($client, $appInstance); |
|
25
|
|
|
}); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Creates Request. |
|
30
|
|
|
* @param object Request. |
|
31
|
|
|
* @param object Upstream application instance. |
|
32
|
|
|
* @return ExampleDNodeTestPageRequest Request. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function beginRequest($req, $upstream) |
|
35
|
|
|
{ |
|
36
|
|
|
return new ExampleDNodeTestPageRequest($this, $upstream, $req); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
class ExampleDNodeRoute extends \PHPDaemon\WebSocket\Route |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
use DNode; |
|
43
|
|
|
|
|
44
|
|
|
public function onHandshake() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->defineLocalMethods([ |
|
47
|
|
|
'serverTest' => function () { |
|
48
|
|
|
$this->callRemote('clientTest', 'foobar', function () { |
|
49
|
|
|
Daemon::log('callback called'); |
|
50
|
|
|
}); |
|
51
|
|
|
}, |
|
52
|
|
|
]); |
|
53
|
|
|
$this->onFrame('{"method":"methods","arguments":[{"clientTest":{}}],"callbacks":{"1":[0,"clientTest"]},"links":[]} ', |
|
|
|
|
|
|
54
|
|
|
'STRING'); |
|
55
|
|
|
parent::onHandshake(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Uncaught exception handler |
|
61
|
|
|
* @param $e |
|
62
|
|
|
* @return boolean Handled? |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handleException($e) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->client->sendFrame('pong from exception: ' . get_class($e)); |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
class ExampleDNodeTestPageRequest extends Generic |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Called when request iterated. |
|
76
|
|
|
* @return integer Status. |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function run() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->header('Content-Type: text/html'); |
|
81
|
|
|
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
82
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
83
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
84
|
|
|
<head> |
|
85
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
|
86
|
|
|
<title>WebSocket test page</title> |
|
87
|
|
|
</head> |
|
88
|
|
|
<body onload="create();"> |
|
89
|
|
|
<script type="text/javascript"> |
|
90
|
|
|
function create() { |
|
91
|
|
|
// Example |
|
92
|
|
|
ws = new WebSocket('ws://' + document.domain + ':8047/exampleDNode'); |
|
93
|
|
|
ws.onopen = function () { |
|
94
|
|
|
document.getElementById('log').innerHTML += 'WebSocket opened <br/>'; |
|
95
|
|
|
} |
|
96
|
|
|
ws.onmessage = function (e) { |
|
97
|
|
|
document.getElementById('log').innerHTML += 'WebSocket message: ' + e.data + ' <br/>'; |
|
98
|
|
|
} |
|
99
|
|
|
ws.onclose = function () { |
|
100
|
|
|
document.getElementById('log').innerHTML += 'WebSocket closed <br/>'; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
</script> |
|
104
|
|
|
<button onclick="create();">Create WebSocket</button> |
|
105
|
|
|
<button onclick="ws.send('ping');">Send ping</button> |
|
106
|
|
|
<button onclick="ws.close();">Close WebSocket</button> |
|
107
|
|
|
<div id="log" style="width:300px; height: 300px; border: 1px solid #999999; overflow:auto;"></div> |
|
108
|
|
|
</body> |
|
109
|
|
|
</html> |
|
110
|
|
|
<?php |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.