php swoole 和 websocket的初次碰撞

php swoole 擴展彷彿爲php開發打開了一扇窗戶

php workman和swoole原來是兩個東東

  • swoole的使用範圍更廣,能作更多事應該

websocket的介紹

socket 與 websocket也是兩個東東

wss 配置 和 nginx的配置

  • wss swoole 服務端
<?php
error_reporting(E_ALL);
set_time_limit(0);

//建立websocket服務器對象,監聽0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9505, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$config = [
        'daemonize' => true,
        'ssl_key_file'  => '/workspace/file/2191282_www.havetatami.com.key',
        'ssl_cert_file' => '/workspace/file/2191282_www.havetatami.com.pem'
];
$ws->set($config);
//監聽WebSocket鏈接打開事件
$ws->on('open', function ($ws, $request) {
        var_dump($request->fd);
        $ws->push($request->fd, "hello, welcome\n");
});

//監聽WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
        var_dump($ws->connection_list());
        foreach ($ws->connection_list() as $fd){
                $ws->push($fd, "server: {$frame->data}");
        }
});

//監聽WebSocket鏈接關閉事件
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();
  • wss 瀏覽器端
<html>
<head>
<meta charset=utf-8 >
<title>test</title>
</head>

<body>

<h3 style="text-align:center;">test</h3>

<script>

var wsServer = 'wss://www.havetatami.com:9502/websocket';
var websocket = new WebSocket(wsServer);

function ssend(){
        websocket.send(1);
        setTimeout(ssend, 100);
}

websocket.onopen = function (evt) {
        console.log("Connected to WebSocket server.");
        //ssend();
};

websocket.onclose = function (evt) {
    console.log("Disconnected");
};

websocket.onmessage = function (evt) {
    console.log('Retrieved data from server: ' + evt.data);
};

websocket.onerror = function (evt, e) {
    console.log('Error occured: ' + evt.data);
};

</script>
</body>
</html>
  • nginx 配置
server{

        listen 443 ssl;

        server_name www.havetatami.com;

        ssl_certificate   /workspace/file/www.havetatami.com.chained.crt;
        ssl_certificate_key /workspace/file/www.havetatami.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        root /workspace/web/gitee/havetatami;


        location / {
                index index.php index.html index.htm;
                if (!-e $request_filename) {
                        rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                        rewrite ^(.*)$ /index.php?s=$1 last;
                        break;
                }
        }


        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        access_log /workspace/data/nginx/log/www_havetatami_com_access.log;

}

server{
        listen 80;
        server_name www.havetatami.com;
        return 301 https://$server_name$request_uri;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server 39.106.58.30:9505;
}
server{

        listen 9502 ssl;

        server_name www.havetatami.com;

        ssl_certificate /workspace/file/2191282_www.havetatami.com.pem;
        ssl_certificate_key /workspace/file/2191282_www.havetatami.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        root /workspace/web/gitee/havetatami;

        location /websocket {
                proxy_pass https://websocket;
                proxy_read_timeout 300s;
                proxy_send_timeout 300s;

                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        access_log /workspace/data/nginx/log/www_havetatami_com_access1.log;

}

其它參考文檔

題外話

  • 今天看了書,看到了一句話,安慰本身:一我的願意讓你愛他,就已是你很大的幸運了。你得好好愛,別辜負了對方的信任和期待。
相關文章
相關標籤/搜索