B
B
BloodVIRUS2018-05-07 18:51:33
PHP
BloodVIRUS, 2018-05-07 18:51:33

How to work with socket.io?

Hello. I am mastering socket.io, I want to make a smart little thing in which a certain group of users will perform some actions together (not a chat).
I learned to receive messages for some events and send them to all \ specific user.
I can not understand how to store some of my data inside the socket. If I take and add $temp[] = 1 to the connection event, then for any number of connections, my temp variable will always have one element. Which is logical, the script worked and died, but it's not logical, it's like a server, the connection is holding ... Therefore, please help me figure it out, suggest a few examples of how ..
-how to store some data in a socket? (I can use mysql for this, but this is a crutch?
- how to get the connection id? Is there a connection id at all?
- how to work with rooms? create, delete, get a list of users\connections in them?
I found only this on the git, by the method of scientific poke I got to the possibility of sending a message to the user that he was connected and not to everyone.

use Workerman\Worker;
use PHPSocketIO\SocketIO;

// listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function($socket)use($io){
  $socket->on('chat message', function($msg)use($io,$socket){
    $io->emit('chat message', $msg);
  $socket->emit('chat message', "Отправлено".date('h:m:s',time()));
  
  });
  
});

Worker::runAll();

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Buchnev, 2018-05-07
@butschster

WebSocket is a full-duplex communication protocol (can transmit and receive simultaneously) over a TCP connection, designed to exchange messages between a browser and a web server in real time.
It only transmits data, all logic is on the server side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question