本文整理汇总了PHP中swoole_server::send方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::send方法的具体用法?PHP swoole_server::send怎么用?PHP swoole_server::send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $rdata)
{
$data = unserialize($rdata);
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$s = microtime(true);
$res = $serv->taskwait($data, 0.5, 0);
echo "use " . (microtime(true) - $s) * 1000 . "ms\n";
$serv->send($fd, PHP_EOL . "get " . $res['key'] . ": " . $res['val']);
break;
case "set":
$serv->task($data, 0);
$serv->send($fd, "OK\n");
break;
case "del":
$serv->task($data, 0);
break;
case "reload":
break;
default:
echo "server:" . $data . PHP_EOL;
}
}
}
示例2: onReceive
public function onReceive(swoole_client $cli, $data)
{
Trace::debug("*********cli {$this->fd} receive lenght:" . strlen($data) . ".");
false !== $this->serv->connection_info($this->fd) && $this->serv->send($this->fd, $this->cryptor->encrypt($data));
$this->lock = false;
$this->send();
}
示例3: onReceive
public function onReceive(swoole_server $server, $fd, $from_id, $data)
{
if (empty($data)) {
// 发送数据给客户端,请求包错误
$data = array('code' => 500, 'msg' => '非法请求', 'data' => null);
$server->send($fd, json_encode($data));
}
//局域网管理
$udpClient = $server->connection_info($fd, $from_id);
if ($udpClient['server_port'] == '9502') {
$data = str_replace("\r\n\r\n", "", $data);
switch ($data) {
case 'stop':
echo '服务器关闭: ' . date('Y-m-d H:i:s') . PHP_EOL;
$server->shutdown();
$server->send($fd, '服务器关闭成功');
break;
case 'reload':
echo 'Worker进程重启: ' . date('Y-m-d H:i:s') . PHP_EOL;
$server->reload();
$server->send($fd, '服务器Worker重启成功');
break;
default:
$server->send($fd, '非法请求');
break;
}
} else {
$data = json_decode($data, true);
echo $data['name'] . " say hello " . Test::WORD . "\n";
$server->send($fd, $data['name'] . " say hello " . Test::WORD);
}
}
示例4: errorHandler
function errorHandler()
{
if (!empty($this->current_fd)) {
$rsp = Proxy::shutdown_handler();
$rsp && $this->serv->send($this->current_fd, $rsp);
}
}
示例5: onReceive
public function onReceive(\swoole_server $server, $fd, $from_id, $data)
{
$data = trim($data);
if ($data == 'Stop') {
//停止
$server->shutdown();
$server->send($fd, 'success', $from_id);
} else {
if ($data == 'Stats') {
//状态
$data = $server->stats();
$server->send($fd, json_encode($data), $from_id);
} else {
if ($data == 'Reload') {
//重启
$server->reload();
$server->send($fd, 'success', $from_id);
} else {
if ($data == 'ResetTelnet') {
//重置所有telnet链接
} else {
$data = json_decode($data, true);
if ($data['act'] == 'Telnet' && isset($data['ip']) && isset($data['cmd'])) {
//执行命令
$service = TelnetEvent::getService($data['ip']);
if ($service == null) {
$result['code'] = 2;
} else {
if ($data['cmd'] > 0) {
if (isset($data['arg'])) {
$result['data'] = $service->exec($data['cmd'], $data['arg']);
} else {
$result['data'] = $service->exec($data['cmd']);
}
if ($result == null || $result['data'] == null) {
$result = ['code' => 3];
} else {
$result['code'] = 1;
}
} else {
$service->getSwitch()->connect();
if ($service->getSwitch()->isConnect()) {
$result['code'] = 1;
} else {
$result['code'] = 3;
}
}
}
$server->send($fd, json_encode($result), $from_id);
} else {
$server->send($fd, 'unknown command!', $from_id);
}
}
}
}
}
$server->close($fd);
}
示例6: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$info = $serv->connection_info($fd, $from_id);
if ($info['server_port'] == 9502) {
$serv->send($fd, "welcom Admin \n");
} else {
$serv->send($fd, 'Swoole:' . $data);
}
}
示例7: message
function message($fd, $code, $msg)
{
$this->serv->send($fd, json_encode(array('code' => $code, 'msg' => $msg)));
echo "[-->{$fd}]\t{$code}\t{$msg}\n";
if ($code != 0) {
$this->serv->close($fd);
}
return true;
}
示例8: sendResult
/**
* 发送回应
* @param $fd
* @param $code
* @param $msg
* @return bool
*/
protected function sendResult($fd, $code, $msg)
{
$this->serv->send($fd, $this->pack(array('code' => $code, 'msg' => $msg)));
//打印日志
if (is_string($msg) and strlen($msg) < 128) {
echo "[-->{$fd}]\t{$code}\t{$msg}\n";
}
//错误时自动关闭连接
if ($code != 0) {
$this->serv->close($fd);
}
return true;
}
示例9: onReceive
function onReceive($serv, $fd, $from_id, $data)
{
if (!isset($this->clients[$fd])) {
$this->serv->send($fd, "backend server not connected. please try reconnect.");
$this->serv->close($fd);
} else {
echo microtime() . ": client receive\n";
$backend_socket = $this->clients[$fd]['socket'];
$backend_socket->send($data);
echo microtime() . ": send to backend\n";
echo str_repeat('-', 100) . "\n";
}
}
示例10: onReceive
function onReceive($serv, $fd, $from_id, $data)
{
//没有空闲的数据库连接
if (count($this->idle_pool) == 0) {
//等待队列未满
if (count($this->wait_queue) < $this->wait_queue_max) {
$this->wait_queue[] = array('fd' => $fd, 'sql' => $data);
} else {
$this->serv->send($fd, "request too many, Please try again later.");
}
} else {
$this->doQuery($fd, $data);
}
}
示例11: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
//var_dump($serv->connection_info($fd, $from_id));
//echo "worker_pid=".posix_getpid().PHP_EOL;
//var_dump($fd, $from_id);
$serv->send($fd, 'Swoole: ' . $data, $from_id);
}
示例12: on_receive
/**
* listen on receive data
*
* @param \swoole_server $serv
* @param $fd
* @param $from_id
* @param $data
*/
public function on_receive(\swoole_server $serv, $fd, $from_id, $data)
{
// format passed
$data = str_replace(self::EOFF, '', $data);
$data = unserialize($data);
// check is signal for server shutdown
if (!empty($data['shutdown']) || !empty($data['reload'])) {
// try to clear timers
$this->__clear_timer();
$send = !empty($data['shutdown']) ? $serv->shutdown() : $serv->reload();
$send = serialize($send);
$send .= self::EOFF;
$ends = $serv->send($fd, $send);
if ($ends) {
$serv->close($fd);
}
return;
}
// dont send back
if (!empty($data['return']) && $data['return'] === FALSE) {
$back = serialize(TRUE);
$back .= self::EOFF;
$serv->send($fd, $back);
$serv->close($fd);
}
// now post data to a task
$param = ['fd' => $fd, 'data' => $data];
$param = serialize($param);
$param .= self::EOFF;
// worker direct
$this->on_task($serv, NULL, NULL, $param);
return;
}
示例13: onReceive
function onReceive(swoole_server $serv, $fd, $reactor_id, $data)
{
$op = strtolower(strstr($data, ' ', true));
//出队
if ($op == 'pop') {
if ($this->head_index >= $this->tail_index) {
$serv->send($fd, 'ERR empty' . self::EOF);
return;
}
$result = $this->leveldb->get('data_' . $this->head_index);
if ($result) {
$this->leveldb->delete('data_' . $this->head_index);
$this->head_index++;
$this->pop_count++;
$this->leveldb->set('_head_index', $this->head_index);
$serv->send($fd, 'OK ' . $result . self::EOF);
} else {
$serv->send($fd, 'ERR get() failed.' . self::EOF);
}
} elseif ($op == 'push') {
$result = $this->leveldb->set('data_' . $this->tail_index, substr($data, 5, strlen($data) - 5 - strlen(self::EOF)));
if ($result) {
$this->tail_index++;
$this->push_count++;
$this->leveldb->set('_tail_index', $this->tail_index);
$serv->send($fd, 'OK ' . self::EOF);
} else {
$serv->send($fd, 'ERR write to leveldb failed.' . self::EOF);
}
} elseif ($op == 'stats') {
$serv->send($fd, 'OK ' . var_export(['head_index' => $this->head_index, 'tail_index' => $this->tail_index, 'push_count' => $this->push_count, 'pop_count' => $this->pop_count], true) . self::EOF);
} else {
$serv->send($fd, 'ERR unsupported command [' . $op . ']' . self::EOF);
}
}
示例14: sendResult
/**
* 发送回应
* @param $fd
* @param $code
* @param $msg
* @return bool
*/
protected function sendResult($fd, $code, $msg, $data = null)
{
$array = array('code' => $code, 'msg' => $msg);
if ($data) {
$array['data'] = $data;
}
$this->serv->send($fd, $this->pack($array));
//打印日志
if (is_string($msg) and strlen($msg) < 128) {
echo "[-->{$fd}]\t{$this->currentCommand}\t{$code}\t{$msg}\n";
}
// //错误时自动关闭连接
// if ($code != 0)
// {
// $this->serv->close($fd);
// }
return true;
}
示例15: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
foreach ($serv->connections as $client) {
if ($fd != $client) {
$serv->send($client, $data);
}
}
}