时间:2021-05-26
由于本文的能力有限,有好多聊天逻辑的细节没有实现,只实现了群发,具体代码如下所示:
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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><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> <div id="output"></div></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>推荐文章:php安装swoole扩展的方法 使用swoole扩展php websocket示例
以上代码给大家分享了PHP+swoole实现简单多人在线聊天群发的相关代码,希望本文所述对大家有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言Swoole可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。使用PHP+Swoole作为网络通信框架,
本文实例讲述了php基于swoole实现的异步处理任务功能。分享给大家供大家参考,具体如下:安装swoole:下载官方swoole压缩包,解压进入目录$cdsw
本文实例讲述了php异步多线程swoole用法。分享给大家供大家参考。具体分析如下:swoole重新定义PHP语言的高性能网络通信框架,提供了PHP语言的异步多
前言Swoole内核团队开设的专栏,会逐渐投入精力写文章介绍Swoole的开发历程,实现原理,应用实践等,大家可以更好的交流,共同学习,建设PHP生态。协程调度
本文实例讲述了PHP使用swoole编写简单的echo服务器。分享给大家供大家参考,具体如下:server.php代码如下:serv=newswoole_ser