PHP swoole websocket協議上機指南

這一上機實驗的開發環境用的是jetbrain公司的phpstormphp

上級步驟以下:css

  • 建立websocket服務端
<?php $server = new swoole_websocket_server('127.0.0.1',9502); function onopen($serv,$request) { echo "成功開啓,已經爲標示符".$request->fd."打開鏈接\n"; } $server->on('open','onopen'); $server->on('message',function(swoole_websocket_server $sw,$frame) { echo "opcode操做符---{$frame->opcode};操做數據---{$frame->data};fd數據來自句柄
---{$frame->fd};finish結束符號{$frame->finish}.\n
"; $sw->push($frame->fd,"推送的數據".$frame->data);//數據大小不過2M }); $server->on('close',function($serv,$fd) { echo "來自{$fd}的通訊結束\n"; }); $server->start(); ?>
  • 準備一個html頁面,並在頁面中附上websocket協議腳本
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>websocket協議</title>

    <!-- Bootstrap core CSS -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="../mycss/sticky-footer-navbar.css" rel="stylesheet">
</head>

<body>

<header>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
        <a class="navbar-brand" href="#">Fixed navbar</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarCollapse">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled" href="#">Disabled</a>
                </li>
            </ul>
            <form class="form-inline mt-2 mt-md-0">
                <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
        </div>
    </nav>
</header>

<!-- Begin page content -->
<main role="main" class="container">
    <h1 class="mt-5">Sticky footer with fixed navbar</h1>
    <p class="lead">Pin a fixed-height footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS. A fixed navbar has been added with <code>padding-top: 60px;</code> on the <code>body &gt; .container</code>.</p>
    <p>Back to <a href="../sticky-footer">the default sticky footer</a> minus the navbar.</p>
</main>

<footer class="footer">
    <div class="container">
        <span class="text-muted">Place sticky footer content here.</span>
    </div>
</footer>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../js/jquery-3.4.1.min.js"></script>
<script>
    var myurl = "ws://swsocks.com:9502";
    var ws = new WebSocket(myurl);
    ws.onopen = function (evt) {
        ws.send("hello server");
        console.log("向服務器打了個招呼")
    };
    ws.onmessage = function (evt) {
        console.log("喲吼~服務器返回了數據"+evt.data);
    };
    ws.onclose = function (evt) {
        console.log("關閉了面向服務器的鏈接");
    }

</script>
</body>
</html>
#精簡---websocket部分
<script> var myurl = "ws://swsocks.com:9502"; var ws = new WebSocket(myurl); ws.onopen = function (evt) { ws.send("hello server"); console.log("向服務器打了個招呼") }; ws.onmessage = function (evt) { console.log("喲吼~服務器返回了數據"+evt.data); }; ws.onclose = function (evt) { console.log("關閉了面向服務器的鏈接"); } </script>
  • 在phpstorm中運行寫好的前端頁面----右鍵點擊要運行的html標籤頁,運行Run index.html

  • 運行結果

 

  • 若是運行多個前端頁面實例將會顯示更多句柄
成功開啓,已經爲標示符1打開鏈接
opcode操做符---1;操做數據---hello server;fd數據來自句柄---1;finish結束符號1.
成功開啓,已經爲標示符2打開鏈接
opcode操做符---1;操做數據---hello server;fd數據來自句柄---2;finish結束符號1.
成功開啓,已經爲標示符3打開鏈接
opcode操做符---1;操做數據---hello server;fd數據來自句柄---3;finish結束符號1.
成功開啓,已經爲標示符4打開鏈接
opcode操做符---1;操做數據---hello server;fd數據來自句柄---4;finish結束符號1.
  •  優化,把服務端封裝成類
class myswserver
{
    const myip = '127.0.0.1';
    const myport = 9502;
    public $serv  = null;
    public function __construct()
    {
        $this->serv = new swoole_websocket_server('127.0.0.1',9502);
        $this->serv->on('open',[$this,'onopen']);#多是swoole特有回調函數形式,能夠對比php魔術方法call_user_func
        $this->serv->on('message',[$this,'onmessage']);
        $this->serv->on('close',[$this,'onclose']);
        $this->serv->start();
    }

    public function onopen($serv,$request)
    {
        echo "鏈接{$request->fd}已經創建\n";
    }
    public function  onmessage($serv,$frame)
    {
        echo "已經爲{$frame->fd}創建了鏈接,併發送了數據{$frame->data},其退出標示符爲{$frame->finish},其opcode爲{$frame->opcode}\n";
        $serv->push($frame->fd,$frame->data);//向客戶端推送數據
    }
    public function onclose($serv,$fd)
    {
        echo "關閉了{$fd}的鏈接\n";
    }
}

$serv = new myswserver();

$serv->start();
相關文章
相關標籤/搜索