用于 Socket.IO 实时聊天的 nodeJS 和 PHP (Laravel) 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25366232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:57:57  来源:igfitidea点击:

nodeJS and PHP (Laravel) integration for Socket.IO live chat

phpnode.jslaravelsocket.iointegration

提问by Placeholder

Currently I have a website which I wrote on PHPvia the Laravel framework. I have wrote a live chat using nodeJSwith Socket.IOand Expressand now what I want to do is to integrate it inside my already written Laravel website. The problem is the chat must be in the main page, which is currently rendered by the views of Laravel. Currently I am on a shared hosting.

目前我有一个网站,我通过Laravel 框架PHP编写。我已经使用带有Socket.IOExpress 的nodeJS编写了一个实时聊天,现在我想做的是将它集成到我已经编写好的 Laravel 网站中。问题是聊天必须在主页面中,当前由 Laravel 的视图呈现。目前我在共享主机上。

The question:What are your best suggestions for such integration? I know that the LAMP stack comes ready in most shared domains but I have completely no idea how I am to get PHP(Laravel) and my nodeJS chat to work together.

问题:您对这种整合的最佳建议是什么?我知道 LAMP 堆栈在大多数共享域中都已准备就绪,但我完全不知道如何让 PHP(Laravel) 和我的 nodeJS 聊天协同工作。

Things I have tried:

我尝试过的事情:

  • Elephant.IO - Didn't have any big success with it yet...
  • Elephant.IO - 还没有取得任何大的成功......

采纳答案by Placeholder

the solution is simple (but finding ANYTHING about it on the internet was not). You just need to include your socket.io JS file in the HTML view of PHP, then the socket.io JS files makes a connection to your node.JS server. This works all fine on localhost. However, if someone else tries to log into your chat from outside, they will experience a "Forbidden crossdomain request" error, which is because you have probably followed some "guide" like me and your socket.io connection in the CLIENT is like that:

解决方案很简单(但在互联网上找不到任何相关信息)。您只需要在 PHP 的 HTML 视图中包含您的 socket.io JS 文件,然后 socket.io JS 文件就会连接到您的 node.JS 服务器。这在本地主机上一切正常。但是,如果其他人尝试从外部登录您的聊天,他们将遇到“禁止跨域请求”错误,这是因为您可能已经遵循了一些像我这样的“指南”,并且您在客户端中的 socket.io 连接是这样的:

var socket = io.connect('localhost:8080');

instead of

代替

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

The PHP client code is:

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>

The server-side node.JS code does not need any tweaks, forget everything about Redis or in PHP (Elephant.IO, AJAX random injects, forget about any hacks). It simply works as a magic.

服务器端 node.JS 代码不需要任何调整,忘记关于 Redis 或 PHP 的所有内容(Elephant.IO,AJAX 随机注入,忘记任何黑客)。它只是一种魔法。