Open AKhateeb opened 8 years ago
@AKhateeb : Hello, we are working on this. Once we done with it, we will commit the required code. Thanks for using the library.
we are waiting...
@masoud005 : Sorry, you wait a long. Actually I stopped working on this project currently.
can you please share me the code for send individual notification?
@jibinprabha : Sorry, you wait a long. Actually I stopped working on this project currently.
I try make solution for this issue.
` class Chat implements MessageComponentInterface {
protected $repository;
private $users;
private $usersDb;
/**
* Default constructor of the class
*/
public function __construct() {
$this->repository = new ChatRepository;
$this->users = [];
$this->usersDb = [];
}
/**
* This function is used to add the connected machine to queue
* @param {object} $conn : Connection interface object
*/
public function onOpen(ConnectionInterface $conn) {
$this->repository->addClient($conn);
$this->users[$conn->resourceId] = $conn;
}
public function onClose(ConnectionInterface $conn) {
$this->repository->removeClient($conn);
unset($this->users[$conn->resourceId]);
unset($this->usersDb[$conn->resourceId]);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "The following error occured : " . $e->getMessage();
$client = $this->repository->getClientByConnection($conn);
if ($client !== null) {
$client->getConnection()->close();
$this->repository->removeClient($conn);
}
}
public function onMessage(ConnectionInterface $conn, $msg) {
$data = $this->parseMessage($msg);
$currClient = $this->repository->getClientByConnection($conn);
switch ($data->command) {
case 'reg':
$this->usersDb[$conn->resourceId] = $data->id;
break;
case 'msg':
if (isset($this->usersDb[$conn->resourceId])) {
foreach ($this->usersDb as $id => $idClient) {
if ($idClient == $data->target || $idClient == $data->from) {
$this->users[$id]->send(json_encode([
"msg" => $data->msg
]));
}
}
}
break;
}
}
private function parseMessage($msg) {
return json_decode($msg);
}
}`
`sendMsg: function (from, target, message) {
this.socket.send(JSON.stringify({
msg: message,
target: target,
from: from,
command: 'msg'
}));
},`
connectionOpen: function (evt) { this.open = true; this.regisId(); this.addSystemMessage("Connected"); },
regisId: function () { this.socket.send(JSON.stringify({ id: ID_USER, command: 'reg' })); }
easy, just use the client id as a reference.
How do I send message to specific client ? please work on this project to add this functionality, Thanks in Advance
@abhiburk you just need to send it to the specific socket id
Can u give me am example ?
@abhiburk basically I used an array to store client resource ids. Assume we store all connected clients to an array '$clients' and this is the structure of the clients array
[ 'id' = 1, 'con' => <resource id, when client connected, store clients resources here $conn > ]
and then I just do
$this->clients[1]['con']->send( message here... );
But what if their are so many users like 10000 say will it iterate through all 10000 for 1 client
@abhiburk no, we're not using loop there
$clients[1]['conn']
we're just directly get it from clients array.
And one thing i am confuse with is how does client id gets store ?
When client is connected.
` protected $clients;
public function __construct() { $this->clients = array(); } public function onOpen(ConnectionInterface $conn) { // store client resource id $this->clients[$conn->resourceId] = array( 'con' => $conn); } `
So every client connecting will have diffeent resource id ? And how will i compare my db userid and resource id
@abhiburk absolutely, every client has their own respective resource id. I can suggest a flow for what you need, refer below...
Yeah thats what i wanted to know thanks for your time and help
cud u give me your fb id in case if i am stuck bcz talking here is long process.thanks
@abhiburk you can post your problems and if you want help, you can get answers from stackoverflow.com I'm one of the folks there.
@mejuliver @kishor10d How can we use database within this Chat class ? Or basically if we want the information sent to socket server to be stored into database , how can we achieve it ? Thanks in advanc.
You can use the onmessage function if you want to store the messages to database. i recommend using document type database like mongodb, firebase, rethinkdb
I don't use this anymore. I'm using laravel + vue + redis
I have a website that should serve a student communication in our university, I found that this project is like a chat room, How can I convert it to another form where student can chat with a specific user using their ID ?? I so grateful for every help