PHPのWEBSOCKETの問題

WBOY
リリース: 2016-06-23 13:53:43
オリジナル
976 人が閲覧しました

<?php    // Usage: $master=new WebSocket("localhost",12345);      class WebSocket{        var $master;        var $sockets = array();        var $users   = array();        var $debug   = false;               function __construct($address,$port){          error_reporting(E_ALL);          set_time_limit(0);          ob_implicit_flush();          $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");          socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1)  or die("socket_option() failed");          socket_bind($this->master, $address, $port)                    or die("socket_bind() failed");          socket_listen($this->master,20)                                or die("socket_listen() failed");          $this->sockets[] = $this->master;          $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){            $changed = $this->sockets;            socket_select($changed,$write=NULL,$except=NULL,NULL);            foreach($changed as $socket){              if($socket==$this->master){                $client=socket_accept($this->master);                if($client<0){ $this->log("socket_accept() failed"); continue; }                else{ $this->connect($client); }              }              else{                $bytes = @socket_recv($socket,$buffer,2048,0);                if($bytes==0){ $this->disconnect($socket); }                else{                  $user = $this->getuserbysocket($socket);                  if(!$user->handshake){ $this->dohandshake($user,$buffer); }                  else{ $this->process($user,$this->unwrap($buffer)); }                }              }            }          }        }        function process($user,$msg){          /* Extend and modify this method to suit your needs */          /* Basic usage is to echo incoming messages back to client */          $this->send($user->socket,$msg);        }        function send($client,$msg){          $this->say("> ".$msg);          $msg = $this->wrap($msg);          socket_write($client,$msg,strlen($msg));          $this->say("! ".strlen($msg));        }        function connect($socket){          $user = new User();          $user->id = uniqid();          $user->socket = $socket;          array_push($this->users,$user);          array_push($this->sockets,$socket);          $this->log($socket." CONNECTED!");          $this->log(date("d/n/Y ")."at ".date("H:i:s T"));        }        function disconnect($socket){          $found=null;          $n=count($this->users);          for($i=0;$i<$n;$i++){            if($this->users[$i]->socket==$socket){ $found=$i; break; }          }          if(!is_null($found)){ array_splice($this->users,$found,1); }          $index=array_search($socket,$this->sockets);          socket_close($socket);          $this->log($socket." DISCONNECTED!");          if($index>=0){ array_splice($this->sockets,$index,1); }        }        function dohandshake($user,$buffer){          $this->log("/nRequesting handshake...");          $this->log($buffer);          list($resource,$host,$origin,$key1,$key2,$l8b) = $this->getheaders($buffer);          $this->log("Handshaking...");          //$port = explode(":",$host);          //$port = $port[1];          //$this->log($origin."/r/n".$host);          $upgrade  = "HTTP/1.1 101 WebSocket Protocol Handshake/r/n" .                      "Upgrade: WebSocket/r/n" .                      "Connection: Upgrade/r/n" .                                      //"WebSocket-Origin: " . $origin . "/r/n" .                                      //"WebSocket-Location: ws://" . $host . $resource . "/r/n" .                      "Sec-WebSocket-Origin: " . $origin . "/r/n" .                          "Sec-WebSocket-Location: ws://" . $host . $resource . "/r/n" .                          //"Sec-WebSocket-Protocol: icbmgame/r/n" . //Client doesn't send this                      "/r/n" .                          $this->calcKey($key1,$key2,$l8b) . "/r/n";// .                              //"/r/n";          socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));          $user->handshake=true;          $this->log($upgrade);          $this->log("Done handshaking...");          return true;        }               function calcKey($key1,$key2,$l8b){              //Get the numbers              preg_match_all('/([/d]+)/', $key1, $key1_num);              preg_match_all('/([/d]+)/', $key2, $key2_num);              //Number crunching [/bad pun]              $this->log("Key1: " . $key1_num = implode($key1_num[0]) );              $this->log("Key2: " . $key2_num = implode($key2_num[0]) );              //Count spaces              preg_match_all('/([ ]+)/', $key1, $key1_spc);              preg_match_all('/([ ]+)/', $key2, $key2_spc);              //How many spaces did it find?              $this->log("Key1 Spaces: " . $key1_spc = strlen(implode($key1_spc[0])) );              $this->log("Key2 Spaces: " . $key2_spc = strlen(implode($key2_spc[0])) );              if($key1_spc==0|$key2_spc==0){ $this->log("Invalid key");return; }              //Some math              $key1_sec = pack("N",$key1_num / $key1_spc); //Get the 32bit secret key, minus the other thing              $key2_sec = pack("N",$key2_num / $key2_spc);              //This needs checking, I'm not completely sure it should be a binary string              return md5($key1_sec.$key2_sec.$l8b,1); //The result, I think        }               function getheaders($req){          $r=$h=$o=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-Key1: (.*)/r/n/",$req,$match)){ $this->log("Sec Key1: ".$sk1=$match[1]); }          if(preg_match("/Sec-WebSocket-Key2: (.*)/r/n/",$req,$match)){ $this->log("Sec Key2: ".$sk2=$match[1]); }          if($match=substr($req,-8))                                                                  { $this->log("Last 8 bytes: ".$l8b=$match); }          return array($r,$h,$o,$sk1,$sk2,$l8b);        }        function getuserbysocket($socket){          $found=null;          foreach($this->users as $user){            if($user->socket==$socket){ $found=$user; break; }          }          return $found;        }        function     say($msg=""){ echo $msg."/n"; }        function     log($msg=""){ if($this->debug){ echo $msg."/n"; } }        function    wrap($msg=""){ return chr(0).$msg.chr(255); }        function  unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }      }      class User{        var $id;        var $socket;        var $handshake;      }    ?>
ログイン後にコピー

<?php       // Run from command prompt > php -q chatbot.demo.php      include "ewq.php";      // Extended basic WebSocket as ChatBot      class ChatBot extends WebSocket{          function process($user,$msg){                            if (isset($user->first)) {                  $this->send($user->socket,'');                  $user->first = true;              }                             $this->say("< ".$msg);              switch($msg){                case "hello" : $this->send($user->socket,"hello human");                       break;                case "hi"    : $this->send($user->socket,"zup human");                         break;                case "name"  : $this->send($user->socket,"my name is Multivac, silly I know"); break;                case "age"   : $this->send($user->socket,"I am older than time itself");       break;                case "date"  : $this->send($user->socket,"today is ".date("Y.m.d"));           break;                case "time"  : $this->send($user->socket,"server time is ".date("H:i:s"));     break;                case "thanks": $this->send($user->socket,"you're welcome");                    break;                case "bye"   : $this->send($user->socket,"bye");                               break;                //default      : $this->send($user->socket,$msg." not understood");              break;                default      : $this->sendAll($user, $msg);              break;              }          }          function sendAll($currentUser, $msg){              $usersList = $this->users;              foreach ($usersList as $user){                if ($user !== $currentUser) // 自己发送的消息就不再接收一次了                  $this->send($user->socket, $msg);              }          }      }      $master = new ChatBot("127.0.0.1",10035);    ?>
ログイン後にコピー

私の端末の PHP ss.php がエラーを報告するのはなぜですか?

警告: date(): システムのタイムゾーン設定に依存するのは安全ではありません。 date.timezone 設定を使用することが *必須* です。 date_default_timezone_set( ) 関数。これらのメソッドのいずれかを使用してもこの警告が表示される場合は、タイムゾーン識別子のスペルを間違えている可能性があります。ここではタイムゾーン「UTC」を選択していますが、タイムゾーンを選択するには date.timezone を設定してください。 . in / Applications/XAMPP/xamppfiles/htdocs/socket/ewq.php 行 18
サーバー開始: 2014-07-17 05:18:18/nListening on: 127.0.0.1 port 10035/nMaster ソケット: リソース ID #5 /n/ n


ディスカッションに返信 (解決策)

これは、date を呼び出すときにタイムゾーン設定が正しくないために発生する E_NOTICE|E_WARNING エラーです

この問題を解決するには、いくつかの解決策があります:

date_default_timezone_set ( )Set date_default_timezone_set('PRC');
ini_set('date.timezone','Asia/Shanghai');
エラー レベル設定を変更します (推奨されません)
php.ini を変更して、date.timezone=PRC を明示的に設定します

タイムゾーンは設定されていません。設定するだけです

コードの先頭にタイムゾーン設定を追加します:
date_default_timezone_set('PRC'); //中国のタイムゾーンを設定します

date_default_timezone_set("Asia/Hong_Kong");
ログイン後にコピー

厳格な基準: 変数のみが必要です/Applications/XAMPP/xamppfiles/htdocs/socket/websocket.class.php の 22 行目で参照によって渡されます。

厳格な標準: /Applications/XAMPP/xamppfiles/htdocs/socket/websocket では変数のみが参照によって渡されます。 class.php の 22 行目
このエラーは再び報告されます

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!