Open KristienJones opened 4 years ago
OK. Update, I've managed to get it working but i've hit another wall.
Due to the nature of it being asynchronous, I cannot seem to manage variables correctly (Which I need in order to close a connection if the auth fails)
This is what I have so far:
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
$connection = $this->factory->createLazyConnection($this->uri);
$connection->query("select token from wsSessions where userID = ? limit 1",[1])->then(function(ConnectionInterface $conn,QueryResult $command) {
//logic here, if not authorized, close connection
$conn->close();
});
$connection->quit();
}
The problem I have, is that I cannot close the connection. $conn doesn't seem to parse correctly. And I can't update global variables and do the logic outside of the query function because it's asynchronous.
Any advice?
Hi,
I think you just need to add use($conn)
to use it in your anonymous function scope.
Like this
$connection->query("select token from wsSessions where userID = ? limit 1", [1])
->then(function(ConnectionInterface $conn,QueryResult $command) use($conn) { // <- here
//logic here, if not authorized, close connection
$conn->close();
});
Hi. I need some examples of using Async MySQL with an IoServer. I'll be using https://github.com/friends-of-reactphp/mysql most likely, but I can't get it to work correctly with Ratchet.
My Scenario: I need to authenticate connections using tokens that are stored on a database. The user connects with a token. Then once connected, the server authenticates and allows the connection to continue.
I'm entirely new to Ratchet and using composer also, so i'm clearly a little lost. I've spent the last day trying to figure it out but to no avail.
Below is my working code (Non-Async) server.php:
Chat.php
If anyone can point me in the right direction, that would be appreciated. Thanks.