简体   繁体   English

如何在php站点上的专用node.js服务器上使用Socket.io?

[英]How would I use Socket.io on a dedicated node.js server on a php site?

For a web application coded in PHP, I am powering a lot of the functionality that would traditionally utilize AJAX, like real-time chat, with Socket.io. 对于用PHP编码的Web应用程序,我提供了许多传统上会利用AJAX的功能,例如通过Socket.io进行实时聊天。 In order to use Websockets without straining Apache servers, I have servers running node.js specifically for the Websocket connections. 为了在不使Apache服务器紧张的情况下使用Websockets,我有专门针对Websocket连接运行node.js的服务器。 I intend to use DNode to allow the php scripts to call the node.js Websocket functions. 我打算使用DNode允许php脚本调用node.js Websocket函数。 How would I do this? 我该怎么做? Please provide a simple example if possible. 如果可能,请提供一个简单的示例。

I realize that this may not be the most efficient structure, but because of the large number of connections utilizing real-time functionality at the same time, running Websockets from PHP would be very server-intensive. 我意识到这可能不是最有效的结构,但是由于大量连接同时利用实时功能,因此从PHP运行Websockets将非常耗费服务器资源。 I also know that there are other ways of achieving real-time communication between server and client, like long polling and forever iframes, but there are specific reasons behind the choice of Websockets. 我还知道,还有其他方法可以实现服务器与客户端之间的实时通信,例如长轮询和永久iframe,但是选择Websockets背后有特定原因。

This is an alternative to send data from PHP to Node.js: 这是将数据从PHP发送到Node.js的一种替代方法:

<?php

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/img.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost:8082/test'); /* Nodejs */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>

More info here . 更多信息在这里

In app.js 在app.js中

app.post('/test', function(req, res){
    console.log("name: " + req.body.name);
...

I hope that is helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM