Files
2025-10-02 10:33:06 +08:00

82 lines
1.9 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------
use Swoole\WebSocket\Server;
use think\facade\Cache;
class Tcp {
public $server;
public $redis;
public $key;
public function __construct() {
if(empty($this->redis)){
$this->redis = new Redis();
$this->redis ->connect('127.0.0.1',6379);
}
//创建Server对象监听 127.0.0.1:9501 端口
$server = new \Swoole\Server('127.0.0.1', 9501);
// $this->server->set(array(
//
// 'reactor_num' => 2, //reactor thread num
//
// 'worker_num' => 4, //worker process num
//
// 'backlog' => 128, //listen backlog
//
// 'max_request' => 50,
//
// 'dispatch_mode' => 1,
//
//// 'daemonize' => 1
//
// ));
//监听连接进入事件
$server->on('Connect', function ($server, $fd) {
echo "Client: Connect.\n";
});
//监听数据接收事件
$server->on('Receive', function ($server, $fd, $reactor_id, $data) {
$server->send($fd, "Server: {$data}");
});
//监听连接关闭事件
$server->on('Close', function ($server, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$server->start();
}
}
new Tcp();