ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

我如何在SharedWorkers中使用Pusher Laravel Echo?

2019-12-11 00:28:32  阅读:323  来源: 互联网

标签:laravel websocket pusher php echo


我试图在每个使用Laravel Echo使用Pusher的应用程序中打开多个选项卡的用户中,保持一个连接处于活动状态,通过遵循以下文章和示例项目,我能够使其在测试项目上正常工作.

https://blog.pusher.com/reduce-websocket-connections-with-shared-workers/
https://github.com/pusher-community/pusher-with-shared-workers

但是我很难适应它来适应我的Laravel项目,我应该怎么做?

这是我添加到bootstrap.js文件中的配置

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

$.getJSON( "/heartbeat", function( json ) {

    window.Echo.channel('user.' + json.user_id).listen('NotifyUser', e => {
        displayModal(e.msg);
        console.log('User with an id of ' + e.id + ' has been notified.');
        console.log(e);
        // Relay the message on to each client
        clients.forEach(function(client){
          client.postMessage(data);
        });
    });

});

self.addEventListener("connect", function(evt){

  // Add the port to the list of connected clients
  var port = evt.ports[0];
  clients.push(port);

  // Start the worker.
  port.start();
});

它正在工作,但不是我想要的方式,它正在为每个打开的选项卡创建一个新连接.

解决方法:

如您自己提供的链接中所指定的:https://blog.pusher.com/reduce-websocket-connections-with-shared-workers/,您需要导入特定版本的pusher才能使用worker:pusher.worker.js.

所以,去这里:https://github.com/pusher/pusher-js/tree/master/dist/worker

并下载pusher.worker.min.js.将其放在与boostrap.js相同的目录中,并需要它.代替:

window.Pusher = require('pusher-js');

采用

importScripts("pusher.worker.min.js");

标签:laravel,websocket,pusher,php,echo
来源: https://codeday.me/bug/20191210/2105233.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有