Closed mkeyno closed 7 years ago
Really I has been joined to varspool community only last week or close to it. I updated their 2.x version that seems abandoned in time they concentrated all free time on 3.x version.
So, if you keep in mind some server with PHP 7.0 or 7.1 PHP better ask Dominic Scheirlinck dominic@varspool.com.
If you interested in PHP from 5.3 to 5.6 I can at least show you how I has implemented Wrench library in my project.
First question is are you using websocket server as standalone or with Nginx/Apache HTTP server. My project is complex and has http server, nginx on production and apache on developers machines.
If yes, its better to pass data through http server as proxy. Its allows to use one SSL certificate for both http server and web server. Also, websocket will use same 80 port what is less complicated of firewall settings, client browser, client firewalls/proxy/corporate proxies etc.
Nginx: You MUST add this to host configuration location /websocket { proxy_pass http://localhost:8010; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; }
You SHOULD add this to http configuration proxy_read_timeout 600s; map $http_upgrade $connection_upgrade { default upgrade; '' close; }
600s is nginx proxy timeout for case if no data was sent between server and client. You can add a script that will ping all clients every 50 seconds as I did (default nginx timout is 60 seconds).
Apache: Add this to host configuration ProxyPass /websocket ws://localhost:8010/websocket
You MUST enable two Apache modules: "proxy" and "proxy_wstunnel";
If no http server at all, I advise you to use 80 port for websocket server privately.
My project was built on Yii2 framework so mine websocket server is one of this framework console controllers:
<?php
namespace console\controllers;
use Yii; use yii\helpers\Inflector; use Wrench\Server; use Wrench\Payload\Payload; use Wrench\Connection; use Wrench\Protocol\Protocol;
class WebsocketController extends yii\console\Controller {
/**
* @var integer ping interval, in seconds
*/
public $pingInterval = 50;
/**
* @var array clients connections
*/
protected $connections = array();
/**
* @var array registered subscriptions for events
*/
protected $subscriptions = [];
/**
* @var integer last timestamp when ping signal was sent to clients
*/
protected $lastPinged;
/**
* @param Connection $connection
*/
public function onConnect($connection)
{
$connectionId = $connection->getId();
$this->connections[$connectionId] = $connection;
}
/**
* @param Connection $connection
*/
public function onDisconnect($connection)
{
$connectionId = $connection->getId();
unset($this->connections[$connectionId]);
unset($this->subscriptions[$connectionId]);
}
/**
* Could be used as timer event.
* Sends ping to all clients once per minutes to keep connection.
* Useful for Nginx with default proxy settings (60 seconds timeout
to close connection). */ public function onUpdate() { if (time() - $this->lastPinged > $this->pingInterval) { $this->lastPinged = time(); $this->broadcast($this->lastPinged, null, Protocol::TYPE_PING); } }
/**
* Handle data received from a client
* @param Payload $data A payload object, that supports __toString()
* @param Connection $connection
*/
public function onData(Payload $data, Connection $connection)
{
if (!strlen($data) || $data->getType() != Protocol::TYPE_TEXT) {
return;
}
$parsed = (array) json_decode($data, true);
if (!isset($parsed[0])) {
$parsed = [$parsed];
}
foreach ($parsed as $command) {
$action = Inflector::id2camel(@$command['action']);
$method = 'action' . $action;
$params = @$command['params'];
if (method_exists($this, $method)) {
$this->$method($connection, $params);
}
}
}
/**
* Closes connection on client request
* @param Connection $connection
* @param array $params parsed data
*/
protected function actionClose($connection, $params)
{
$connection->close();
}
/**
* Subscribes client for command notifications
* @param Connection $connection
* @param array $params parsed data
*/
protected function actionSubscribe($connection, $params)
{
$this->subscriptions[$connection->getId()][] = $params;
}
/**
* Analyze and process entity events.
* Processing entity events, analyzes subscriptions and sends messages.
* @param array $params parsed data
*/
protected function processEvent($params)
{
$this->delivery($params);
}
/**
* Sends event notification on client request
* @param Connection $connection
* @param array $params parsed data
*/
protected function actionEvent($connection, $params)
{
$this->processEvent($params);
}
/**
* Sends data to all clients
* @param string|Payload $data
* @param int $except
*/
protected function broadcast($data, $except = null, $type =
Protocol::TYPE_TEXT) { foreach ($this->connections as $connection) { if ($connection->getId() != $except) { $connection->send($data, $type); } } }
/**
* Sends data to clients have a subscriptions
* @param string|Payload $params
* @param int $except
*/
protected function delivery($params)
{
$data = ['action' => 'Event', 'params' => $params];
foreach ($this->subscriptions as $connectionId => $subscriptions) {
foreach ($subscriptions as $subscription) {
$valid = true;
foreach ($subscription as $key => $value) {
if (empty($params[$key]) || $value != $params[$key]) {
$valid = false;
break;
}
}
if ($valid) {
$connection = $this->connections[$connectionId];
$connection->send($data);
}
}
}
}
/**
* Logger event
* @param type $message
* @param type $priority
*/
public function log($message, $priority = 'info')
{
if (YII_DEBUG) {
echo $priority . ': ' . $color . $message . PHP_EOL;
}
}
/**
* Starts websocket server
*/
public function actionIndex()
{
$server = new Server(Yii::$app->websocket->serverString, [
'logger' => [$this, 'log'],
]);
$server->registerApplication(Yii::$app->websocket->url, $this); $this->lastPinged = time(); $server->run(); }
}
That is just a sample with event + subscription mechanism. Anyway, main is $connections array. Just save who is connected and you can send messages to them with broadcast function or one by one.
My project's websocket server does not use its own SSL certificate and I forgot did I ever tested it with.
Hope it will help.
From: mehrdad notifications@github.com Sent: 18/02/17 12:27 To: varspool/Wrench Wrench@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [varspool/Wrench] websocket lunch on remote host (mehrdad00)
hi, I'm sorry to open ticket for my question , but actually I'm not pro in php and with your instruction not figure out how to install it in my remote directory host , and really appreciate for couple of hints , in fact I've tried to hook url on my host to the Telegram webhoock call back bot, so at first step new update data inserted to MySQL database then websoket server give the update data to designated live client . can you tell me which files should be copy in designated host URL and what files should be modified? Thanks
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/varspool/Wrench/issues/100, or mute the thread https://github.com/notifications/unsubscribe-auth/AK-_soR8LaAmHew9BjLHCjRGMK7D0bS2ks5rdsd6gaJpZM4MFEws.
Yii::$app->websocket->serverString
is
ws:://localhost:8010/websocket or http:://localhost:8010/websocket or tcp:://localhost:8010/websocket (try different if first dont works)
Yii::$app->websocket->url
is
/websocket
From: mehrdad notifications@github.com Sent: 18/02/17 12:27 To: varspool/Wrench Wrench@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [varspool/Wrench] websocket lunch on remote host (mehrdad00)
hi, I'm sorry to open ticket for my question , but actually I'm not pro in php and with your instruction not figure out how to install it in my remote directory host , and really appreciate for couple of hints , in fact I've tried to hook url on my host to the Telegram webhoock call back bot, so at first step new update data inserted to MySQL database then websoket server give the update data to designated live client . can you tell me which files should be copy in designated host URL and what files should be modified? Thanks
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/varspool/Wrench/issues/100, or mute the thread https://github.com/notifications/unsubscribe-auth/AK-_soR8LaAmHew9BjLHCjRGMK7D0bS2ks5rdsd6gaJpZM4MFEws.
@nexen2 really appreciated for the explanation , but I bought the host & domain which is only has Cpanel, and I don't know how to change the my php server configuration so it could run websoket service , but I think it would be source lib and one url like http//:mydomain/url/handle.php, also I use EPS8266 module which only could has the http client websocket and not https , your advise really appreciated Cpanel image
Wrench library is what you can find in"lib" directory. "example" directory has "server.php" file that could be suitable for you as sample of server main file.
"Application" is any PHP class that has onConnect, onDisconnect, onUpdate and onData fucntions. As you can see from my example, I start server and right in application class (starting server I sending $this into it as application).
Upload lib directory and server.php file and try. If you has SSH access to your host, thats all. If not.... Well thats really bad.
You should have one additional file to start server. Something like start.php:
<?php system('php server.php >/dev/null 2>&1 &');
This code will start server.php as background process.
From: mehrdad notifications@github.com Sent: 18/02/17 14:45 To: varspool/Wrench Wrench@noreply.github.com Cc: nexen2 vovasn@yandex.ru, Mention mention@noreply.github.com Subject: Re: [varspool/Wrench] websocket lunch on remote host (mehrdad00)
@nexen2 https://github.com/nexen2 really appreciated for the explanation , but I bought the host & domain which is only has Cpanel, and I don't know how to change the my php server configuration so it could run websoket service , but I think it would be source lib and one url like http//:mydomain/url/handle.php, also I use EPS8266 module which only could has the http client websocket and not https , your advise really appreciated Cpanel image sbr https://cloud.githubusercontent.com/assets/6232001/23093326/57fb941c-f5f5-11e6-82cc-33b0f8bb0195.jpg
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/varspool/Wrench/issues/100#issuecomment-280843396, or mute the thread https://github.com/notifications/unsubscribe-auth/AK-_snrfgB4WjOzNGa7q589PfGl-LajGks5rdufigaJpZM4MFEws.
hi, I'm sorry to open ticket for my question , but actually I'm not pro in php and with your instruction not figure out how to install it in my remote directory host , and really appreciate for couple of hints , in fact I've tried to hook url on my host to the Telegram webhoock call back bot, so at first step new update data inserted to MySQL database then websoket server give the update data to designated live client . can you tell me which files should be copy in designated host URL and what files should be modified? Thanks