用于Socket.IO实时聊天的nodeJS和PHP(Laravel)集成 [英] nodeJS and PHP (Laravel) integration for Socket.IO live chat

查看:184
本文介绍了用于Socket.IO实时聊天的nodeJS和PHP(Laravel)集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I have a website which I wrote on PHP via the Laravel framework. 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. 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.

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

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.

我尝试过的事情:

  • Elephant.IO-尚未获得任何成功...

推荐答案

该解决方案很简单(但要在互联网上找到关于它的任何内容都不是).您只需要在PHP的HTML视图中包含socket.io JS文件,然后socket.io JS文件即可连接到node.JS服务器.这在localhost上一切正常.但是,如果其他人尝试从外部登录到您的聊天,他们将遇到禁止跨域请求"错误,这是因为您可能已经遵循了像我这样的指南",并且CLIENT中的socket.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. 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:

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

代替

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

PHP客户端代码为:

The PHP client code is:

<!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>

服务器端的node.JS代码不需要任何调整,无需再处理Redis或PHP中的所有内容(Elephant.IO,AJAX随机注入,无需任何黑客操作).它只是像魔术一样起作用.

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.

这篇关于用于Socket.IO实时聊天的nodeJS和PHP(Laravel)集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆