You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying my hands on websockets and i think its working too. I created a dummy php file to start a websocket server then at onOpen i am attaching to user ID of each clients so i can access it later or can use their ID to send message to them. After starting and connecting to the websocket, i try to access list of users connected to the websocket like this
This file shows error that port is already in use, its right because websocket server is running on 20081 port and all users connected to websocket on this port too.
<br />
<b>Fatal error</b>: Uncaught RuntimeException: Failed to listen on "tcp: //0.0.0.0:20081": Address already in use (EADDRINUSE) in /home/bitnami/vendor/react/socket/src/TcpServer.php:184
Stack trace:
#0 /home/bitnami/vendor/react/socket/src/Server.php(77): React\Socket\TcpServer->__construct()
#1 /home/bitnami/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(59): React\Socket\Server->__construct()
#2 /opt/bitnami/apache/htdocs/net/websocket.php(171): Ratchet\Server\IoServer: :factory()
#3 /opt/bitnami/apache/htdocs/net/test.php(6): require_once('/opt/bitnami/ap...')
#4 {main
}
thrown in <b>/home/bitnami/vendor/react/socket/src/TcpServer.php</b> on line <b>184</b><br />
Websocket
<?phprequire_once("/home/bitnami/vendor/autoload.php");
useRatchet\MessageComponentInterface;;
useRatchet\ConnectionInterface;
useRatchet\Server\IoServer;
useRatchet\Http\HttpServer;
useRatchet\WebSocket\WsServer;
classWebSocketHandlerimplementsMessageComponentInterface {
private$clients;
publicfunction__construct() {
$this->clients = new \SplObjectStorage();
}
publicfunctiononOpen(ConnectionInterface$conn) {
// Handle new WebSocket connection$request = $conn->httpRequest;
// Get the action$action = $request->getHeaderLine("action");
// Check if the action is "addClient"if ($action === "addClient") {
// Add the user to the list of connected users$userId = $request->getHeaderLine('userId');
// Save the user ID in the resourceId property of the connection$conn->resourceId = $userId;
// Add the connection to the clients list$this->clients->attach($conn, $userId);
}
file_put_contents('zee1.log',print_r($userId.' '.$action,true));
}
publicfunctiononClose(ConnectionInterface$conn) {
// The connection is closed, remove it, as we can no longer send it messages$this->clients->detach($conn);
}
publicfunctiononError(ConnectionInterface$conn, \Exception$e) {
$conn->close();
}
privatefunctionfindClientConnection($userId) {
foreach ($this->clientsas$client) {
if ($client->resourceId === $userId) {
return$client;
}
}
returnnull;
}
publicfunctiongetClients(){
$connectedClients = [];
foreach ($this->clientsas$client) {
$connectedClients[$client->resourceId] = $client;
}
return$connectedClients;
}
}
$webSocketServer = newWebSocketHandler();
$server = IoServer::factory(
newHttpServer(
newWsServer(
$webSocketServer
)
),
20081
);
$server->run();
I stuck to get connected users from websocket, but not able to find any docs or help which can help me to get active/connected users (with their IDs).
The text was updated successfully, but these errors were encountered:
I can see in your example above that you use SplObjectStorage in order to store your clients, attach them inside the onOpen() method and detach them in onClose(). So it seems to me that you're already keeping track of your connected clients, what exactly is the problem you're facing here (What makes you think it doesn't work)?
@SimonFrings Is there any method or function which i can call to get ID of users currently connected to websocket. Right now i am doing it by saving user id at onOpen in text file and delete at onClose but i guess this is the hack not a solution. Can you please look at this point ?
I am trying my hands on websockets and i think its working too. I created a dummy php file to start a websocket server then at onOpen i am attaching to user ID of each clients so i can access it later or can use their ID to send message to them. After starting and connecting to the websocket, i try to access list of users connected to the websocket like this
This file shows error that port is already in use, its right because websocket server is running on 20081 port and all users connected to websocket on this port too.
Websocket
I stuck to get connected users from websocket, but not able to find any docs or help which can help me to get active/connected users (with their IDs).
The text was updated successfully, but these errors were encountered: