首先服务端配置监听
swoole.php
<?php
return [
'http' => [
'enable' => true,
'host' => '0.0.0.0',
'port' => 8000,
'worker_num' => swoole_cpu_num(),
'options' => [],
],
'websocket' => [
'enable' => true,
'handler' => \think\swoole\websocket\Handler::class,
'ping_interval' => 25000,
'ping_timeout' => 60000,
'room' => [
'type' => 'table',
'table' => [
'room_rows' => 8192,
'room_size' => 2048,
'client_rows' => 4096,
'client_size' => 2048,
],
'redis' => [
'host' => '127.0.0.1',
'port' => 6379,
'max_active' => 3,
'max_wait_time' => 5,
],
],
'listen' => [
// 'connect' => \app\listener\WsConnect::class,
'close' => 'app\listener\WsClose',//关闭事件
// 'test' => \app\listener\WsTest::class
'event' => \app\listener\WebsocketTest::class,//事件监听
],
'subscribe' => [],
],
'rpc' => [
'server' => [
'enable' => false,
'host' => '0.0.0.0',
'port' => 9000,
'worker_num' => swoole_cpu_num(),
'services' => [],
],
'client' => [],
],
//队列
'queue' => [
'enable' => false,
'workers' => [],
],
'hot_update' => [
'enable' => env('APP_DEBUG', false),
'name' => ['*.php'],
'include' => [app_path()],
'exclude' => [],
],
//连接池
'pool' => [
'db' => [
'enable' => true,
'max_active' => 3,
'max_wait_time' => 5,
],
'cache' => [
'enable' => true,
'max_active' => 3,
'max_wait_time' => 5,
],
//自定义连接池
],
'ipc' => [
'type' => 'unix_socket',
'redis' => [
'host' => '127.0.0.1',
'port' => 6379,
'max_active' => 3,
'max_wait_time' => 5,
],
],
'tables' => [],
//每个worker里需要预加载以共用的实例
'concretes' => [],
//重置器
'resetters' => [],
//每次请求前需要清空的实例
'instances' => [],
//每次请求前需要重新执行的服务
'services' => [],
];
WebsocketTest.php
<?php
declare (strict_types = 1);
namespace app\listener;
use think\Container;
use think\swoole\Websocket;
class WebsocketTest
{
public $websocket = null;
public function __construct(Container $container){
$this->websocket = $container->make(Websocket::class);
}
/**
* 事件监听处理
* @param $event
*/
public function handle($event)
{
echo '接收到事件,' . $event->type . '---' . $event->data;
echo '--------';
var_dump($event);
$func = $event->type;
$this->$func($event);
}
/**
* 测试类型
* @param $event
*/
public function test($event)
{
$msg = json_encode($event->data,256);
$this->websocket->emit('callback', $msg);
}
}
WsClose.php
<?php
declare (strict_types = 1);
namespace app\listener;
class WsClose
{
/**
* 事件监听处理
*
* @return mixed
*/
public function handle($event)
{
//
echo '已经断开了';
}
}
页面客户端代码
<html>
<head>
<title>websocket</title>
</head>
<body>
<h1>websocket功能</h1>
<input id="msg" type="text"/>
<button onclick="send()">发送</button>
<script>
var ws = new WebSocket("ws://192.168.50.114:8000");
ws.onopen = function (){
console.log("连接成功");
var sendObj = {};
sendObj.type = 'connect';
sendObj.data = 'connect success';
console.log('msg',JSON.stringify(sendObj));
ws.send(JSON.stringify(sendObj));
}
ws.onclose = function () {
console.log("连接失败")
}
ws.onmessage = function (evt) {
console.log("数据已接收",evt);
}
function send(){
console.log('运行到这里了');
var msg = document.getElementById('msg').value;
var sendObj = {};
sendObj.type = 'mtest';
sendObj.data = msg;
console.log('msg',JSON.stringify(sendObj));
ws.send(JSON.stringify(sendObj));
}
</script>
</body>
</html>
测试截图
能够处理连接、断开、发送消息。