AntonZelenin Posted June 17, 2017 Share Posted June 17, 2017 Is there a way to assign some unique value to a php socket? By default they get id's starting from 1, then 2, 3 and so on. Could I assign the value I want to a new socket connection?If no, how to identify specific user socket to send him a message? Just tell every connected user his temporary id and save it to database? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 17, 2017 Share Posted June 17, 2017 Is there a way to assign some unique value to a php socket? By default they get id's starting from 1, then 2, 3 and so on. Could I assign the value I want to a new socket connection?What kind of "value" are you thinking about? If no, how to identify specific user socket to send him a message? Just tell every connected user his temporary id and save it to database? Going to need context for that. Quote Link to comment Share on other sites More sharing options...
AntonZelenin Posted June 17, 2017 Author Share Posted June 17, 2017 (edited) What kind of "value" are you thinking about? Going to need context for that. Randomly generated value.. or maybe user id from database. About context. For example, several users connected to my server. And now I see this: print_r($this->clients); Array ( [0] => Resource id #5 [1] => Resource id #6 [2] => Resource id #7 ) So I can't figure out who is who. That's why I've got a question if I could specify id for a new socket) I could give you source code if needed:) Edited June 17, 2017 by AntonZelenin Quote Link to comment Share on other sites More sharing options...
AntonZelenin Posted June 17, 2017 Author Share Posted June 17, 2017 This piece of code accepts new sockets $changed = $this->clients; socket_select($changed, $null, $null, 0, 10); if (in_array($this->socket, $changed)) { $socket_new = socket_accept($this->socket); $this->clients[] = $socket_new; $header = socket_read($socket_new, 1024); $this->perform_handshaking($header, $socket_new, $this->host, $this->port); $found_socket = array_search($this->socket, $changed); unset($changed[$found_socket]); } Quote Link to comment Share on other sites More sharing options...
requinix Posted June 17, 2017 Share Posted June 17, 2017 I'd probably just stick with array_search, then use the key as your identifier for use in a second array containing the additional data. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.