Utilize php websocket to implement message push or timely communication function of applet wss

Applet does not have message pushing function, to achieve this, you can use php's websocket

Configure the server.

1. Upload the web-msg-sender directory to the server (download address https://www.workerman.net/web-sender)
2. Modify the start_io.php file to modify the certificate path (nginx certificate)

$sender_io = new SocketIO(2120);

Modify to

$context = array(
    'ssl' => array(
        'local_cert'  => '/ssl/cn_bundle.crt',
        'local_pk'    => '/ssl/pk.key',
        'verify_peer' => false,
// PHPSocketIO Service
$sender_io = new SocketIO(2120,$context);

Certificates can be applied for through Tencent Cloud. After downloading, select the nginx certificate inside.
3. Check the environment with http://doc.workerman.net/faq/disable-function-check.html
If disable function vi/usr/local/php/etc/php.ini finds disable_functions to remove the disable item
4. After entering the web-msg-sender directory, execute the startup command windows system enters the framework directory, double-click the start_for_win.bat file to start

php start.php start -d

5. Open port 2120 2121 and join the Cloud Server Security Group
You can perform telnet domain name 2120 test in cmd to see if it is connected

6. Don't forget to add the domain name or ip to the wss domain name of the authorized domain name of the WeChat applet, otherwise it will not be accessible
2. Configure Client

1 Applet Client

weapp.socket.io.js File Download



const io = require('../../utils/weapp.socket.io.js')
     * Initial data for page
    data: {},

     * Lifecycle Functions--Listen for Page Loading
    onLoad: function () {
        var that = this;
        var uid='bitefu1';  
        const socket = io('https://Domain name or ip:2120')
        socket.on('connect', () => {
          console.log(socket.connected); // true
          socket.emit('login', uid);
        socket.on('new_msg', d => {
          console.log('received news: ', d)

2. Web Client

Easy to send tests Deliberately send a web-side for easy testing.

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src='https://cdn.bootcss.com/socket.io/2.0.3/socket.io.js'></script>
<script src='//cdn.bootcss.com/jquery/1.11.3/jquery.js'></script>
var domain='Domain name or ip';
<div style="width:850px;">   
//Current user uid:<b class="uid"></b><br>
//You can send a message to the current user via url:<a id="send_to_one" href="" target="_blank"><font style="color:#91BD09">http://<font class="domain"></font>:2121?Type=publish&to=<b class="uid"></b>&content=message content</font></a>
//You can push messages to all online users via url:<a href=""target="_blank" id="send_to_all"><font style="color:#91BD09">http://<font class="domain"></font>:2121?Type=publish&to=&content=message content</font></a>
    // Replace usage with real uid, here's a convenient demonstration of using timestamps
    var uid = Date.parse(new Date());
    $('#send_to_one').attr('href', 'http://'+domain+':2121/?type=publish&content=%E6%B6%88%E6%81%AF%E5%86%85%E5%AE%B9&to='+uid);
	$('#send_to_all').attr('href', 'http://'+domain+':2121/?type=publish&content=%E6%B6%88%E6%81%AF%E5%86%85%E5%AE%B9');

$(document).ready(function () {
    // Connect server
    var socket = io('https://'+domain+':2120');
    // Log on after connection
    socket.on('connect', function(){
    	socket.emit('login', uid);
    // When the backend pushes a message
    socket.on('new_msg', function(msg){
         $('#content').append('received message:'+msg+'<br>');
    // When backend pushes online data
    socket.on('update_online_count', function(online_stat){
<div id="online_box"></div>
<div id="content"></div>

Three-server messaging

// Indicates to whom to push, empty means to push to all online users
$to_uid = 'bitefu1';
// Push url address, use your own server address
$push_api_url = "http://Domain name or ip:2121/";
$content='Hello, everyone';
$post_data = array(
   "type" => "publish",
   "content" =>$content ,
   "to" => $to_uid, 

function curlget($url, $params='', $method = 'GET', $header = array(), $multi = false,$debug=false,$optsother='') {
	switch (strtoupper($method)) {/* Setting specific parameters based on request type */
		case 'GET':$opts[CURLOPT_URL] = $params?$url.'?'.http_build_query($params):$url;break;
		case 'POST':$params = $multi ? $params : http_build_query($params);//Determine whether to transfer files
    	$opts[CURLOPT_URL] = $url;$opts[CURLOPT_POST] = 1;$opts[CURLOPT_POSTFIELDS] = $params;break;			
		default:if($debug)E('Unsupported request method!');break;
	}$ch = curl_init();if($optsother && is_array($optsother))$opts=$opts+$optsother;curl_setopt_array($ch, $opts);$data = curl_exec($ch);$error = curl_error($ch);curl_close($ch);/* Initialize and execute curl requests */
	if($error && $debug){E('An error occurred in the request:'.$error);}
	return $data;

Keywords: Programming socket PHP SSL Nginx

Added by n8r0x on Mon, 02 Dec 2019 11:36:22 +0200