PHP 框架使用的是 codeigniter4,前端用的是 uniapp,gatewayworker 從官網(wǎng)下載的 linux 版本,gatewayclient 使用composer 安裝。gatewayWorke、gatewayclient 和應(yīng)用使用同一服務(wù)器。
App/Gatewayworker/Applications/YourApp/start.gateway.php中只做了一處修改:將原來(lái)的:
$gateway = new Gateway(" tcp://0.0.0.0:8282");
改成了 websocket 協(xié)議:
$gateway = new Gateway("websocket://0.0.0.0:8282"); ```
App/Gatewayworker/Applications/YourApp/Events.php中,只對(duì) onConnect 做了以下修改:
public static function onConnect($client_id)
{
Gateway::sendToClient($client_id, json_encode(array(
'type' => 'init',
'client_id' => $client_id
)));
// 向當(dāng)前client_id發(fā)送數(shù)據(jù)
Gateway::sendToClient($client_id, "Hello $client_id\r\n");
// 向所有人發(fā)送
Gateway::sendToAll("$client_id login\r\n");
}```
前端使用 websocket 連接,在收到'init'和 client_id的信息后,通過(guò) bind_uid 接口向后臺(tái)上傳 uid 和 client_id 兩個(gè)參數(shù)。
后臺(tái)接收前端上傳的 uid 和 client_id 兩個(gè)參數(shù)后,進(jìn)行綁定。后臺(tái)代碼如下:
<?php namespace App\Controllers;
require_once '../vendor/autoload.php';
use CodeIgniter\Controller;
use App\Models\Push_model;
use GatewayClient\Gateway;
Class Push2 extends Controller
{
public function __construct()
{
Gateway::$registerAddress = '127.0.0.1:1238';
}
public function bind_uid()
{
$uid = $this->request->getPost('uid');
$client_id = $this->request->getPost('client_id');
try
{
Gateway::$registerAddress = '127.0.0.1:1238';
Gateway::bindUid($client_id, $uid);
$message = "binding is fixed";
Gateway::sendToUid($uid, json_encode($message));
}
catch (\Exception $e)
{
log_message('error', '[ERROR] {exception}', ['exception' => $e]);
}
}
問(wèn)題:
1.前端上傳后,收不到返回的消息
2.打印錯(cuò)誤日志,報(bào)如下內(nèi)容:
(Connection refused) /srv/vendor/workerman/gatewayclient/Gateway.php:1187
ERROR - 2021-08-28 08:28:03 --> [ERROR] stream_socket_client(): unable to connect to tcp://127.0.0.1:2903 (Connection refused) /srv/vendor/workerman/gatewayclient/Gateway.php:1188
已經(jīng)做過(guò)的嘗試:
1.服務(wù)器端口放行1238、2900、8282
2.重啟過(guò) gateway 服務(wù)器
以上調(diào)整沒(méi)有效果。
請(qǐng)高手指點(diǎn)。十分期待,感謝感謝!??!
謝謝回復(fù)。
服務(wù)端 debug 模式運(yùn)行,不輸出錯(cuò)誤。
服務(wù)端 php start.php status,提示:
Workerman[start.php] status
Workerman[start.php] not run
咋會(huì)not run,not run就是沒(méi)啟動(dòng)啊