31

我正试图让我的头脑围绕 node.js ......

我对我的 LAMP 设置感到非常满意,因为它目前满足了我的要求。虽然我想在我的 PHP 应用程序中添加一些实时功能。例如显示所有当前登录到我的网站的用户和可能的聊天功能。

我不想替换我的 PHP 后端,但我确实想要可扩展的实时解决方案。

1. 我可以在不重建整个应用程序服务器端脚本的情况下将 node.js 加入到组合中来满足我的需求吗?

2. node.js 如何最好地服务于我的“聊天”和“当前登录”功能?

很高兴听到你的意见!

W。

4

5 回答 5

23

我建议你在 node.js 旁边使用 Socket.io。从http://socket.io/安装和下载库。您可以在 Apache 服务器旁边运行它,没有问题。

首先创建一个节点服务器:

var http = require('http')
  , url = require('url')
  , fs = require('fs')
  , io = require('../')//path to your socket.io lib
  , sys = require(process.binding('natives').util ? 'util' : 'sys')
  , server;

server = http.createServer(function(req, res){
  var path = url.parse(req.url).pathname;
}),

server.listen(8084);//This could be almost any port number

其次,使用以下命令从命令行运行您的服务器:

node /path/to/your/server.js

三、使用客户端js连接socket:

var socket = new io.Socket(null, {port: 8084, rememberTransport: false});
socket.connect();

您还必须包含 socket.io lib 客户端。

使用以下命令将数据从客户端发送到节点服务器:

socket.send({data:data});

您的 server.js 还应该具有处理请求的功能:

io.on('connection', function(client){
//action when client connets

 client.on('message', function(message){
    //action when client sends msg
  });

  client.on('disconnect', function(){
    //action when client disconnects
  });
});

有两种主要方式将数据从服务器发送到客户端:

client.send({ data: data});//sends it back to the client making the request

client.broadcast({  data: data});//sends it too every client connected to the server
于 2011-02-16T11:07:17.663 回答
2

我怀疑聊天以及登录列表将通过 Ajax 工作。

聊天部分很容易在 Node.js 中编程,使用 Node 的mysql 模块之一连接到您现有的数据库并查询登录信息等,然后通过 Node.js 进行所有实际聊天,我建议您查看Socket.io,因为它使 Browser/Node.js 通信变得非常简单,这应该可以让您专注于实际的聊天逻辑。

此外,您可以查看 Node.js 的“官方”聊天演示,以获得一些灵感。

就当前在线部分而言,这绝非易事,因为您所能做的就是显示“最近 X 分钟内有 5 个活跃用户”这样的内容。

当然,您可以轻松添加一些 Ajax 来查询聊天服务器并在主页上显示用户列表。

或者你完全疯了,为每个访问者建立一个 Socket.io 连接并以这种方式监控它,尽管这是否值得努力值得怀疑。

于 2011-01-09T21:26:25.047 回答
1

像 pedro 对 ngnx 那样使用套接字文件怎么样? http://nodetuts.com/tutorials/25-nginx-and-nodejs.html

于 2011-08-15T17:13:16.950 回答
0

您可以使用 node-php 从节点 js 运行 php:https ://github.com/mkschreder/siteboot_php

于 2014-01-26T21:55:10.400 回答
0

我在我的 LAMP 设置旁边运行一个 wss(安全 websocket)服务器。

Node.js 可以轻松地与您想要的任何其他 Web 服务器 (apache) 一起运行。在@KitCarrau 示例中,他让节点在端口 8084 上运行——这是它运行和监听的地方,而不是 80 或 443 等(无论如何,这些通常由 apache 使用)。但是您仍然可以使用相同的端口来服务 http/https(在我的情况下,只是说明服务已启动的一些 conf 和一般信息)。

从控制台开始不是最好的方法(远程,关闭控制台时节点停止)。我建议看一下Running node as service

易于实时跟踪日志(使用 console.log("hello"); 在您的应用程序中记录):

tail -f /var/.../SocketServer.log

示例脚本(node-server.conf):

author ....    
description "node.js server"    
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

# Max open files are @ 1024 by default. Bit few.
limit nofile 32768 32768

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/root"

    exec node /var/.../SocketServer.js >> /var/www/node/.../SocketServer.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
   echo "\n*********\nServer started\n$(date)\n*********" >> /var/.../SocketServer.log

end script
于 2014-11-11T14:36:31.103 回答