简体   繁体   English

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

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

Currently I have a website which I wrote on PHP via the Laravel framework . 目前我有一个我通过Laravel框架PHP编写的网站。 I have wrote a live chat using nodeJS with Socket.IO and Express and now what I want to do is to integrate it inside my already written Laravel website. 我使用带有Socket.IOExpress的 nodeJS编写了一个实时聊天,现在我要做的就是将它集成到我已经写好的Laravel网站中。 The problem is the chat must be in the main page, which is currently rendered by the views of Laravel. 问题是聊天必须在主页面中,目前由Laravel的视图呈现。 Currently I am on a shared hosting. 目前我在共享主机上。

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 - 还没有取得任何重大成功......

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. 您只需要在PHP的HTML视图中包含socket.io JS文件,然后socket.io JS文件与您的node.JS服务器建立连接。 This works all fine on localhost. 这在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: 但是,如果其他人尝试从外部登录您的聊天,他们将遇到“禁止跨域请求”错误,这是因为您可能跟着像我一样的“指南”,而您在CLIENT中的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). 服务器端node.JS代码不需要任何调整,忘记关于Redis或PHP的一切(Elephant.IO,AJAX随机注入,忘记任何黑客攻击)。 It simply works as a magic. 它只是作为一种魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM