具有实时&的Angular2 + Laravel Web套接字 [英] Angular2 + Laravel with Real time & WebSockets

查看:65
本文介绍了具有实时&的Angular2 + Laravel Web套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个应用程序,并计划与Angular 2和laravel进行实时战斗.例如,您按下攻击"按钮,对手就会看到他的生命正在实时下降.

I built an app and i'm planning to make a real time battle with Angular 2 and laravel. For example, you hit the "attack" button, and your opponent see his life going down in real time.

我的应用使用以下内容构建:

前端:Angular 2

frontend: Angular 2

后端:PHP Laravel 5.2

Backend: PHP Laravel 5.2

现在我正在搜索和学习我的实时战斗部分, 而且我看到了不同的指南和教程:

Now I'm searching and learning for my real time battle component, and I saw different guides and tutorials for it:

  1. https://www.codetutorial.io/laravel-5 -and-socket-io-tutorial/
  1. https://www.codetutorial.io/laravel-5-and-socket-io-tutorial/
  2. http://4dev.tech/2016/02/creating-a-live-auction-app-with-angular-2-node-js-and-socket-io/

第一教程是关于如何使用 Laravel 5和socket io 的.

The first tutorial is about how to use Laravel 5 and socket io.

第二之一是如何将 Angular 2与NODS JS和socket io一起使用.

我说的是实时,是指两个用户都在屏幕上看到同一件事.

When I say real time, I mean that both users see the same thing that is happening on the screen)

我的后端和前端完全分开了,我的应用程序中的任何地方都没有设置NodeJS.

My Backend and Frontend are totally divided and I have no setup with NodeJS anywhere in my app.

这两个用户都需要在我的应用中查看战斗中发生的动作,并且需要通过我的laravel API并通过我的Angular 2战斗组件进行显示

Both users need to see actions happening during a battle in my app, and It need to go through my laravel API and shown via my Angular 2 battle component

我的问题是-

使用Angular2和Laravel 5.2来获得我想要达到的预期结果的实时应用程序(似乎是websockets)的最佳方法是什么?

推荐答案

在这种情况下,Laravel只是模板和提供客户端文件,并充当客户端和socket.io服务器之间的接口.它实际上不充当socket.io服务器,我不相信它可以.

Laravel in this context is just templating and serving the client files, and acting as an interface inbetween the client and the socket.io server. It doesn't actually act as the socket.io server, and I don't believe it can.

所以,是的,您仍然需要某些(节点)来托管socket.io服务器,以通过PHP或其他方式与客户端进行交互.就个人而言,我将完全跳过Laravel/PHP,而仅将node与koa/express/一起使用,以任何方式对您的客户端(html/js/css/etc)文件进行模板化.感觉对我来说是不必要的抽象.

So yes, you would still need something (node) to host the socket.io server to interact with the client, through PHP or otherwise. Personally, I'd skip Laravel/PHP altogether and just use node with koa/express/whatever to template your client (html/js/css/etc) files. Feels like an unnecessary abstraction to me.

下面来自socket.blade.php的代码已经与实际的socket.io服务器建立了连接,因此我不明白为什么通过PHP/Laravel进行HTTP POST的额外开销是一个好主意.也许可以保证安全性,但是您也可以使用实际的socket.io服务器来处理.

The code below from socket.blade.php already has a connection to the actual socket.io server, so I don't see why the additional overhead of an HTTP POST through PHP/Laravel is a good idea. Security, perhaps, but you can handle that with the actual socket.io server as well.

var socket = io.connect('http://localhost:8890');
socket.on('message', function (data) {
    $( "#messages" ).append( "<p>"+data+"</p>" );
});

这篇关于具有实时&amp;的Angular2 + Laravel Web套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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