Node.js的运行和Apache一起PHP? [英] node.js running alongside Apache PHP?

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

问题描述

我试图让我的头一轮的node.js ...

I am trying to get my head round node.js...

我很高兴我的LAMP设置为它目前符合我的要求。虽然我想添加一些实时功能集成到我的PHP应用程序。如显示的所有用户当前登录到我的网站和可能的聊天功能。

I am very happy with my LAMP set up as it currently fulfils my requirements. Although I want to add some real-time features into my PHP app. Such as showing all users currently logged into my site and possible chat features.

我不想代替我的PHP后台,但我不希望可扩展的实时解决方案。

I don't want to replace my PHP backend, but I do want scalable real-time solutions.

1。我可以抛出的node.js进入组合为我的需求,无需重建整个应用程序的服务器端脚本?

2。如何以最佳可能的node.js为我的聊天和功能当前登录?

大听听大家的意见!

W的

推荐答案

我建议你使用Socket.io沿边node.js中安装和下载通过 http://socket.io/ 的库。你可以沿着侧Apache服务器没有问题,运行它。

I suggest you use Socket.io along side node.js. Install and download the libs from http://socket.io/. You can run it along side your Apache server no problems.

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

First create a node server:

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

二,在命令行中使用运行服务器:

Second, run your server from the command line using:

node /path/to/your/server.js

三,使用客户端JS连接到插座:

Third, connect to the socket using client side js:

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

您必须有包括socket.io LIB客户端以及。

You will have to have include the socket.io lib client side aswell.

使用从客户端将数据发送到节点服务器

Send data from client side to the node server using:

socket.send({data:data});

您server.js也应该具备的功能处理请求:

Your server.js should also have functions for processing requests:

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
  });
});

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

There are two main ways to send data from the server to the client:

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

这篇关于Node.js的运行和Apache一起PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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