2

I'm working on a small project with PHP-Websocket.

The Server side is running with this https://github.com/ghedipunk/PHP-Websockets

Server side:

require "PHP-Websockets/websockets.php";

class Server extends WebSocketServer
{
    private $_connecting = 'Connecting..';
    private $_welcome = 'SOCKET SERVER!';


    protected function connected($user)
    {
        // Send welcome message to user when connected

    }    



    protected function process($user, $message)
    {
        // data sent from client
        $json = json_decode($message);

        //prepare data response to client
        $response = json_encode(array('type'=>'notify', 'message'=>'Client'.$user->id.' has sent a request.'));
        $this->send($user, $response);
    }

    protected function closed($user)
    {
        // Alert on server
        echo "User $user->id has closed the connection".PHP_EOL;
    }

    public function __destruct()
    {
        echo "Server Closed!".PHP_EOL;
    }
}

$addr = 'localhost';
$port = '2207';
$server = new Server($addr, $port);
$server->run();

Client Side:

<script>
var uri = "ws://localhost:2207";
function socket_connect(){
    socket = new WebSocket(uri);
    if(!socket || socket == undefined) return false;

    socket.onopen = function(){
        console.log('Connected to Server!');
    }

    socket.onerror = function(){
        console.log('Connection Failed!');
    }

    socket.onclose = function(){
        socket_log('Connection Closed! ')
    }

    socket.onmessage = function(e){
        //var response_data = e.data;
        var msg = JSON.parse(e.data); //PHP sends Json data to client
        console.log(msg.message);
        var new_response = '<li>'+msg.message+'</li>;
        $('#response').append(new_response);
    }
}

function send_data_to_server(data){
    if(!socket || socket == undefined) return false;
    socket.send(JSON.stringify(data));
}

$(document).ready(function(){
    socket_connect();


    $('#send_request').click(function(){
        send_data_to_server({message: 'Message sent from Client'});
    });
});
</script>

<input type="button" id="send_request" value="Send Request to Server" />

<ul id="responses"></ul>

Everything works fine with those code above.

When Client1 sends a request to Server, the Server responses to him instantly. BUT the other clients can not see the response message.

So I want to make it go further : When a client sends a request to the server, the server will response to ALL clients so that all client can see the message.

How can I do that?

Thanks in advance && sorry for my bad English!

Nấm Lùn
  • 1,277
  • 6
  • 28
  • 48
  • 1
    According to [this guide](https://github.com/ghedipunk/PHP-Websockets/wiki/Getting-Started), there might already be a `WebSocketServer::$users` traversable variable that you can `foreach`. – Passerby Mar 04 '14 at 11:11
  • I know this is old, but how did the ghedipunk api work for you? I'm considering it right now for a basic web sockets application. Has anyone on this thread found better? – Robert Jan 07 '16 at 05:32

2 Answers2

2

When a user connect, you need to add him to an array with every other users. When one disconnect, remove him from this array.

Then when you want to send a message to every user, iterate on the array and send the message to each connected user.

Loïc
  • 11,804
  • 1
  • 31
  • 49
0
WebSocketServer class has WebSocketServer::$users variable.

If you iterate over WebSocketServer::$users in split_packet function and then call main process it will work. In latest source code please iterate in line no-405.

//original
if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
        //$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message); 
        $this->process($user, $message);
      } else {
        $this->stderr("not UTF-8\n");
      }

//have to change

if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
        //$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message); 
        foreach($this->users as $usr){
            $this->process($usr, $message);
        }
      } else {
        $this->stderr("not UTF-8\n");
      }
sharif779
  • 174
  • 2
  • 10