同时最佳实践PHP和NodeJS

时间:2019-06-12 01:35:13

标签: php node.js ssl https socket.io

当前在开发人员中,我同时通过cli在nodejs上运行WAMP。 基本上,您访问网站https://localhost:443,然后加载index.php,该文件会在head标签中初始化socketio脚本。它连接到cli nodejs,然后通过apache / php解析index.php时进行双向通信。

同时运行PHP和NodeJS的最佳实践是什么?伸缩会有问题吗?

出于安全性考虑,在apache / php和cli / nodejs中同时使用https是否过大?

View Image

WAMP / Php网站通过端口80/443 通过端口5000进行CLI / NodeJS实时通信

index.php

<!doctype html>
<html>
<head>
	<script src='https://localhost:5000/socket.io/socket.io.js'></script>
	<script>
		var socket = io('https://localhost:5000');

		socket.on('welcome', function(data) {
			console.log('Chrome Console!');
			socket.emit('2Server', {msg:'Thanks Server!'});
		});
					
					
		socket.on('2Client', function(data) {
			console.log(data.msg);
		});
	</script>
</head>
    <body>
        <?php echo 'Welcome!' ?>
    </body>
</html>

app.js

var fs = require('fs');
var socket = require('socket.io')
var https = require("https");
var options = {
  key: fs.readFileSync("certs/server.key"),
  cert: fs.readFileSync("certs/server.cert")
};

var app = https.createServer(options, function(req, res){
  res.end();
});

var io = socket(app);

setInterval(function(){
    io.emit('2Client', { msg:'Hi Client'});
}, 10000);

// Emit welcome message on connection
io.on('connection', function(socket) {
    socket.emit('welcome', { message: 'Welcome!', id: socket.id });
    socket.on('2Server', function(data){
		console.log(data.msg);
	});
});

app.listen(5000, function(){
	console.log('Listening on port 5000');
});

0 个答案:

没有答案