一、创建 workerman 服务,并启动

用以接收 websocket 消息

1、安装 workerman 服务

1
composer require workerman/workerman

2、根目录下创建文件:**server.php**

方法 1:使用回调函数

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
<?php
require_once __DIR__.'/vendor/autoload.php';

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$worker = new Worker();

$worker->onWorkerStart = function ($worker) {

$con = new AsyncTcpConnection('ws://127.0.0.1:7600/wcf/socket_receiver');

// websocket握手成功后
$con->onWebSocketConnect = function (AsyncTcpConnection $con,) {
$con->send('hello');
};

// 当收到消息时
$con->onMessage = function (AsyncTcpConnection $con, $data) {
echo $data.PHP_EOL;
//方法一:使用回调函数给控制器传递消息
$gogogoInstance = new \app\wx\controller\Gogogo();
call_user_func_array([$gogogoInstance, 'getMsgData'], [$data]);
};

$con->onError = function (AsyncTcpConnection $con, $code, $msg) {
echo "error $code $msg\n";
};

$con->connect();
};

Worker::runAll();

方法 2:使用 thinkphp 容器及中间件

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
<?php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/app/wx/controller/Index.php';

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

use think\Container;

$worker = new Worker();

$worker->onWorkerStart = function ($worker) {

$con = new AsyncTcpConnection('ws://127.0.0.1:7600/wcf/socket_receiver');

// websocket握手成功后
$con->onWebSocketConnect = function (AsyncTcpConnection $con,) {
$con->send('hello');
};

// 当收到消息时
$con->onMessage = function (AsyncTcpConnection $con, $data) {
echo $data.PHP_EOL;
$webSocketService = Container::getInstance()->make(\App\Service\WebSocketService::class);
$webSocketService->handleMessage($data);

};

$con->onError = function (AsyncTcpConnection $con, $code, $msg) {
echo "error $code $msg\n";
};

$con->connect();
};

Worker::runAll();

3、创建中间件(如果需要【方法 2 代码】)

文件:app\service\WebSocketService.php

1
2
3
4
5
6
7
8
9
10
11
12
namespace app\service;

class WebSocketService
{

public function handleMessage($message)
{
// 这里可以调用控制器方法
$controller = new \app\wx\controller\Gogogo();
$controller->getMsgData($message);
}
}

4、创建控制器处理数据

文件:app/wx/controller/Gogogo.php

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace app\wx\controller;

class Gogogo
{

public function getMsgData($message)
{
// 处理接收到的WebSocket消息
file_put_contents("t.txt", gettype($message).PHP_EOL, FILE_APPEND);
file_put_contents("t.txt", $message.PHP_EOL, FILE_APPEND);
// 你可以在这里进行其他业务逻辑处理
}
}