Vue運用swoole 實現多人聊天

2022-09-12 06:12:13 字數 2312 閱讀 4584

1:專案目錄新建php檔案和html檔案,這裡我建了乙個swoole資料夾,chat.php 和chat.html 

2:將專案同步上線 

chat.php**:

<?php 

//建立websocket server物件,監聽0.0.0.0:9502埠

$ws = new swoole\websocket\server('

0.0.0.0

', 9507

);//

監聽websocket連線開啟事件

$ws->on('

open

', function ($ws, $request) );

//監聽websocket訊息事件

$ws->on('

message

', function ($ws, $frame)

else

$result =[

'error_code

' => 0

,

'data

' =>[

'data

' => $frame->data,

'class

' => $class

],

'msg

' => '

傳送成功'];

//傳送群聊中的所有人

$ws->push($fd, json_encode($result,256

)); }

});//

監聽websocket連線關閉事件

$ws->on('

close

', function ($ws, $fd) is closed\n";

});$ws->start();

3:chat.html

"en

">class="

">

class="

container

">

class="

left

">

class="

top

class="

right

">

class="

top">class="

name

">多人聊天室

class="

chat

" data-chat="

person2

">

for="

item in msglist

">

class="

item.data.class

">}

class="

write

">

"text

" v-model="

msg" @keyup.enter="

send()

"/>

"j**ascript:;

"class="

write-link send

" @click="

send()

">

<

4:寶塔終端執行chat.php 檔案,切記切換到檔案目錄下執行.php檔案

5:將寶塔9507埠放行

6:阿里雲伺服器9507埠放行

效果圖:

git參考:

上線參考:

swoole實現websocket推送

環境配置 swoole 1.9.3 centos6.5 虛擬機器 php7.01 思路 通過server中的collections取出fd 寫乙個 admin.html 管理員推送頁面 與 client.html 客戶端 html頁面的websocket自行可編寫 並在server.php中mess...

Swoole多程序的實現

使用swoole多程序上傳檔案到網宿伺服器 created by phpstorm.user leeo date 2016 9 29 time 15 26 引入檔案上傳類include once filehandleclass.php obj fetch new filehandleclass wo...

swoole多程序實現demo

php大任務劃分成多個小任務 體驗多程序的美,提高5倍效能 將迴圈執行的任務,劃分為多個程序執行,提高工作效率 假設我們現在有乙個通過curl抓取網頁內容的需求,需要抓取10個網頁,url位址通過陣列讀取,每個curl耗時2s。如果我們通過for迴圈來抓取這10個網頁,需要耗時20s,使用多程序我們...