1

Hello i want to send message to all connected client but my code dont work (for one user its good)

require_once('websockets.php');
$userConnected = array();


class echoServer extends WebSocketServer {

  protected function process ($user, $exec) {
      foreach($userConnected as $u) {
        $this->send($u,'test');
      }
  }

  protected function connected ($user) {
    array_push($userConnected, $user);
  }

  protected function closed ($user) {
    if(($key = array_search($user, $userConnected)) !== false) {
        unset($userConnected[$key]);
    }
  }
}
soooo
  • 135
  • 1
  • 1
  • 12
  • To broadcast a message you need to use UDP, not TCP. – Jay Blanchard Oct 15 '15 at 14:25
  • how i can use UDP with websocket ? – soooo Oct 15 '15 at 14:28
  • 1
    You need to go study the documentation. The question that you're posing is awfully broad. – Jay Blanchard Oct 15 '15 at 14:31
  • ok thanks you i think i change for node js + socket io, now my code work with setInterval socket.sent('') – soooo Oct 15 '15 at 14:44
  • 1
    For 1, `$userConnected` is not going to be in scope inside any of those functions. It is a global variable and needs to be pulled into scope inside of the function using the `global` keyword. It would be better to store that as a class property instead. See [manual](http://php.net/manual/en/language.variables.scope.php#language.variables.scope.global) – Jeff Lambert Oct 15 '15 at 14:56
  • Oh yes its work great ! Thanks you Watcher, i need to allow TCP or UDP in iptables ? – soooo Oct 15 '15 at 15:04
  • @soooo http://stackoverflow.com/q/4657033/697370 – Jeff Lambert Oct 15 '15 at 15:09
  • 1
    @JayBlanchard are you sure the answer is using UDP? :) – Mjh Oct 15 '15 at 15:16
  • Yes @Mjh because using UDP is favorable for broadcast messages vs. TCP. – Jay Blanchard Oct 15 '15 at 17:04
  • He is asking to broadcast to "all connected client" so it cannot be UDP. – vtortola Oct 16 '15 at 07:09
  • @JayBlanchard without trying to sound rude here, I think you mixed something up. Websocket protocol is independent TCP-based protocol.. there's no UDP here, the proper way to perform websocket broadcast is to send the same message to every connected user which implies that underlying code has to be able to iterate over an array of connected people and issue the same message to everyone. – Mjh Oct 16 '15 at 10:41
  • Undoubtedly @Mjh, I understand what the OP is after and what you're saying. I am suggesting that the OP might want to think outside of the box and use a methodology better suited for broadcast messaging. It wasn't a mix up on my part. – Jay Blanchard Oct 16 '15 at 11:48
  • @JayBlanchard - alright, since I'm interested on how to do it - have you got any resources to read about it? We are talking about websockets here, which implies browser. How to perform broadcasting using UDP and delivering to browser (without tech like flash or java applets)? – Mjh Oct 16 '15 at 11:52
  • Are you OK with using nodejs? You can create a UDP listener that will forward UDP messages over websockets. – Jay Blanchard Oct 16 '15 at 11:57

0 Answers0