workerman简单例子

https://www.cnblogs.com/vania/p/6019425.html

workerman下载地址 http://www.workerman.net/

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
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
<!DOCTYPE html> 
<html> 
<head> 
<title>HTML5</title> 
<meta charset="utf-8" /> 
<script src="/public/admin/js/jquery.min.js"></script> 
<script> 
$(function() {     
    var socket; 
    var readyState = ["connecting", "connected", "closing", "closed"]; 
    /* 打开连接事件 */ 
    $("button:eq(0)").click(function() { 
        try { 
             /* 连接 */ 
             socket = new WebSocket("ws://192.168.56.1:2345"); 
                
             /* 绑定事件 */ 
             socket.onopen = function() { 
                 $("#msg").html("连接成功..."); 
             }; 
                
            socket.onmessage = function(e) { 
                 $("#msg").html($("#msg").html() + "<br />" + e.data); 
             }; 
                
             socket.onclose = function() { 
                 $("#msg").html($("#msg").html() + "<br />关闭连接..."); 
             }; 
        } catch(exception) { 
            $("#msg").html($("#msg").html() + "<br />有错误发生"); 
        } 
    }); 
       
    /* 发送数据事件 */ 
    $("button:eq(1)").click(function() { 
        /* 检查文本框是否为空 */ 
        if($("#data").val() == "") { 
            alert("请输入数据!"); 
            return; 
        } 
           
        try { 
            socket.send($("#data").val()); 
            $("#msg").html($("#msg").html() + "<br />发送数据:" + $("#data").val()); 
        } catch (exception) { 
            $("#msg").html($("#msg").html() + "<br />发送数据出错"); 
        } 
           
        /* 清空文本框 */ 
        $("#data").val(""); 
    }); 
       
    /* 断开连接 */ 
    $("button:eq(2)").click(function() { 
        socket.close(); 
    }); 
}); 
</script> 
</head> 
   
<body> 
<h1>WebSocket示例</h1> 
<input type="text" id="data" /> 
<button>打开连接</button> 
<button>发送数据</button> 
<button>关闭连接</button> 
<p id="msg"></p> 
</body> 
</html> 

php

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
<?php
use Workerman\Worker;
require_once '../Workerman/Autoloader.php';
 
// 创建一个Worker监听2345端口,使用http协议通讯
$ws_worker = new Worker("websocket://0.0.0.0:2345");
 
//启动4个进程对外提供服务 
$ws_worker->count = 4; 
 
$ws_worker->onWorkerStart = function($worker)
{
    echo "Worker starting...\n";
};
   
//当接收到客户端发来的数据后显示数据并回发到客户端 
$ws_worker->onMessage = function($connection, $data) { 
    global $ws_worker;
    //显示数据 
    echo "you just received: $data\n"; 
       
    //向客户端回发数据 
   if(!isset($connection->uid))
    {
       // 没验证的话把第一个包当做uid(这里为了方便演示,没做真正的验证)
       $connection->uid = $data;
       /* 保存uid到connection的映射,这样可以方便的通过uid查找connection,
        * 实现针对特定uid推送数据
        */
       $ws_worker->uidConnections[$connection->uid] = $connection;
       return $connection->send('login success, your uid is ' . $connection->uid);
    }
    // uid 为 all 时是全局广播
    list($recv_uid, $message) = explode(':', $data);
    // 全局广播
    if($recv_uid == 'all')
    {
        broadcast($message);
    }
    // 给特定uid发送
    else
    {
        sendMessageByUid($recv_uid, $message);
    }
};
 
// 向所有验证的用户推送数据
function broadcast($message)
{
   global $ws_worker;
   foreach($ws_worker->uidConnections as $connection)
   {
        echo '1/';
        $connection->send($message);
   }
}
 
// 针对uid推送数据
function sendMessageByUid($uid, $message)
{
    global $ws_worker;
    if(isset($ws_worker->uidConnections[$uid]))
    {
        $connection = $ws_worker->uidConnections[$uid];
        $connection->send($message);
    }
}
 
// 运行worker
Worker::runAll();
?>

可以单独给某个用户发信息,又可以给全部人发信息

    A+
发布日期:2018年08月07日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: