zhukangfeng / phpwebsocket

Automatically exported from code.google.com/p/phpwebsocket
0 stars 0 forks source link

Having a message sent when client connect? #24

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Change the WebSocket.Class.php code to have :

$client=socket_accept($this->master);
          if($client<0){ $this->log("socket_accept() failed"); continue; }
          else{ $this->connect($client); 
          $this->send($client,"Default message");

What is the expected output? What do you see instead?
Instead of having the "Default message" nothing is sent and all broke.

Any idea?

Original issue reported on code.google.com by mrdesjar...@gmail.com on 22 Feb 2011 at 2:03

GoogleCodeExporter commented 8 years ago
Here is the fix :

 $client=socket_accept($this->master);
          if($client<0)
          { 
            $this->log("socket_accept() failed"); 
            continue; 
          }
          else
          { 
            $this->log("---------Connect"); 
            $this->connect($client); 
            $this->log("---------Handshake"); 
            $user = $this->getuserbysocket($client);
            $this->processWhenConnection($user,"");

          }

...

Add in the class : 

  public function processWhenConnecting($user,&$msg)
  {
     /* Extend and modify this method to suit your needs */
    /* Basic usage is to echo incoming messages back to client */
    $this->log("---------processWhenConnecting"); 
    $this->processWhenConnection($user,$msg);
  }
  private function processWhenConnection($user, $msg)
  {
    $bytes = @socket_recv($user->socket,$buffer,2048,0);
    if(!$user->handshake){ $this->dohandshake($user,$buffer); }
    $this->log("---------Send"); 
    $this->processWhenConnecting($user,$msg);
    $this->send($user->socket,$msg);
  }

Original comment by mrdesjar...@gmail.com on 26 Feb 2011 at 3:58