根据协议,如何使用WebSocket在服务器端发送和接收消息?
当我将数据从浏览器发送到服务器时,为什么我在服务器上看到看似随机的字节?它以某种方式编码数据?
框架如何在服务器→客户端和客户端→服务器方向上工作?
寻找Web套接字实现的Hello World Type示例:
这是来自php.net的Socket Create参考,但这看起来比Web Sockets更低.
我想在caniuse.com上使用这个Web套接字,现在可以在所有新的主流浏览器中实现.
谷歌搜索出现了这个Nets.TutsPlus网站,我可以在其中使用JavaScript示例代码...但我需要知道如何在PHP中实现服务器端,而不是像示例中那样在Java,Ruby或Node.js中实现.
PHP Socket Create是否相关?PHP本身是否支持Web套接字?我想在PHP实现的正确方向上的一点是有帮助的.
实际上这个教程有一个到phpwebsockets的断开链接......这是应该使用的库吗?
Websockets.org有一个测试应用程序,但没有提到PHP.
我想将数据从服务器推送到浏览器.我已经知道ob_flush()发送输出缓冲区的php函数了.我需要一些逻辑方面的帮助.我正在使用Facebook实时API,所以我想每次Facebook访问我的网站时将数据推送给用户.
这是我的代码,我试图将数据推送到浏览器,但它无法正常工作.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/event-stream');
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
error_log( "LOGS STARTS FROM HERE" );
if(isset($_GET['hub_challenge'])){
echo $_GET['hub_challenge'];
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
$updates = json_decode(file_get_contents("php://input"), true);
// Replace with your own code here to handle the update
// Note the request must complete within 15 seconds.
// Otherwise Facebook server will consider it a timeout and
// resend the push notification again.
print_r($updates);
ob_flush();
flush();
//file_put_contents('fb.log', print_r($updates,true), FILE_APPEND);
//error_log('updates = ' . print_r($updates, true));
}
?>
Run Code Online (Sandbox Code Playgroud) 我正在尝试将基于PHP的客户端连接到websocket服务器.
这是我一直在使用的代码,已经在不同的论坛上广泛发布.但由于某种原因,我无法让它发挥作用.
任何帮助,将不胜感激.
$host = 'host'; //where is the websocket server
$port = 443; //ssl
$local = "http://www.example.com/"; //url where this script run
$data = '{"id": 2,"command": "server_info"}'; //data to be send
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
////WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000); //receives the data included in the …Run Code Online (Sandbox Code Playgroud) 我的PHP代码在Amazon.com上发送查询以检索有关书籍的信息.当它接收到信息时,有两种可能性来执行以下程序.可以定义应该使用的内容,它必须考虑研究的图书利润总数.
我现在做的是,我发送第一个请求并检索结果总数.根据结果的数量,我为变量分配一个新值$queryUrl.如果结果数大于1200,程序应该按原样执行.
如果结果的数量小于1200,程序应该完成执行循环以遍历整个结果页面和其余的PHP代码但只有一次.
目前,如果结果少于1200.该程序遍历所有结果页面,但在PHP代码结束时停止.它执行所有代码几次,具体取决于查询的参数是$searchMonthUrlParam继承变量recupMonthJavaScript.
现在,我有
PHP:
//Retrieve variable value passed in POST from JavaScript
$pageNum = (isset($_POST["pageNum"]) && $_POST["pageNum"]) ? intval($_POST["pageNum"]) : 1;
$writeMode = (isset($_POST["writeMode"]) && $_POST["writeMode"]) ? $_POST["writeMode"] : "w";
$searchType = (isset($_POST["searchType"]) && $_POST["searchType"]) ? intval($_POST["searchType"]) : 0;
$month = (isset($_POST["month"]) && $_POST["month"]) ? intval($_POST["month"]) : date("n");
$year = (isset($_POST["year"]) && $_POST["year"]) ? intval($_POST["year"]) : date("Y") ;
$keyword = (isset($_POST["keyword"]) && strlen($_POST["keyword"])) ? $_POST["keyword"] : "";
$startMonth = (isset($_POST["startMonth"]) && strlen($_POST["startMonth"])) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个写在的IOS社交应用程序SWIFT.
后端是PHP, MySQL(用于事件处理),+一点NodeJS, Socket.io(用于实时聊天和通知)
我成功地聊了聊:
当用户发送消息时,Socket.io服务器通过以下方式处理它:
/为此,后端只是Socket.io服务器,它也处理数据库
工作良好.
但是有些事件并不是实时的,但我仍然希望通过Socket.io向给定用户发送通知.
例如:如果已经喜欢帖子,则将noti发送给帖子所有者
我已经编写了PHP文件,用于在数据库中保存,但是
我该怎么做通知部分,安全吗?
我想出了3个想法:
- 应用程序向我的PHP + MySQL后端发送Web请求,它处理那里的数据,然后在返回"成功"后,应用程序(SWIFT)向帖子所有者发送通知(通过Socket.io XCode pod)
func likePost(postId : Int, completion: @escaping (ActionResult?)->()){
let connectUrl = URL(string: appSettings.url + "/src/main/like.php")
var request = URLRequest(url: connectUrl!)
request.httpMethod = "POST"
let postString = "userId=\(userId)&session=\(session)&pId=\(postId)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) {
(data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
return completion(ActionResult(type: 0, code: 0, …Run Code Online (Sandbox Code Playgroud) 你好我想知道是否有什么东西可以让我们用它来使用php进行实时更新.
我学会了node.js希望解决这个问题,但我不知道托管一个使用php和node.js的网站的托管选项很少,我不喜欢它们.
这是我的技术堆栈:
什么是实时更新的最佳选择,而不必使用ajax?