PHP+swoole实现聊天群发功能

墨辰丷
发布: 2023-03-29 19:38:01
原创
2839 人浏览过

本篇文章主要介绍PHP+swoole实现聊天群发功能,感兴趣的朋友参考下,希望对大家有所帮助。

php代码:

$serv = new swoole_websocket_server("127.0.0.1",3999);
//服务的基本设置
$serv->set(array(
'worker_num' => 2,
'reactor_num'=>8,
'task_worker_num'=>1,
'dispatch_mode' => 2,
'debug_mode'=> 1,
'daemonize' => true,
'log_file' => __DIR__.'/log/webs_swoole.log',
'heartbeat_check_interval' => 60,
'heartbeat_idle_time' => 600,
));
$serv->on('connect', function ($serv,$fd){
// echo "client:$fd Connect.".PHP_EOL;
});
//测试receive
$serv->on("receive",function(swoole_server $serv,$fd,$from_id,$data){
// echo "receive#{$from_id}: receive $data ".PHP_EOL;
});
$serv->on('open', function($server, $req) {
// echo "server#{$server->worker_pid}: handshake success with fd#{$req->fd}".PHP_EOL;;
// echo PHP_EOL;
});
$serv->on('message',function($server,$frame) {
// echo "message: ".$frame->data.PHP_EOL;
$msg=json_decode($frame->data,true);
switch ($msg['type']){
case 'login':
$server->push($frame->fd,"欢迎欢迎~");
break;
default:
break;
}
$msg['fd']=$frame->fd;
$server->task($msg);
});
$serv->on("workerstart",function($server,$workerid){
// echo "workerstart: ".$workerid.PHP_EOL;
// echo PHP_EOL;
});
$serv->on("task","on_task");
$serv->on("finish",function($serv,$task_id,$data){
return ;
});
$serv->on('close', function($server,$fd,$from_id) {
// echo "connection close: ".$fd.PHP_EOL;
// echo PHP_EOL;
});
$serv->start();
function on_task($serv,$task_id,$from_id,$data) {
switch ($data['type']){
case 'login':
$send_msg="说:我来了~";
break;
default:
$send_msg="说:{$data['msg']['speak']}";
break;
}
foreach ($serv->connections as $conn){
if ($conn!=$data['fd']){
if (strpos($data['msg']['name'],"游客")===0){
$name=$data['msg']['name']."_".$data['fd'];
}else{
$name=$data['msg']['name'];
}
}else{
$name="我";
}
$serv->push($conn,$name.$send_msg);
}
return;
}
function on_finish($serv,$task_id,$data){
return true;
}
登录后复制

前端代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebSocket测试</title> 
<script language="javascript"type="text/javascript" src="jquery-1.12.0.min.js"> 
</script>
</head>
<body>
<h2>WebSocket Test</h2> 
昵称:<input type="text" id="name" size="5" value="游客"/> <input type="text" id="content"> 
<button onclick="speak_to_all()">发送</button>
<br/><br/>
<textarea id="message" style="overflow-x:hidden" rows="10" cols="50"></textarea> 
<p id="output"></p>
</body> 
<script language="javascript"type="text/javascript"> 
var wsUri ="ws://127.0.0.1:3999/"; 
var output; 
function init() { 
output = document.getElementById("output"); 
testWebSocket();
}
function testWebSocket() { 
websocket = new WebSocket(wsUri); 
websocket.onopen = function(evt) { 
onOpen(evt) 
}; 
websocket.onclose = function(evt) { 
onClose(evt) 
}; 
websocket.onmessage = function(evt) { 
onMessage(evt) 
}; 
websocket.onerror = function(evt) { 
onError(evt) 
}; 
}
function get_speak_msg(){
var name=document.getElementById("name").value;
var speak=document.getElementById("content").value;
var json_msg=&#39;{"name":"&#39;+name+&#39;","speak":\"&#39;+speak+&#39;"}&#39;;
return json_msg;
}
function pack_msg(type,msg){
return &#39;{"type":"&#39;+type+&#39;","msg":&#39;+msg+&#39;}&#39;;
}
function onOpen(evt) {
append_speak("已经联通服务器.........");
speak_msg=get_speak_msg();
send_msg=pack_msg("login",speak_msg);
doSend(send_msg);
}
function onClose(evt) { 
append_speak("俺老孙去也!");
} 
function onMessage(evt) {
append_speak(evt.data);
}
function onError(evt) {
alert(evt.data);
}
function doSend(message) { 
websocket.send(message);
}
function append_speak(new_msg){
document.getElementById("message").value=document.getElementById("message").value+new_msg+"\n";
document.getElementById(&#39;message&#39;).scrollTop = document.getElementById(&#39;message&#39;).scrollHeight;
}
function speak_to_all(){
send_msg=pack_msg("speak",get_speak_msg());
if(document.getElementById("content").value==""){
return;
}
doSend(send_msg);
document.getElementById("content").value="";
}
init();
</script>
</html>
登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP模块化安装详细步骤教程

PHP邮箱验证实例详解

php文件操作的方法及实例详解

以上是PHP+swoole实现聊天群发功能的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!