简体   繁体   English

我是否正确使用websockets?

[英]Am I using websockets correctly?

Background: 背景:

  • I am using the API from Pusher. 我正在使用Pusher的API。
  • I am using PHP serverside. 我正在使用PHP服务器端。
  • I have installed the Pusher package server side via composer. 我已经通过composer安装了Pusher软件包服务器端。

How I am using it 我如何使用它

I am building a slide-show that is controlled by a single user. 我正在建立一个由单个用户控制的幻灯片放映。 If I want that one person to change the slide for all the people on that page then I make an AJAX call with the number slide that the user moved to. 如果我希望一个人更改该页面上所有人员的幻灯片,那么我将使用用户移动到的号码幻灯片进行AJAX呼叫。

$('#nextCard').click(function(){
    if ($('#card-' + (cardId + 1)).length != 0) {  // if there is a next card
        $('#card-' + (cardId)).hide();             // hide current card
        cardId++;                                  // increment the card id
        $('#card-' + (cardId)).show();             // and show the next card
        location.hash = cardId;

        /**
         * make ajax call to push function
         */
        $.ajax({
            method: 'post',
            url: '<?php echo base_url('learn/pusher'); ?>',
            data: {card_id: cardId},
            dataType: 'json',
            async: false,
        });
    }
});

This number is sent via ajax to the server where it is channelled via pusher. 该数字通过ajax发送到通过pusher进行引导的服务器。

Pusher then sends the slide number real time to all users on the same page... in effect pushing changes to different peoples screens. 然后,Pusher将幻灯片编号实时发送给同一页面上的所有用户……实际上是更改送到其他人的屏幕上。

var channel = pusher.subscribe('notifications');
channel.bind('new-notification', function(data) {

    // call something here

});

Then the changes are effected by the function that is called when the data is pushed. 然后,更改将受到推送数据时调用的函数的影响。

My question 我的问题

I was under the impression that Websockets was an alternative/replacement to AJAX and other similar technologies...but here I find myself relying on AJAX to send my Websockets data to pusher from the server side. 我给人的印象是Websockets是AJAX和其他类似技术的替代/替代品...但是在这里,我发现自己依靠AJAX将Websockets数据从服务器端发送到推送器。

I thought that Websockets was advantageous over AJAX because it was faster, but here I find myself being bottlenecked by AJAX. 我认为Websockets比AJAX更具优势,因为它速度更快,但在这里我发现自己被AJAX所束缚。

So am I using websockets correctly here? 我在这里正确使用websockets吗?

Using AJAX to send requests to your server which in turn triggers a Pusher event in order to broadcast it to subscribers is an idiomatic way of using Pusher. 使用AJAX将请求发送到您的服务器,该请求又触发一次Pusher事件,以便将其广播给订阅者,这是使用Pusher的一种惯用方式。 The underlying WebSockets technology is bidirectional (more info here ), but Pusher's pub/sub model is unidirectional. 底层的WebSockets技术是双向的(更多信息在这里 ),但推的pub / sub模型是单向的。

One alternative for your use-case is to use client events . 用例的一种替代方法是使用客户端事件 This allows you to trigger events directly from the client. 这使您可以直接从客户端触发事件。 The extra consideration with doing this is you have to use private or presence channels. 这样做的额外注意事项是您必须使用私人或在线状态渠道。

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

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