欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP+swoole实现聊天群发功能

程序员文章站 2023-09-28 22:02:53
本篇文章主要介绍PHP+swoole实现聊天群发功能,感兴趣的朋友参考下,希望对大家有所帮助。 php代码: $serv = new swoole_websocket_server("127.0.0.1",3999); //服务的基本设置 $serv->set(array( 'worker_num' ......

本篇文章主要介绍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='{"name":"'+name+'","speak":\"'+speak+'"}';

return json_msg;

}

function pack_msg(type,msg){

return '{"type":"'+type+'","msg":'+msg+'}';

}

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('message').scrolltop = document.getelementbyid('message').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>

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

以上内容希望帮助到大家,很多phper在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、tp6,laravel,yii2,redis,swoole、swoft、kafka、mysql优化、shell脚本、docker、微服务、nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的官方群点击此处