websocket.php
1.4 KB
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
<?php
use Illuminate\Http\Request;
use SwooleTW\Http\Websocket\Facades\Websocket;
/*
|--------------------------------------------------------------------------
| Websocket Routes
|--------------------------------------------------------------------------
|
| Here is where you can register websocket events for your application.
|
*/
Websocket::on('connect', function ($websocket, Request $request) {
@file_put_contents(storage_path('logs/lyh_error.log'), var_export(111111, true) . PHP_EOL, FILE_APPEND);
// called while socket on connect
});
Websocket::on('disconnect', function ($websocket) {
@file_put_contents(storage_path('logs/lyh_error.log'), var_export(22222, true) . PHP_EOL, FILE_APPEND);
// called while socket on disconnect
});
Websocket::on('example', function ($websocket, $data) {
@file_put_contents(storage_path('logs/lyh_error.log'), var_export(3333, true) . PHP_EOL, FILE_APPEND);
$websocket->emit('message', $data);
});
Websocket::on('message', function ($websocket, $data) {
// 处理接收到消息时的逻辑
// 假设 $data 是客户端发送过来的数据
// 在这里您可以根据接收到的消息进行处理
@file_put_contents(storage_path('logs/lyh_error.log'), var_export(4444444, true) . PHP_EOL, FILE_APPEND);
// 回复消息给客户端
$websocket->send("Hello, Client! You sent: $data");
});