PHP socket使用实例

参考:https://www.cnblogs.com/saonian/p/5504456.html

事先准备:

打开php.ini文件,确认extension=php_sockets.dll是否开启,有分号记得去掉分号

新建a.php:

a.php 在cli运行,即在文件目录下cmd,执行php a.php

代码如下:

<?php
class WS {
    var $master;
    var $sockets = array();
    var $debug = false;//true为调试模式,输出log日志
    var $handshake = array();

    function __construct($address, $port){
        // 建立一个 socket 套接字
        $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");//创建一个套接字Socket(通讯节点)
        socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");//设置Socket连接选项
        socket_bind($this->master, $address, $port) or die("socket_bind() failed");//把socket绑定在一个IP地址和端口上
        socket_listen($this->master,20) or die("socket_listen() failed");//监听已连接的套接字

        $this->sockets[] = $this->master;
        // debug
        $this->say("Server Started : ".date('Y-m-d H:i:s'));
        $this->say("Listening on   : ".$address." port ".$port);
        $this->say("Master socket  : ".$this->master."\n");

        while(true){
            $socketArr = $this->sockets;
            $write = NULL;
            $except = NULL;
            socket_select($socketArr, $write, $except, NULL);  //自动选择来消息的socket 如果是握手 自动选择主机
            foreach ($socketArr as $socket){
                if ($socket == $this->master){  // 连接主机的 client
                    $client = socket_accept($this->master);//接受套接字上的连接
                    if ($client < 0){
                        $this->log("socket_accept() failed");//debug
                        continue;
                    } else{
                        $this->connect($client);
                    }
                } else {
                    $bytes = @socket_recv($socket,$buffer,2048,0);//从已连接的socket接收数据
                    if ($bytes == 0){
                        $this->disConnect($socket);
                    }
                    else{
                        $key = array_search($socket, $this->sockets);//在数组中搜索给定的值,如果成功则返回首个相应的键名
                        if (empty($this->handshake) || !isset($this->handshake[$key]) || !$this->handshake[$key]){ // 如果没有握手,先握手回应
                            $this->doHandShake($socket, $buffer, $key);
                        }
                        else{// 如果已经握手,直接接受数据,并处理
                            $buffer = $this->decode($buffer);
                            echo $buffer.PHP_EOL;
                            $key = array_search($socket, $this->sockets);
                            $arr = $this->sockets;
                            array_shift($arr);//将数组开头的单元移出数组
                            foreach ($arr as $s){
                                $this->send($s, $buffer);
                            }
                        }
                    }
                }
            }
        }
    }

    function connect($socket){
        array_push($this->sockets, $socket);//将一个或多个单元压入数组的末尾(入栈)
        $this->say("\n" . $socket . " CONNECTED!");
        $this->say(date("Y-n-d H:i:s"));
    }
    function disConnect($socket){
        $index = array_search($socket, $this->sockets);
        socket_close($socket);
        $this->say($socket . " DISCONNECTED!");
        if ($index >= 0){
            echo 'unset index is:'.PHP_EOL;
            unset($this->sockets[$index]);
        }
    }


    //提取 Sec-WebSocket-Key 信息
    function getHeaders($req){
        $r = $h = $o = $key = null;
        if (preg_match("/GET (.*) HTTP/"              ,$req,$match)) { $r = $match[1]; }
        if (preg_match("/Host: (.*)\r\n/"             ,$req,$match)) { $h = $match[1]; }
        if (preg_match("/Origin: (.*)\r\n/"           ,$req,$match)) { $o = $match[1]; }
        if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)) { $key = $match[1]; }
        return array($r, $h, $o, $key);
    }
    //加密 Sec-WebSocket-Key
    function calcKey($key){
        //基于websocket version 13
        $accept = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
        return $accept;
    }
    //应答 Sec-WebSocket-Accept
    function doHandShake($socket, $buffer, $handKey){
        $this->log("\nRequesting handshake...");
        $this->log($buffer);
        list($resource, $host, $origin, $key) = $this->getHeaders($buffer);
        $this->log("Handshaking...");
        $upgrade  = "HTTP/1.1 101 Switching Protocol\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n";  //必须以两个回车结尾
        $this->log($upgrade);
        $sent = socket_write($socket, $upgrade, strlen($upgrade));
        $this->handshake[$handKey]=true;
        $this->log("Done handshaking...");
        return true;
    }


    //数据帧处理,解析数据帧
    function decode($buffer) {
        $len = $masks = $data = $decoded = null;
        $len = ord($buffer[1]) & 127;

        if ($len === 126) {
            $masks = substr($buffer, 4, 4);
            $data = substr($buffer, 8);
        }
        else if ($len === 127) {
            $masks = substr($buffer, 10, 4);
            $data = substr($buffer, 14);
        }
        else {
            $masks = substr($buffer, 2, 4);
            $data = substr($buffer, 6);
        }
        for ($index = 0; $index < strlen($data); $index++) {
            $decoded .= $data[$index] ^ $masks[$index % 4];
        }
        return $decoded;
    }


    //返回帧信息处理
    function frame($s){
        $a = str_split($s, 125);
        if (count($a) == 1){
            return "\x81" . chr(strlen($a[0])) . $a[0];
        }
        $ns = "";
        foreach ($a as $o){
            $ns .= "\x81" . chr(strlen($o)) . $o;
        }
        return $ns;
    }
    // 返回数据
    function send($client, $msg){
        $msg = $this->frame($msg);
        socket_write($client, $msg, strlen($msg));//写数据到socket缓存,获取字符串长度
    }


    function say($msg = ""){
        echo $msg . "\n";
    }
    function log($msg = ""){
        if ($this->debug){
            echo $msg . "\n";
        }
    }
}


new WS('localhost', 4000);

新建b.html:

<html>
<head>
    <title>demo</title>
    <script src="js/plug/jq/jquery-3.3.1.js"></script>
    <script src="js/plug/jq/jquery-3.3.1.min.js"></script>
</head>
<body>
<input type="text" id="content">
<input type="button" value="send" id="send">
<script type="text/javascript">
    var ws = new WebSocket("ws://localhost:4000");
    ws.onopen = function(){
        console.log("握手成功");
    }
    ws.onmessage = function(e){
        console.log("message:" + e.data);
    }
    ws.onerror = function(){
        console.log("error");
    }
    $("#send").click(function(){
        content = $("#content").val();
        console.log(content);
        ws.send(content);
    })
</script>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值