php实时消息推送功能

1、做实时消息推送的方式大概有前端轮询、长连接等方式,前端轮询的话对服务器压力会很大,所以采用长连接的方式

2、安装启动workerman,下载地址https://www.workerman.net/download/web-msg-sender.zip,启动:linux系统cd到到框架目录里运行php start.php start -d。windows系统进入到框架目录里双击start_for_win.bat文件启动。

3、启动成功后在浏览器输入http://localhost:2123/可以看到一个demo界面

4、再在浏览器地址上输入http://localhost:2121?type=publish&to=1604023861000&content=你在干嘛,向uid为1604023861000发送“你在干嘛”的消息,

前端代码:

后端代码

// 指明给谁推送,为空表示向所有在线用户推送

$to_uid = "";

// 推送的url地址,使用自己的服务器地址

$push_api_url = "http://workerman.net:2121/";

$post_data = array(

"type" => "publish",

"content" => "这个是推送的测试数据",

"to" => $to_uid,

);

$ch = curl_init ();

curl_setopt ( $ch, CURLOPT_URL, $push_api_url );

curl_setopt ( $ch, CURLOPT_POST, 1 );

curl_setopt ( $ch, CURLOPT_HEADER, 0 );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );

curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));

$return = curl_exec ( $ch );

curl_close ( $ch );

var_export($return);

Copyright © 2088 斗念英雄·卡牌对战活动专区 All Rights Reserved.
友情链接