HTML代码
<!DOCTYPE html>
<html>
<head>
<title>swoole chat room</title>
<meta charset="UTF-8">
<script type="text/javascript">
if(window.WebSocket){
var webSocket = new WebSocket("ws://101.200.223.48:9502");
webSocket.onopen = function (event) {
//webSocket.send("Hello,WebSocket!");
};
webSocket.onmessage = function (event) {
var content = document.getElementById('content');
content.innerHTML = content.innerHTML.concat('<p style="margin-left:20px;height:20px;line-height:20px;">'+event.data+'</p>');
}
var sendMessage = function(){
var data = document.getElementById('message').value;
webSocket.send(data);
}
}else{
console.log("您的浏览器不支持WebSocket");
}
</script>
</head>
<body>
<div style="width:600px;margin:0 auto;border:1px solid #ccc;">
<div id="content" style="overflow-y:auto;height:300px;"></div>
<hr/>
<div style="height:40px">
<input type="text" id="message" style="margin-left:10px;height:25px;width:450px;">
<button onclick="sendMessage()" style="height:28px;width:75px;">发送</button>
</div>
</div>
</body>
</html>
----------------------------------------------------------------------------------
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) {
$ws->push($request->fd, "hello, welcome to chatroom\n");
});
//监听WebSocket消息事件,其他:swoole提供了bind方法,支持uid和fd绑定
$ws->on('message', function ($ws, $frame) {
$msg = 'from'.$frame->fd.":{$frame->data}\n";
// 分批次发送
$start_fd = 0;
while(true)
{
// connection_list函数获取现在连接中的fd
$conn_list = $ws->connection_list($start_fd, 100); // 获取从fd之后一百个进行发送
var_dump($conn_list);
echo count($conn_list);
if($conn_list === false || count($conn_list) === 0)
{
echo "finish\n";
return;
}
$start_fd = end($conn_list);
foreach($conn_list as $fd)
{
$ws->push($fd, $msg);
}
}
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
$ws->close($fd); // 销毁fd链接信息
});
$ws->start();
然后服务器端直接php swoole.php启动swoole就行,多个浏览器访问swoole.html页面,可以模拟聊天室,查看推送。
匿名
2025-10-22
盖楼盖楼!
匿名
2025-08-11
沙发沙发
匿名
2025-08-10
https://at.oiik.cn/bing.html
匿名
2025-02-21
实用,我在开发https://minmail.app/时候使用到了
王飞翔
2024-12-30
亲爱的朋友:您好!中国疫情持续蔓延,很多人症状非常严重持久不愈,医院人满为患,各年龄段随地倒猝死的现象暴增,多省感染手足口、甲流、乙流、支原体、合胞及腺病毒的儿童不断攀升,目前各种天灾人祸,天气异象频发。古今中外的很多预言都说了这几年人类有大灾难,如刘伯温在预言中说 “贫者一万留一千,富者一万留二三”,“贫富若不回心转,看看死期到眼前”, 预言中也告诉世人如何逃离劫难的方法,真心希望您能躲过末劫中的劫难,有个美好的未来,请您务必打开下方网址认真了解,内有躲避瘟疫保平安的方法。网址1:https://github.com/1992513/www/blob/master/README.md?abhgc#1 网址2:bitly.net/55bbbb 网址3:https://d3ankibxiji86m.cloudfront.net/30gj 如打不开请多换几个浏览器试
匿名
2024-12-12
Backdata 搜索引擎网址提交入口:https://backdata.net/submit-site.html