大佬教程收集整理的这篇文章主要介绍了node.js与Apache PHP一起运行?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对我的LAMP设置非常满意,因为它目前满足我的要求。虽然我想添加一些实时功能到我的PHP应用程序。例如显示所有用户当前登录我的网站和可能的聊天功能。
我可以抛出node.js到混合,以满足我的需要,而不重建整个应用程序服务器端脚本?
2. node.js如何最好地服务我的“聊天”和“当前登录”功能?
很高兴听到你的意见!
W.
首先创建一个节点服务器:
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:
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
以上是大佬教程为你收集整理的node.js与Apache PHP一起运行?全部内容,希望文章能够帮你解决node.js与Apache PHP一起运行?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。