Joke Collection Website - Public benefit messages - How to push messages to the client in a timely manner on the php backend?

How to push messages to the client in a timely manner on the php backend?

Use the following code to change the settings.

Backend code

push.php

lt;?php

use?Workerman\Worker;

require_once?'./Workerman/Autoloader.php';

$worker?=?new?Worker('websocket://0.0.0.0:1234');

/ /?The number of processes here must be set to 1

$worker-gt; count?=?1;

//?Establish an internal communication port after the worker process is started

$worker-gt;onWorkerStart?=?function($worker)

{

//?Open an internal port to facilitate the internal system to push data, Text protocol format? Text newline $inner_text_worker?=?new?Worker('Text://0.0.0.0:5678');

$inner_text_worker-gt;onMessage?=?function($connection,?$buffer)

{

global?$worker;

//?$data array format, with uid in it, means pushing data to the page with that uid

$data?=?json_decode($buffer,?true);

$uid?=?$data['uid'];

//?By workererman , push data to the page of uid

$ret?=?sendMessageByUid($uid,?$buffer);

//?Return the push result

$ connection-gt;send($ret'ok'?:?'fail');

};

$inner_text_worker-gt;listen();

};

//?Add a new attribute to save the mapping from uid to connection

$worker-gt;uidConnections?=?array();

//?The callback function $worker-gt that is executed when a message is sent from the client; onMessage?=?function($connection,?$data)use($worker)

{

//? Determine whether the current client has been verified, that is, whether the uid has been set

if(!isset($connection-gt;uid))

{

//?If there is no verification, treat the first package as uid (for the convenience of demonstration, no real verification is done here)

$connection-gt; uid?=?$data ;

/*?Save the mapping from uid to connection, so that you can easily find the connection by uid,

*?Push data for a specific uid

* /

$worker-gt;uidConnections[$connection-gt;uid]?=?$connection;

return;

}

};

//?When a client connection is disconnected

$worker-gt;onClose?=?function($co

nnection)use($worker)

{

global?$worker;

if(isset($connection-gt;uid))

{

//?Delete mapping when connection is disconnected

unset($worker-gt;uidConnections[$connection-gt;uid]);

}

};

//?Push data to all authenticated users

function?broadcast($message)

{

global?$worker;

foreach($worker-gt;uidConnections?as?$connection)

{

$ connection-gt;send($message);

}

}

//?Push data against uid

function?sendMessageByUid ($uid,?$message)

{

global?$worker;

if(isset($worker-gt;uidConnections[$uid]) )

{

$connection?=?$worker-gt;uidConnections[$uid];

$connection-gt;send($message);

return?true;

}

return?false;

}

//?Run all worker (actually only one is currently defined)

Worker::runAll();

Start the back-end service

php?push.php?start? -d

The front end receives the pushed js code

var?ws?=?new?WebSocket('ws://127.0.0.1:1234');

ws.onopen?=?function(){

var?uid?=?'uid1';

ws.send(uid);

};

ws.onmessage?=?function(e){

alert(e.data);

}; Code for backend push messages

//?Establish a socket connection to the internal push port

$client?=?stream_socket_client('tcp://127.0.0.1:5678',?$errno,?$errmsg, ?1,?STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);

//?The pushed data contains the uid field, indicating that it is pushed to this uid

$data?=?array('uid' =gt;'uid1',?'percent'=gt;'88');

//?Send data. Note that port 5678 is the port of the Text protocol. The Text protocol requires a newline at the end of the data. Symbol

fwrite($client,?json_encode($data)."\n");

//?Read push results

echo?frea

d($client,?8192); the uid here is not necessarily the user’s id, it can also be understood as the task id, taskid