may-meow-cloud / socket-server
此包已被废弃且不再维护。未建议替代包。
API
v0.1.3
2017-03-24 19:19 UTC
This package is not auto-updated.
Last update: 2021-11-11 16:08:19 UTC
README
简单的套接字服务器和客户端实现
要求
- PHP 5.5 及以上
- Composer
包含内容
- 服务器:是一个简单的套接字服务器。监听指定端口以接收API调用。
- 客户端:是套接字服务器的客户端。
- 代理:是一个类似于应用程序的客户端,在定义的时间内检查云端服务器以执行操作。
所需条件
在这里您可以看到每个应用程序的需求。对于服务器,建议使用静态IP地址,否则您必须使用DDNS服务之一。
应用程序 | 公网IP地址 | 端口转发 | 服务器 |
---|---|---|---|
服务器 | 是 | 是 | 否 |
客户端 | 否 | 否 | 是 |
代理 | 否 | 否 | 是 - 云端 |
- 服务器和客户端需要每次IP更改时更新配置。
- 代理不依赖于IP。(云端服务器使用DNS名称)
安装
克隆仓库
git clone https://github.com/MayMeow/uuid.git
使用Composer - 即将推出
使用
服务器
为您服务器创建文件,例如 server.php
并加载Socket服务器
$server = new \MayMeow\Cloud\Sockets\SocketServer('tcp://0.0.0.0:4443'); // ... here you can define actions // run server $server->connect();
要启动服务器,请调用 php server.php
服务器将在任何IP地址上的4443端口监听。您将看到已连接客户端和调用的操作日志。
此服务器将针对客户端调用的任何操作返回NotFound。在您可以使用操作之前,必须注册它们。
注册操作
// Register server actions. This block must be before $server->connect() $server->addAction('Ping', new \MayMeow\Cloud\Sockets\Actions\Ping()); $server->addAction('Pong', new \MayMeow\Cloud\Sockets\Actions\Pong()); // From version 0.0.2 // Names are defined in Actions php file $server->addAction(new \MayMeow\Cloud\Sockets\Actions\Pong());
创建操作
namespace MayMeow\Actions; class Ping extends AbstractAction { /** * From Version 0.0.2 you have to define action name in configure function */ protected function configure() { $this ->setName('Ping'); } /** * @param null $what * @return string * * On input is simple string */ public function response($what = null) { return trim(exec('ping ' . $what)); } }
客户端
创建文件 client.php
并加载SocketClient
$client = new \MayMeow\SocketClient('tcp://:4443');
调用操作并获取响应
$response = $client->run([ 'action' => 'Pong', 'data' => 'May' ]); echo $response; // {"server":"MayMeow Sock Server","response":"OK","code":"200","action":"Pong","data":"Hello World May"}
版本 0.0.2 及以上
$response = $client-> ->setAction('Pong') ->setData('May')->run(); echo $response; // {"server":"MayMeow Sock Server","response":"OK","code":"200","action":"Pong","data":"Hello World May"}
代理
要使用代理,您需要在我们的云端服务器上创建账户,但目前该功能不对公众用户开放。如果您想进行测试,请联系我们。
- 创建代理
require_once 'vendor/autoload.php'; require_once 'config/paths.php'; $agent = new \MayMeow\Cloud\Sockets\CloudRunner(); // ... place for register actions $agent->addAction(new \MayMeow\Cloud\Sockets\Actions\Ping()); $agent->run();
-
创建名为
config/agent.json
的配置文件。内容请参照代理视图页面(云端服务器)上的说明。 -
运行您的代理并享受吧 :)
响应
成功
{ "server":"MayMeow Sock Server", "response":"OK","code":"200", "action":"ActionName", "data":"Requested data" }
未找到
{ "server":"MayMeow Sock Server", "response":"NotFound", "code":"404", "action":"ActionName" }
作为服务运行
- 作为root用户创建文件
sudo touch /lib/systemd/system/mcloudapi.service
- 更新文件,内容如下
[Unit] Description=May Meow Cloud API Daemon After=network.target [Service] ExecStart=/usr/bin/php /opt/mcloudapi/srv.php WorkingDirectory=/opt/mcloudapi Type=simple Restart=always RestartSec=10 KillMode=process [Install] WantedBy=multi-user.target
- 使systemd加载新的单元
sudo systemctl daemon-reload
- 启动服务
sudo systemctl start mcloudapi.service
- 启动后运行(可选)
sudo systemctl enable mcloudapi.service
贡献
- 进行Fork操作!
- 创建您的功能分支:
git checkout -b my-new-feature
- 提交您的更改:
git commit -am '添加一些功能'
- 推送到分支:
git push origin my-new-feature
- 提交一个Pull Request。
致谢
许可协议
MIT