1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> </head> <body> <h1>singwa-swoole-ws测试<h1> <script> var wsUrl = "ws://singwa.swoole.com:8812" ; var websocket = new WebSocket(wsUrl); //实例对象的onopen属性 websocket.onopen = function (evt) { websocket.send( "hello-sinwa" ); console.log( "conected-swoole-success" ); } // 实例化 onmessage websocket.onmessage = function (evt) { console.log( "ws-server-return-data:" + evt.data); } //onclose websocket.onclose = function (evt) { console.log( "close" ); } //onerror websocket.onerror = function (evt, e) { console.log( "error:" + evt.data); } </script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php /** * Created by PhpStorm. * User: baidu * Date: 18/3/1 * Time: 下午11:45 */ $server = new swoole_websocket_server( "0.0.0.0" , 8812); //$server->set([]); $server ->set( [ 'enable_static_handler' => true, 'document_root' => "/home/work/hdtocs/swoole_mooc/data" , ] ); //监听websocket连接打开事件 $server ->on( 'open' , 'onOpen' ); function onOpen( $server , $request ) { print_r( $request ->fd); } // 监听ws消息事件 $server ->on( 'message' , function (swoole_websocket_server $server , $frame ) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n" ; $server ->push( $frame ->fd, "singwa-push-secesss" ); }); $server ->on( 'close' , function ( $ser , $fd ) { echo "client {$fd} closed\n" ; }); $server ->start(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | <?php /** * ws 优化 基础类库 * User: singwa * Date: 18/3/2 * Time: 上午12:34 */ class Ws { CONST HOST = "0.0.0.0" ; CONST PORT = 8812; public $ws = null; public function __construct() { $this ->ws = new swoole_websocket_server( "0.0.0.0" , 8812); $this ->ws->set( [ 'worker_num' => 2, 'task_worker_num' => 2, ] ); $this ->ws->on( "open" , [ $this , 'onOpen' ]); $this ->ws->on( "message" , [ $this , 'onMessage' ]); $this ->ws->on( "task" , [ $this , 'onTask' ]); $this ->ws->on( "finish" , [ $this , 'onFinish' ]); $this ->ws->on( "close" , [ $this , 'onClose' ]); $this ->ws->start(); } /** * 监听ws连接事件 * @param $ws * @param $request */ public function onOpen( $ws , $request ) { var_dump( $request ->fd); if ( $request ->fd == 1) { // 每2秒执行 swoole_timer_tick(2000, function ( $timer_id ){ echo "2s: timerId:{$timer_id}\n" ; }); } } /** * 监听ws消息事件 * @param $ws * @param $frame */ public function onMessage( $ws , $frame ) { echo "ser-push-message:{$frame->data}\n" ; // todo 10s $data = [ 'task' => 1, 'fd' => $frame ->fd, ]; //$ws->task($data); swoole_timer_after(5000, function () use ( $ws , $frame ) { echo "5s-after\n" ; $ws ->push( $frame ->fd, "server-time-after:" ); }); $ws ->push( $frame ->fd, "server-push:" . date ( "Y-m-d H:i:s" )); } /** * @param $serv * @param $taskId * @param $workerId * @param $data */ public function onTask( $serv , $taskId , $workerId , $data ) { print_r( $data ); // 耗时场景 10s sleep(10); return "on task finish" ; // 告诉worker } /** * @param $serv * @param $taskId * @param $data */ public function onFinish( $serv , $taskId , $data ) { echo "taskId:{$taskId}\n" ; echo "finish-data-sucess:{$data}\n" ; } /** * close * @param $ws * @param $fd */ public function onClose( $ws , $fd ) { echo "clientid:{$fd}\n" ; } } $obj = new Ws(); |
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/8996473.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能