Node.js + 代码点火器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5654421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js + codeigniter
提问by DregondRahl
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.
最近在阅读 Node.js 以及它是如何成为一个出色的网络服务器并且甚至支持套接字的。我想在我的一个项目中使用它,但我仍然无法弄清楚从 CI 到 node.js 的交互。有一个用 Drupal 为它完成的项目,它似乎运行良好,但我仍然无法弄清楚他们是如何将它集成在一起的。
Just wondering if anyone has any experience with the idea.
只是想知道是否有人对这个想法有任何经验。
http://drupal.org/project/nodejs
http://www.youtube.com/watch?v=UV8lbdJfESg
Example:
例子:
User posts a comment via AJAX Comment gets store in DB All users watching the thread gets notification
用户通过 AJAX 发表评论评论被存储在数据库中 所有观看线程的用户都会收到通知
now th part where after its submitted the comment and the notification, how does the msg get sent to node.js
现在是提交评论和通知后的部分,味精如何发送到 node.js
回答by Raynos
node.js is a non-blocking IO library capable of being used as a Web Server.
node.js 是一个非阻塞 IO 库,能够用作 Web 服务器。
Code Igniter is a PHP framework.
Code Igniter 是一个 PHP 框架。
Do you want to run a node.js Web Server beside your PHP Web Server and have them talk to each other?
您想在 PHP Web Server 旁边运行一个 node.js Web Server 并让它们相互通信吗?
I'd recommend you do one or the other. Re write your entire website in expressand now.
我建议你做一个或另一个。用express和now重新编写您的整个网站。
If they must talk to each other you can easily open a TCP socket in node by using net.
如果它们必须相互通信,您可以使用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");
Then just use fsockopenin PHP to connect to node over a TCP socket.
然后只需fsockopen在 PHP 中使用通过 TCP 套接字连接到节点。
Edit:
编辑:
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.
实时评论完全独立于 CI。你只需要在你的 CI 服务器页面上有一些 socket.io javascript。您的页面通过单独的套接字与 node.js 通信,并且永远不会接触 PHP 后端。您的 socket.io 会将数据推送到您的所有客户端,并且页面将使用 javascript 呈现新消息。
All codeigniter needs to do is insert
codeigniter 需要做的就是插入
<script src="url/socket-io.js" />
<script src="url/myChat.js" />
Further Edit:
进一步编辑:
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.
所以你需要你的用户通过你的 websocket 登录。我不确定他们现在如何登录,但向 node.js 发送相同的用户名/密码哈希应该不会太难。获取 node.js 以打开与存储用户的数据库的连接。然后将特定用户“订阅”的频道/话题/聊天室/消息存储在数据库中。
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.
然后,当节点从“通道”接收消息时,它只询问数据库用户将该消息推送到哪些用户,然后将其推送。
I answered a similar questionabout writing a chat server using node and the video tutorial of nowhas a good example. You should be able to turn "multiple rooms chatting" into "multiple thread commenting" pretty easily.
我回答了一个关于使用 node 编写聊天服务器的类似问题,现在的视频教程有一个很好的例子。您应该能够非常轻松地将“多房间聊天”变成“多线程评论”。
Further Further Edit
进一步编辑
Don't post to the URL comment/add/when you click add. Don't use ajax. Instead use socket.io.
comment/add/单击添加时不要发布到 URL 。不要使用ajax。而是使用 socket.io。
So something like:
所以像:
// 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 otherclients. These clients are already listening for the new message, and new appends a comment.
因此,当您单击添加时,客户端只需发送一条“添加评论”消息即可。服务器侦听添加消息并将消息广播给所有其他客户端。这些客户端已经在侦听新消息,并且 new 会附加一条评论。
回答by ShadowMorph
The way Drupal has done it, is use the Node.js plugin Socket.io. When a comment gets posted, drupal notifies the socket with the comment details, node.js then notifies the other "clients".
Drupal 的做法是使用 Node.js 插件 Socket.io。当评论被发布时,drupal 用评论细节通知套接字,然后 node.js 通知其他“客户端”。
回答by Rick Sanchez
I have just found this package:
我刚刚找到了这个包:
NodeigniterMVC - an MVC framework for node.js inspired by Codeigniter. It allows custom routing, chaining, and partial view rendering; built-in with helpers, libraries, and a CLI. Fully compatible with Bower.
NodeigniterMVC - 受 Codeigniter 启发的 node.js MVC 框架。它允许自定义路由、链接和局部视图渲染;内置帮助程序、库和 CLI。与 Bower 完全兼容。
https://www.npmjs.com/package/nodeignitermvc
https://www.npmjs.com/package/nodeignitermvc
So from what I understad, it's a package that runs unde NODE.JS if you haven't started you're project and want them both without getting yourself into too much trouble playing with sockets on your server - this looks ideal.
因此,据我所知,它是一个在 NODE.JS 下运行的包,如果你还没有开始你的项目,并且想要它们同时又不会让你在服务器上使用套接字遇到太多麻烦 - 这看起来很理想。
maybe this helps :)
也许这有帮助:)
回答by JohnWright
I think in a general case of using node.js with CodeIgniter, there would be no direct communication between the two.
我认为在将 node.js 与 CodeIgniter 一起使用的一般情况下,两者之间不会有直接通信。
In the example you described above this could be accomplished using a technique call "long polling" with node.js . (http://blog.nemikor.com/2010/05/21/long-polling-in-nodejs/)
在您上面描述的示例中,这可以通过使用 node.js 的“长轮询”技术调用来完成。(http://blog.nemikor.com/2010/05/21/long-polling-in-nodejs/)
Essentially, your client side AJAX would make a request to your node.js server. This request on the node.js server would start a process that checks your DB for new comments every second (or 5 seconds, etc). When it finds a new comment, it would return it as the response to the client side JS AJAX call, and your JS would handle it from there.
本质上,您的客户端 AJAX 会向您的 node.js 服务器发出请求。node.js 服务器上的这个请求将启动一个进程,每秒(或 5 秒等)检查您的数据库是否有新评论。当它找到一条新评论时,它会将它作为对客户端 JS AJAX 调用的响应返回,您的 JS 将从那里处理它。
This is one approach in which node.js and CodeIgniter could be combined.
这是一种可以结合使用 node.js 和 CodeIgniter 的方法。
NOTE: Node.js is very good for long polling because you can maintain many simultaneousness requests from one node.js server, due to the non-blocking, event loop based design of node.js
注意:Node.js 非常适合长轮询,因为 node.js 的非阻塞、基于事件循环的设计,你可以维护来自一个 node.js 服务器的许多并发请求

