Node.js + codeigniter [英] Node.js + codeigniter

查看:209
本文介绍了Node.js + codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近阅读了Node.js,它是一个伟大的网络服务器,甚至支持socket。我正在考虑在我的一个项目上使用它,但我仍然没有能够弄清楚从CI到node.js的交互。有一个项目为它与Drupal做了,它似乎工作很好,但我仍然不能弄清楚如何将它们集成在一起。

Recently been reading up on Node.js and how it is a great webserver and supports sockets even. I was thinking of using it on a project of mine, but i still haven't been able to figure out to to interact from CI to node.js. There was a project done for it with Drupal and it seems to be working well however i still can't figure out how they integrated it together.

只是想知道有没有人任何经验。

Just wondering if anyone has any experience with the idea.

http://drupal.org/project/nodejs

http://www.youtube.com/watch?v=UV8lbdJfESg

示例:

用户通过AJAX发布评论
评论获得存储在DB
所有看到该线程的用户都会收到通知

User posts a comment via AJAX Comment gets store in DB All users watching the thread gets notification

现在的部分,在它提交注释和通知后,如何将msg发送到node.js

now th part where after its submitted the comment and the notification, how does the msg get sent to node.js

推荐答案

节点.js是一个可以用作Web服务器的非阻塞IO库。

node.js is a non-blocking IO library capable of being used as a Web Server.

代码Igniter是一个PHP框架。

Code Igniter is a PHP framework.

您要在PHP Web服务器旁边运行node.js Web服务器,并让他们彼此通信吗?

Do you want to run a node.js Web Server beside your PHP Web Server and have them talk to each other?

我建议你做一个或另一个。请在快速现在重新整理整个网站。

I'd recommend you do one or the other. Re write your entire website in express and now.

如果他们必须彼此交谈,您可以使用 net 在节点中轻松打开TCP套接字。

If they must talk to each other you can easily open a TCP socket in node by using net.

var net = require('net');

var server = net.createServer(function (socket) {
  socket.write("Echo server\r\n");
  socket.pipe(socket);
})

server.listen(8124, "127.0.0.1");

然后只需使用 fsockopen 在PHP中通过TCP套接字连接到节点。

Then just use fsockopen in PHP to connect to node over a TCP socket.

修改

实时评论完全独立于CI。你只需要在你的CI服务器页面上有一些socket.io javascript。您的页面通过单独的套接字与node.js通信,不会碰到PHP后端。您的socket.io会将数据推送到所有客户端,页面将使用javascript呈现新邮件。

The live comments is completely independant of CI. You just need to have some socket.io javascript on you CI server pages. Your pages talk to node.js over a seperate socket and never touch the PHP back end. Your socket.io will push data to all your clients and the pages will render new messages with javascript.

所有代码指示器需要做的是插入

All codeigniter needs to do is insert

<script src="url/socket-io.js" />
<script src="url/myChat.js" />

进一步编辑

所以你需要你的用户登录你的websocket。我不知道他们如何登录,但发送相同的用户名/密码哈希到node.js不应该太难。获取node.js以打开与存储用户的数据库的连接。然后存储特定用户在数据库中被预订到哪些频道/线程/聊天室/消息中。

So you need your user to log in over your websocket. I'm not sure how they log in now but sending the same username/password hash to node.js shouldn't be too hard. Get node.js to open a connection to your database where you store users. Then store which channels / threads / chat rooms / messages a particular user is "subscriped" to in a database.

然后当节点从频道接收到消息时

Then when node receives a message from a "channel" it just asks the database which users to push that message to, and then it pushes it.

我回答了类似的问题 .com>现在有一个很好的例子。

I answered a similar question about writing a chat server using node and the video tutorial of now has a good example. You should be able to turn "multiple rooms chatting" into "multiple thread commenting" pretty easily.

进一步编辑

当您单击添加时,不要发布到URL comment / add / 。不要使用ajax。

Don't post to the URL comment/add/ when you click add. Don't use ajax. Instead use socket.io.

像这样:

// on the client side
$("#add").click(function() {
    socket.send("add" + user.toJSON());
});

socket.on("message", function(m) {
    if (/^new/.test(m)) {
         var post = m.substring(3);
         $("#comments").append($("<div></div>").text(post));
    }
});

// on the server side
var socket = io.listen(server); 
socket.on('connection', function(client){ 
    // new client is here! 
    client.on('message', function(m){ 
        if (/^add/.test(m)) {
             client.broadcast("new"+m.substring(3));
        }
    }); 
}); 

因此,当您单击添加时,客户端会发送添加评论服务器侦听添加消息并将消息广播到所有其他客户端。这些客户端已在侦听新邮件,新邮件附加了评论。

So simply the client sends a "add comment" message when you click add. The server listens for the add message and broadcasts the message to all other clients. These clients are already listening for the new message, and new appends a comment.

这篇关于Node.js + codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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