wokerman 上在线聊天室本地实现

workerman上给的chat例子在win下实现过程

1、html代码

  1. <html>  
  2. <meta charset="UTF-8">  
  3. <body>  
  4.     <div id='tips'></div>  
  5.     <ul id="msg">  
  6.           
  7.     </ul>  
  8.     <input type="text" id ="say" />   
  9.     <input id="submit" onclick="chat()" type="submit">  
  10. </body>  
  11. <script type="text/javascript" src="jquery-2.0.0.min.js" ></script>  
  12. <script>  
  13.     var ws;  
  14.     $(function(){  
  15.         ws = new WebSocket("ws://192.168.10.249:2347");  
  16.         ws.onopen = function() {  
  17.             $('#tips').html('连接服务器成功!');  
  18.         };  
  19.     })  
  20.     function chat(){  
  21.         var txt = $('#say').val();  
  22.         if(txt == ''){  
  23.             return false;  
  24.         }  
  25.         ws.send(txt);  
  26.         $('#say').val('');  
  27.         ws.onmessage = function(e) {  
  28.             $('#msg').append('<li>'+e.data+'</li>');  
  29.         };  
  30.     }  
  31.       
  32. </script>  
  33.   
  34. </html>  

  35. 2、php代码

  36. [php]  view plain  copy
    1. <?php  
    2. use Workerman\Worker;  
    3. require_once '../workerman/Autoloader.php';  
    4.   
    5. $global_uid = 0;  
    6.   
    7. function handle_connection($connection)  
    8. {  
    9.     global $text_worker$global_uid;  
    10.   
    11.     $connection->uid = ++$global_uid;  
    12. }  
    13.   
    14.   
    15. function handle_message($connection$data)  
    16. {  
    17.     global $text_worker;  
    18.     foreach($text_worker->connections as $conn)  
    19.     {  
    20.             $conn->send("user[{$connection->uid}]: $data ");  
    21.         }  
    22. }  
    23.   
    24.   
    25. function handle_close($connection)  
    26. {  
    27.     global $text_worker;  
    28.     foreach($text_worker->connections as $conn)  
    29.     {  
    30.             $conn->send("user[{$connection->uid}] logout");  
    31.         }  
    32. }  
    33.   
    34.   
    35. $text_worker = new Worker("websocket://0.0.0.0:2347");  
    36.   
    37. $text_worker->count = 1;  
    38.   
    39. $text_worker->onConnect = 'handle_connection';  
    40. $text_worker->onMessage = 'handle_message';  
    41. $text_worker->onClose = 'handle_close';  
    42.   
    43. Worker::runAll();  

  37. 3、在cmd 命令行运行index.PHP

      首先,添加php在环境变量;在cd切换index.php文件所在目录  

    在php index.php 运行,窗口不能关闭


    在浏览器打开多个窗口测试



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值