如何集成nodeJS + Socket.IO和PHP? [英] How to integrate nodeJS + Socket.IO and PHP?

查看:84
本文介绍了如何集成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上:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆