PHP websocket Issues

Source: Internet
Author: User
Tags php websocket
 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->m                ASTER); 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-echo Incoming messages back to client */$this->sen        D ($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, $key 1, $key 2, $l 8b) = $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 ($key 1, $key 2, $l 8b).                              "/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 ($key 1, $key 2, $l 8b) {//get The numbers Preg_match_all ('/([/d]+)/'              , $key 1, $key 1_num);              Preg_match_all ('/([/d]+)/', $key 2, $key 2_num); Number crunching [/bad pun] $this->log ("Key1:".              $key 1_num = implode ($key 1_num[0])); $this->log ("Key2:".              $key 2_num = implode ($key 2_num[0]));              Count spaces Preg_match_all ('/([]+)/', $key 1, $key 1_SPC);              Preg_match_all ('/([]+)/', $key 2, $key 2_SPC);              How many spaces did it find? $this->log ("Key1 Spaces:".              $key 1_SPC = strlen (Implode ($key 1_spc[0])); $this->log ("Key2 Spaces:".              $key 2_SPC = strlen (Implode ($key 2_spc[0])); if ($key 1_spc==0| $key 2_spc==0) {$this->log("Invalid key"); return; }//some Math $key 1_sec = Pack ("N", $key 1_num/$key 1_spc);              Get the 32bit secret key, minus the other thing $key 2_sec = Pack ("N", $key 2_num/$key 2_spc); This needs checking, I ' m not completely sure it should is a binary string return MD5 ($key 1_sec. $key 2_sec. $l 8b,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:". $sk 1= $match [1]);}          if (Preg_match ("/sec-websocket-key2: (. *)/r/n/", $req, $match)) {$this->log ("Sec Key2:". $sk 2= $match [1]);}               if ($match =substr ($req,-8))                                                   {$this->log ("Last 8 bytes:". $l 8b= $match);}        Return Array ($r, $h, $o, $sk 1, $sk 2, $l 8b);          } 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 "; The 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-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": the $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)//message sent by itself is no longer received $this->se              nd ($user->socket, $msg);    }}} $master = new Chatbot ("127.0.0.1", 10035); ?>

Why my terminal PHP ss.php will error

Warning:date (): It is not safe for rely on the system ' s timezone settings. You is *required* to use the Date.timezone setting or the Date_default_timezone_set () function. In case you used any of those methods and you is still getting this warning, your most likely misspelled the timezone Iden Tifier. We selected the timezone ' UTC ' for now, but please set Date.timezone to select your timezone. in/applications/xampp/xamppfiles/htdocs/socket/ewq.php on line 18
Server started:2014-07-17 05:18:18/nlistening on:127.0.0.1 port 10035/nmaster socket:resource ID #5/n/n


Reply to discussion (solution)

This is due to the fact that when the date is called, if the timezone is set incorrectly, the resulting e_notice| E_warning Error

There are several scenarios to solve this problem:

Use Date_default_timezone_set () to set Date_default_timezone_set (' PRC ');
Ini_set (' Date.timezone ', ' Asia/shanghai ');
Modify error level configuration (not recommended)
Modify PHP.ini, explicitly set DATE.TIMEZONE=PRC

No time zone set, just set it up.

At the beginning of the code, add the time zone setting:
Date_default_timezone_set (' PRC '); Set China time zone

Date_default_timezone_set ("Asia/hong_kong");

Strict standards:only variables should be passed by reference In/applications/xampp/xamppfiles/htdocs/socket/websocket . class.php on line 22

Strict standards:only variables should be passed by reference In/applications/xampp/xamppfiles/htdocs/socket/websocket . class.php on line 22
And now it's a mistake.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.