前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP的socket扩展

PHP的socket扩展

作者头像
老雷PHP全栈开发
发布2020-07-02 14:59:49
1.4K0
发布2020-07-02 14:59:49
举报

我们了解了常用的网络协议,今天我们来了解下socket服务。我们可以基于tcp和udp来实现我们的socket服务,

包括tcp即时通讯,udp即时通讯,websocket服务,http服务等。

我们可以通过一张图来了解socket的实现流程图。

因为我们是使用PHP语言,所以我们先来了解一下PHP的socket扩展实现。

PHP有两个socket的扩展 sockets和streams 。

sockets

socket_create(AF_INET, SOCK_STREAM, SOL_TCP)

socket_write

socket_read

socket_close

客户端

socket_connect(socket, address,

服务端

socket_bind(sock, address,

socket_listen($sock)

socket_accept

Streams

客户端

stream_socket_client

fwrite

fread

fclose($fp);

服务端

stream_socket_server

stream_set_blocking

stream_select

stream_socket_accept conn

stream_socket_recvfrom

stream_socket_sendto

stream_socket_shutdown

代码语言:javascript
复制
<?php

class SocketService
{
    private $address;
    private $port;
    private $_sockets;
  public $clients;
  public $maxid=1000;
  public $groups;
  public $groupClients;
    public function __construct($address = '', $port='')
{
            if(!empty($address)){
                $this->address = $address;
            }
            if(!empty($port)) {
                $this->port = $port;
            }
    }
  public function bindUid($cliend_id,$uid){
    $this->uids[$uid][$cliend_id]=$cliend_id;
  }
  public function sendToUid($uid,$msg){
    //
    $clients=$this->getClientsByUid($uid);
    
  }
  public function addGroup($groupid,$name){
    
  }
  public function joinGroup($cliend_id,$groupid){
    $this->groupClients[$groupid][$cliend_id]=$cliend_id;
  }
  public function sendGroup($groupid,$msg){
    if(!isset($this->groupClients[$groupid])){
      return false;
    }    
    $cids=$this->groupClients[$groupid];
    if(!empty($cids)){
      foreach($cids as $cid){
        if(isset($this->$clients[$cids])){
          $this->send($this->$clients[$cids],$msg);
        }else{
          unset($this->groupClients[$groupid]);
        }
        
      }
    }    
    
  }
  public function sendAll($msg){
    foreach($this->clients as $kk=>$cc){
      if($kk>0){
        $this->send($cc, $msg);
      }                
    }
  }
  public function onConnect($client_id){
    echo  "Client client_id:{$client_id}   \n";
         
  }
  
  public function onMessage($client_id,$msg){
    //发给所有的
    $this->sendAll($msg);  
  }
  
  public function onClose($client_id){
    echo "$client_id close \n";
  }
  
    public function service(){
        //获取tcp协议号码。
        $tcp = getprotobyname("tcp");
        $sock = socket_create(AF_INET, SOCK_STREAM, $tcp);
        socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
        if($sock < 0)
        {
            throw new Exception("failed to create socket: ".socket_strerror($sock)."\n");
        }
        socket_bind($sock, $this->address, $this->port);
        socket_listen($sock);
        echo "listen on $this->address $this->port ... \n";
        $this->_sockets = $sock;
    }
 
    public function run(){
        $this->service();
        $this->clients[] = $this->_sockets;
        while (true){
            $changes = $this->clients;
            $write = NULL;
            $except = NULL;
            socket_select($changes,  $write,  $except, NULL);
            foreach ($changes as $key => $_sock){
                if($this->_sockets == $_sock){ //判断是不是新接入的socket
                    if(($newClient = socket_accept($_sock))  === false){
            unset($this->clients[$key]);
            continue;
                    }
                    $line = trim(socket_read($newClient, 1024));
          echo strlen($line)."\n";
                    $this->handshaking($newClient, $line);
          $this->maxid++;
                    $this->clients[$this->maxid] = $newClient;
                    $this->onConnect($this->maxid);
                } else {
          $ret=@socket_recv($_sock, $buffer,  2048, 0);
          echo strlen($buffer)."\n";
          if(strlen($buffer)<9){
              socket_shutdown ($_sock,2);
              unset($this->clients[$key]);
              $this->onClose($key);
              socket_clear_error();
          }else{
            $msg = $this->decode($buffer);
            $this->onMessage($key,$msg);
          }
                    
                }
            }
        }
    }
 
    /**
     * 握手处理
     * @param $newClient socket
     * @return int  接收到的信息
     */
    public function handshaking($newClient, $line){
 
        $headers = array();
        $lines = preg_split("/\r\n/", $line);
        foreach($lines as $line)
        {
            $line = chop($line);
            if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
            {
                $headers[$matches[1]] = $matches[2];
            }
        }
        $secKey = $headers['Sec-WebSocket-Key'];
        $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
        $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: $this->address\r\n" .
            "WebSocket-Location: ws://$this->address:$this->port/websocket/websocket\r\n".
            "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
        return socket_write($newClient, $upgrade, strlen($upgrade));
    }
  
  
  
  
  /**
     * 发送数据
     * @param $newClinet 新接入的socket
     * @param $msg   要发送的数据
     * @return int|string
     */
    public function send($newClinet, $msg){
        $msg = $this->encode($msg);
        if(false==@socket_write($newClinet, $msg, strlen($msg))){
      socket_shutdown ($newClinet,2);  
    }
    }
    /**
     * 解析接收数据
     * @param $buffer
     * @return null|string
     */
    public 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;
    }
 
    
  /**
  *打包数据
  **/
    public function encode($buffer) {
    $first_byte="\x81";
    $len=strlen($buffer);
    if ($len <= 125) {
            $encode_buffer = $first_byte . chr($len) . $buffer;
        } else {
            if ($len <= 65535) {
                $encode_buffer = $first_byte . chr(126) . pack("n", $len) . $buffer;
            } else {
                $encode_buffer = $first_byte . chr(127) . pack("xxxxN", $len) . $buffer;
            }
        }
    return $encode_buffer;
    }
 
    /**
     * 关闭socket
     */
    public function close(){
        return socket_close($this->_sockets);
    }
}
 
$sock = new SocketService('127.0.0.1','8000');
$sock->run();
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 老雷PHP全栈开发 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
即时通信 IM
即时通信 IM(Instant Messaging)基于腾讯二十余年的 IM 技术积累,支持Android、iOS、Mac、Windows、Web、H5、小程序平台且跨终端互通,低代码 UI 组件助您30分钟集成单聊、群聊、关系链、消息漫游、群组管理、资料管理、直播弹幕和内容审核等能力。适用于直播互动、电商带货、客服咨询、社交沟通、在线课程、企业办公、互动游戏、医疗健康等场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档