Yii2 WebSocketphp
即時通信Demogit
前提github
服務器安裝swooleweb
git clone https://github.com/swoole/swo...redis
cd swoole-srcjson
phpize服務器
./configure --enable-openssl -with-php-config=[PATH] #注意[PATH]爲你的php地址 開啓ssl用websocket
make && make installswoole
安裝yii2
composer執行
composer require "jianyan74/yii2-websocket"
或者在 composer.json 加入
"jianyan74/yii2-websocket": "^1.0"
配置
在 common/config/main.php 加入如下配置
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
在 console/config/main.php 加入如下配置。(注意:配置在controllerMap裏面)
// webSocket
'websocket' => [
//'class' => 'jianyan\websocket\console\WebSocketController',
'class' => 'console\controllers\WebSocketController',
'server' => 'jianyan\websocket\server\WebSocketServer', // 可替換爲本身的業務類繼承該類便可
'host' => '0.0.0.0',// 監聽地址
'port' => 9501,// 監聽端口
'type' => 'ws', // 默認爲ws鏈接,可修改成wss
'config' => [// 標準的swoole配置項均可以再此加入
'daemonize' => false,// 守護進程執行
'task_worker_num' => 4,//task進程的數量
// 'ssl_cert_file' => '',
// 'ssl_key_file' => '',
'pid_file' => __DIR__ . '/../../backend/runtime/logs/server.pid',
'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
'log_level' => 0,
],
],
使用
# 啓動
php ./yii websocket/start
# 中止
php ./yii websocket/stop
# 重啓
php ./yii websocket/restart
測試
<script>
var wsl = 'ws://[to/your/url]:9501'; // 若是是wss的改爲wss://[to/your/url]:9501
ws = new WebSocket(wsl);// 新創建一個鏈接
// 以下指定事件處理
ws.onopen = function () {
var login_data = '{"type":"login","nickname":"隔壁老王","head_portrait":"123","room_id":10001}';
ws.send(login_data);
};
// 接收消息
ws.onmessage = function (evt) {
console.log(evt.data);
/*ws.close();*/
};
// 關閉
ws.onclose = function (evt) {
console.log('WebSocketClosed!');
};
// 報錯
ws.onerror = function (evt) {
console.log('WebSocketError!');
};
// 聊天
function say(msg){
if(msg){
var data = '{"type":"say","to_client_id":"all","content":'+ msg +'}';
ws.send(data);
}
}
</script>
接口文檔
發送消息的格式所有以json字符串發過來
心跳
請求地址
ws://[to/you/url]:9501
參數
參數名 說明
type pong
無返回
進入房間
請求地址
ws://[to/you/url]:9501
參數
參數名 說明
type login
room_id 房間id
user_id 用戶id
nickname 用戶暱稱
head_portrait 用戶頭像
返回(1)
{
"type":"login",
"from_client_id":2,
"to_client_id":"all",
"time":"2018-04-10 16:41:29",
"count":"1",
"member":{
"fd":2,
"room_id":10001,
"user_id":1,
"nickname":"隔壁老王",
"head_portrait":"123"
}
}
返回(2)
當前登陸的人還會返回一個在線列表
{
"type":"list",
"from_client_id":2,
"to_client_id":2,
"time":"2018-04-10 16:41:29",
"list":[{
"fd":2,
"room_id":10001,
"user_id":1,
"nickname":"隔壁老王",
"head_portrait":"123"
}]
}
發言
請求地址
ws://[to/you/url]:9501
參數
參數名 說明
type say
to_client_id 對誰說話:默認 all
content 內容
返回
{
"type":"say",
"from_client_id":2,
"to_client_id":"all",
"time":"2018-04-10 16:43:00",
"content":"123"
}
送禮物
請求地址
ws://[to/you/url]:9501
參數
參數名 說明
type gift
gift_id 禮物id
返回
{
"type": "gift",
"from_client_id": 4,
"to_client_id": "all",
"gift_id": "禮物id",
"time": "2018-03-06 11:27:15"
}
離開房間
請求地址
ws://[to/you/url]:9501
參數
參數名 說明
type leave
返回
{
"type": "leave",
"from_client_id": 1,
"to_client_id": "all",
"count": 2,
"time": "2018-03-06 11:27:15"
}