沃梦达 / 编程问答 / php问题 / 正文

如何集成nodeJS + Socket.IO 和PHP?

How to integrate nodeJS + Socket.IO and PHP?(如何集成nodeJS + Socket.IO 和PHP?)

本文介绍了如何集成nodeJS + Socket.IO 和PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在四处寻找,以找到一种在 nodeJS 和 PHP 之间进行通信的好方法.这是一个想法:nodeJS 仍然很新,仅使用它开发完整的应用程序可能有点棘手.此外,您可能只需要在项目的一个模块中使用它,例如实时通知、聊天……并且您想使用 PHP 管理所有其他内容,因为它对您来说可能更容易(并且您可以利用现有的框架,如 CodeIgniter 或 Symfony).

I have recently been looking around, to find a good way to communicate between nodeJS and PHP. Here is the idea : nodeJS is still quite new, and it can be kind of tricky to develop a full application only with it. Moreover, you may need it only for one module of your project, like realtime notifications, chat, ... And you want to manage all the other stuff with PHP, because it is probably more easy for you (and you can take advantage of the existing frameworks, like CodeIgniter or Symfony).

我想要一个简单的解决方案;我不想使用 cURL 或第三台服务器在 Apache 和 Node 服务器之间进行通信.我想要的是能够在简单的 Javascript 客户端从节点捕获事件.

I would like to have an easy solution ; I don't want to use cURL, or a third server to communicate between Apache and Node servers. What I want is to be able to catch events from node in simple Javascript, client-side.

我没有找到任何完整的答案,大部分时间客户端由节点服务器运行,因此不适用于我的情况.于是我爬了所有可能的话题,终于找到了我的答案;我会试着分享这个,并让一切都清楚.

I didn't find any answers that where complete, most of the time client-side was running by the node server and so not applicable in my case. So I crawled all the possible topics, and finally find my answer ; I'll try to share this, and to have a point where it's all clear.

希望这可以帮助一些人!;)

Hope this can help some people ! ;)

推荐答案

所以,首先,我把我的项目放在 github 上,如果你想访问完整的代码:https://github.com/jdutheil/nodePHP

So, to begin with, I put my project on github, if you want access to the full code: https://github.com/jdutheil/nodePHP

这是一个非常简单的示例项目:网络聊天.你只有一个作者和消息,当你按下发送时,它被保存在一个 mysql 数据库中.这个想法是发送实时更新,并进行真正的对话.;) 为此,我们将使用 nodeJS.

It is a very simple example project: a web chat. You just have an author and message, and when you press send it is saved in a mysql database. The idea is to send real time updates, and have a real conversation. ;) We'll use nodeJS for that.

PHP代码就不说了,这里真的很简单,没啥意思;我想向您展示的是如何集成您的 nodeJS 代码.

I won't talk about PHP code, it is really simple and not interesting here; what I want to show you is how to integrate your nodeJS code.

我使用 express 和 Socket.IO,所以一定要使用 npm 安装这些模块.然后,我们创建一个简单的 nodeJS 服务器:

I use express and Socket.IO, so be sure to install those modules with npm. Then, we create a simple nodeJS server:

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );

var app = express();
var server = http.createServer( app );

var io = socket.listen( server );

io.sockets.on( 'connection', function( client ) {
    console.log( "New client !" );

    client.on( 'message', function( data ) {
        console.log( 'Message received ' + data.name + ":" + data.message );

        io.sockets.emit( 'message', { name: data.name, message: data.message } );
    });
});

server.listen( 8080 );

我们在新用户连接时注册了我们的事件回调;每次我们收到一条消息(代表一条聊天消息)时,我们都会将其广播给每个连接的用户.现在,棘手的部分:客户端!那是我大部分时间花的部分,因为我不知道包含哪个脚本才能在没有 nodeServer 的情况下运行 Socket.IO 代码(因为客户端页面将由 Apache 提供).

We registered our events callback when a new user is connected ; every time we receive a message (represents a chat message), we broadcast it to every users connected. Now, the tricky part: client-side! That the part that took me most of the time, because I didn't know which script include to be able to run Socket.IO code without the nodeServer (because client page will be served by Apache).

但是一切都已经完成了;当您使用 npm 安装 Socket.IO 模块时,/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js 中有一个脚本可用;我们将包含在我们的 PHP 页面中的脚本,在我的例子中:

But everything is already done; when you install Socket.IO module with npm, a script is available in /node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js; that the script we will include in our PHP page, in my case:

    <script src="js/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
    <script src="js/nodeClient.js"></script>

最后,我的 nodeClient.js,我们只需连接到节点服务器并等待事件更新我们的页面.;)

And to finish, my nodeClient.js, where we simply connect to the node server and wait for event to update our page. ;)

var socket = io.connect( 'http://localhost:8080' );

$( "#messageForm" ).submit( function() {
    var nameVal = $( "#nameInput" ).val();
    var msg = $( "#messageInput" ).val();

    socket.emit( 'message', { name: nameVal, message: msg } );

    // Ajax call for saving datas
    $.ajax({
        url: "./ajax/insertNewMessage.php",
        type: "POST",
        data: { name: nameVal, message: msg },
        success: function(data) {

        }
    });

    return false;
});

socket.on( 'message', function( data ) {
    var actualContent = $( "#messages" ).html();
    var newMsgContent = '<li> <strong>' + data.name + '</strong> : ' + data.message + '</li>';
    var content = newMsgContent + actualContent;

    $( "#messages" ).html( content );
});

我会尽快更新和改进我的代码,但我认为它已经对所有很酷的东西开放了!我真的很乐意就这些东西征求建议和评论,这是做这件事的好方法吗?

I'll try to update and improve my code as soon as possible, but I think it already open to all of cool things! I am really open for advice and reviews on this stuff, is it the good way to do it, .. ?

希望这可以帮助一些人!

Hope this can help some people!

这篇关于如何集成nodeJS + Socket.IO 和PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何集成nodeJS + Socket.IO 和PHP?

基础教程推荐