Socket.php
970 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* @remark :
* @name :Socket.php
* @author :lyh
* @method :post
* @time :2023/8/24 10:43
*/
namespace App\Helper;
require __DIR__ . '/vendor/autoload.php';
use WebSocket\Client;
class Socket
{
private $client;
public $serverIp = '127.0.0.1';
public $serverPort = '9555';
public function __construct() {
$socketUrl = "ws://{$this->serverIp}:{$this->serverPort}";
$this->client = new Client($socketUrl);
}
/**
* @remark :发送消息
* @name :send
* @author :lyh
* @method :post
* @time :2023/8/31 10:18
*/
public function send($data) {
$this->client->send($data);
}
public function receive() {
return $this->client->receive();
}
/**
* @remark :关闭连接
* @name :close
* @author :lyh
* @method :post
* @time :2023/8/31 10:21
*/
public function close() {
$this->client->close();
}
}