>  기사  >  PHP 프레임워크  >  워커맨이 단순 포격을 구현하는 방법

워커맨이 단순 포격을 구현하는 방법

尚
앞으로
2020-02-03 16:27:143348검색

연발[dàn mù](연발)은 인기 중국어로 인터넷에서 동영상을 볼 때 뜨는 해설 자막을 말합니다. Workerman을 사용하여 간단한 사격을 구현하는 방법을 살펴보겠습니다.

워커맨이 단순 포격을 구현하는 방법

관련 추천: "workerman Tutorial"

php 코드:

<?php  
use Workerman\Worker;  
require_once &#39;../Autoloader.php&#39;;//注意 这里要看你的workerman里的这个文件在哪 然后在进行修改  
  
$global_uid = 0;  
  
// 当客户端连上来时分配uid,并保存连接,并通知所有客户端  
function handle_connection($connection) {  
    global $text_worker, $global_uid;  
    // 为这个链接分配一个uid  
    $connection->uid = ++$global_uid;  
    foreach ($text_worker->connections as $conn) {  
        $conn->send("user[{$connection->uid}] online");  
    }  
}  
  
// 当客户端发送消息过来时,转发给所有人  
function handle_message($connection, $data) {  
    global $text_worker;  
    foreach ($text_worker->connections as $conn) {  
        $conn->send("user[{$connection->uid}] said: $data");  
    }  
}  
  
// 当客户端断开时,广播给所有客户端  
function handle_close($connection) {  
    global $text_worker;  
    foreach ($text_worker->connections as $conn) {  
        $conn->send("user[{$connection->uid}] logout");  
    }  
}  
  
$text_worker = new Worker("websocket://0.0.0.0:2347");  
  
$text_worker->count = 1;  
  
$text_worker->onConnect = &#39;handle_connection&#39;;  
$text_worker->onMessage = &#39;handle_message&#39;;  
$text_worker->onClose = &#39;handle_close&#39;;  
  
Worker::runAll();

HTML 코드:

<!DOCTYPE html>  

<html>  
<head>  
    <meta charset="UTF-8">  
    <title>Simple Chat</title>  
</head>  
<body>  
    <center> 
<h1>Simple Chat</h1>  
<input type="text" id="msg">  
<button type="button" id="send">send</button> 


<div id="content" style="width:200px;height:200px;border:1px solid red">
    假装在播放视频
    <marquee behavior="" direction=""></marquee>
</div>  
</center>
</body>  
  
<script type="text/javascript">  
    window.onload = function () {  
        var ws = new WebSocket("ws://127.0.0.1:2347");  
  
        document.getElementById("send").onclick = function () {  
            var msg = document.getElementById("msg").value;  
            ws.send(msg);  
        };  
  
        ws.onopen = function () {  
            console.log("连接成功");  
//            ws.send(&#39;raid&#39;);  
        };  
        ws.onmessage = function (e) {  
            document.getElementById("content").innerHTML += &#39;<marquee behavior="" direction="">&#39; + e.data + &#39;</marquee>&#39;;  
        };  
    };  
</script>  
  
</html>

이 기사는 다음에서 복제되었습니다: https://blog.csdn.net/woshiyangyunlong/article/details /80174653

더 많은 워커맨 지식을 알고 싶으시면 PHP 중국어 홈페이지workerman Framework튜토리얼 칼럼을 주목해주세요.

위 내용은 워커맨이 단순 포격을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제