我开发了一个应用程序,我计划与Angular 2和laravel进行实时对抗.例如,你按下"攻击"按钮,你的对手就会看到他的生命在实时下降.

My app built with:

frontend:角2

Backend: PHP Laravel 5.2

现在我正在搜索和学习我的实时作战组件,

  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/

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

The second one is how to use Angular 2 with NODS JS and socket io.

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

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

两个用户都需要在我的应用程序中看到战斗中发生的动作,它需要通过我的laravel API,并通过我的Angular 2 battle组件显示

My question is -

What's the best approach to real time app (seem websockets) using Angular2 and Laravel 5.2 to get the desired result of what I'm trying to achieve?

How?

推荐答案

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.

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的额外开销是个好主意.也许是安全性,但你可以用实际的套接字来处理.io服务器也是如此.

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

Laravel相关问答推荐

哈希:check()不返回预期结果Laravel 10

为什么我的 Laravel 开发服务器不提供 public 文件夹下的文件?

Laravel AppServiceProvider 中的代码在通过 DeployHQ 部署时停止 Composer 和构建

LARVAL SAVE 中的关系有

如何提取每组 groupBy 查询的唯一值?

从主类别模型 laravel 中获取子类别翻译

阻止用户查看其他用户正在查看的内容的最佳方法是什么?

Laravel Homestead vagrant up 超时

如何在 Laravel Passport 中获取刷新令牌?

如何通过一个 laravel 安装处理子域

Laravel,没有shell 访问的转储自动加载

Laravel lockforupdate(悲观锁)

Laravel 5.4 有时验证规则不起作用

Laravel 迁移命名约定

如何在 Laravel 中正确安装软件包?

如何卸载 Laravel Passport

Laravel 分页漂亮的 URL

Eloquent - 带有字符串值而不是列标题的连接子句

Laravel binding绑定的用途和目的是什么?

没有Access-Control-Allow-Origin标头 - Laravel