Pla*_*der 10 php integration node.js socket.io laravel
目前我有一个我通过Laravel框架在PHP上编写的网站.我使用带有Socket.IO和Express的nodeJS编写了一个实时聊天,现在我要做的就是将它集成到我已经写好的Laravel网站中.问题是聊天必须在主页面中,目前由Laravel的视图呈现.目前我在共享主机上.
问题: 对于这种整合,您最好的建议是什么?我知道LAMP堆栈在大多数共享域中都已准备就绪,但我完全不知道如何让PHP(Laravel)和我的nodeJS聊天一起工作.
我尝试过的事情:
解决方案很简单(但在互联网上找不到任何关于它的东西).您只需要在PHP的HTML视图中包含您的socket.io JS文件,然后socket.io JS文件与您的node.JS服务器建立连接.这在localhost上运行良好.但是,如果其他人尝试从外部登录您的聊天,他们将遇到"禁止跨域请求"错误,这是因为您可能跟着像我一样的"指南",而您在CLIENT中的socket.io连接就是这样:
var socket = io.connect('localhost:8080');
Run Code Online (Sandbox Code Playgroud)
代替
var baseURL = getBaseURL(); // Call function to determine it
var socketIOPort = 8080;
var socketIOLocation = baseURL + socketIOPort; // Build Socket.IO location
var socket = io.connect(socketIOLocation);
// Build the user-specific path to the socket.io server, so it works both on 'localhost' and a 'real domain'
function getBaseURL()
{
baseURL = location.protocol + "//" + location.hostname + ":" + location.port;
return baseURL;
}
Run Code Online (Sandbox Code Playgroud)
PHP客户端代码是:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<!-- Wrapper-->
<div id="wrapper">
<!-- Chat: Input -->
<div id="chat-input">
<!-- Username -->
<div class="username">
<p id="username">John Doe</p>
</div>
<!-- Form -->
<form action="">
<!-- Input field -->
<input type="text" class="chat_input-message" id="message" placeholder="Enter your message..." autocomplete="off" autofocus="on" />
<!-- Button -->
<button>Send</button>
</form>
<!-- END: Form -->
</div>
<!-- END Chat: Input -->
<div id="chat-output">
<div id="messages"></div>
</div>
</div>
<!-- END: Wrapper -->
<!-- Scripts -->
<!-- Socket.IO -->
<script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
<!-- jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Chat -->
<script src="../public/js/chat.js"></script>
<!-- End: Scripts -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
服务器端node.JS代码不需要任何调整,忘记关于Redis或PHP的一切(Elephant.IO,AJAX随机注入,忘记任何黑客攻击).它只是作为一种魔力.
| 归档时间: |
|
| 查看次数: |
10912 次 |
| 最近记录: |