swoole簡單的聊天室demo(修正版)

2021-09-16 23:57:09 字數 1243 閱讀 7015

搜了一下,以前的很多態別文章,都是可能採集的,基本一樣,錯誤都一樣,所以自己寫了個,發來共享一下。

咱們可是抄襲文件還有網際網路的,請確保你的伺服器已經安裝swoole1.7.7+版本擴充套件,9502埠未占用而且可以開啟

swoole文件:

效果:

swoole.html頁面

傳送    

swoole.php**:

<?php 

$ws = new swoole_websocket_server("0.0.0.0", 9502);

// 設定配置

$ws->set(

array(

'daemonize' => false, // 是否是守護程序

'max_request' => 10000, // 最大連線數量

'dispatch_mode' => 2,

'debug_mode'=> 1,

// 心跳檢測的設定,自動踢掉掉線的fd

'heartbeat_check_interval' => 5,

'heartbeat_idle_time' => 600,

));//監聽websocket連線開啟事件

$ws->on('open', function ($ws, $request) );

//監聽websocket訊息事件,其他:swoole提供了bind方法,支援uid和fd繫結

$ws->on('message', function ($ws, $frame) \n";

// 分批次傳送

$start_fd = 0;

while(true)

$start_fd = end($conn_list);

foreach($conn_list as $fd)

}});//監聽websocket連線關閉事件

$ws->on('close', function ($ws, $fd) is closed\n";

$ws->close($fd); // 銷毀fd鏈結資訊

});$ws->start();

然後直接php swoole.php啟動swoole就行,多個瀏覽器訪問swoole.html頁面,可以模擬聊天室,檢視推送。

簡單聊天室

include include include include include include include include include include pthread t thread 2 void send msg void ip msg if connect sockfd,struct ...

Linux UDP簡單聊天室

伺服器端 include include include include include include include include int sockfd 0 建立結構體用來存放客戶端資訊 typedef struct node node t node t link head node t h ...

go 簡單聊天室

package main import fmt net strings time 建立使用者結構體型別 type cline struct var onlinemap map string cline 建立全域性 channel 傳遞使用者訊息 var message make chan strin...