-
Notifications
You must be signed in to change notification settings - Fork 229
Open
Description
Hi all!
I create WebSocket server on phpdaemon:
Web client part:
<script>
var ws = new WebSocket('ws://site.ll:3333/?user=tester01');
ws.onmessage = function(evt) { alert(evt.data); };
ws.onopen = function (event) { ws.send('tester01'); }
</script>
Worker part:
namespace PHPDaemon\Applications;
class WSEcho extends \PHPDaemon\Core\AppInstance
{
public function onReady()
{
$appInstance = $this;
\PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute("/", function ($client) use ($appInstance)
{
return new WsEchoRoute($client, $appInstance);
});
}
}
class WsEchoRoute extends \PHPDaemon\WebSocket\Route
{
public function onHandshake()
{
\PHPDaemon\Core\Daemon::log("Connected.");
}
public function onFrame($data, $type)
{
if ($type === "STRING")
{
\PHPDaemon\Core\Daemon::log("Receive data «{$data}».");
$this->client->sendFrame($data);
}
}
public function onFinish()
{
\PHPDaemon\Core\Daemon::log("Finish connection.");
}
}
Config part:
max-workers 1;
min-workers 8;
start-workers 1;
max-idle 0;
Pool:Servers\WebSocket
{
listen 'tcp://0.0.0.0';
port 3333;
}
WSEcho
{
}
It is work :)
And I have 2 question:
1. I receive disconnect each 2 min after connect.
Is it normal?
2. Can I somehow connect from my php code to WebSocket server for sending message
Like next:
$localsocket = 'ws://127.0.0.1:3333';
$user = 'tester01';
$message = 'test';
$instance = stream_socket_client($localsocket);
fwrite($instance, json_encode(['user' => $user, 'message' => $message]) . "\n");
Thanks!
Metadata
Metadata
Assignees
Labels
No labels