2 years ago

#22730

test-img

Brad Cruise

How to access long polling results in wordpress plugin?

I am using Peepso plugin for my Wordpress website. Peepso is using Long polling for the part of the chats in order to create real-time chat. I don't know much on Long polling so I am confused in the way it is written inside the add-on file. What I want is to connect this website with my android application so I want to get those data into the android application in real-time. Here is the code of Peepso messages:

/**
     * Creates a new message thread and adds the first message.
     * @param  PeepSoAjaxResponse $resp
     */
    public function new_message(PeepSoAjaxResponse $resp)
    {
        $subject = '';
        $message = htmlspecialchars($this->_input->raw('message'));

        // SQL safe, forced to int
        $participants = array_map('intval',$this->_input->value('recipients', array(), FALSE));

        $creator_id = get_current_user_id();

        $model = new PeepSoMessagesModel();
        $msg_id = $model->create_new_conversation($creator_id, $message, $subject, $participants);

        if (is_wp_error($msg_id)) {
            $resp->success(FALSE);
            $resp->error($msg_id->get_error_message());
            $resp->set('subject', $subject);
            $resp->set('creator_id', $creator_id);
        } else {
            $ps_messages = PeepSoMessages::get_instance();
            do_action('peepso_messages_new_message', $msg_id);

            $resp->success(TRUE);
            $resp->notice(__('Message sent.', 'msgso'));
            $resp->set('msg_id', $msg_id);
            $resp->set('url', $ps_messages->get_message_url($msg_id));
        }
    }

    /**
     * Add a message to an existing conversation.
     * @param PeepSoAjaxResponse $resp The response object to be sent.
     */
    public function add_message(PeepSoAjaxResponse $resp)
    {
        $message = htmlspecialchars($this->_input->raw('content'));
        $parent_id = $this->_input->int('parent_id');
        $user_id = get_current_user_id();

        // Make sure the "I am typing" flag is deleted
        $mayfly = 'msgso_typing_' . $user_id. '_' . $parent_id;
        PeepSo3_Mayfly::del($mayfly);

        // And that another one can't be set for another full second
        $mayfly_just_posted = "msgso_posted__{$user_id}_{$parent_id}";
        PeepSo3_Mayfly::set($mayfly_just_posted,1,1);

        $participants = new PeepSoMessageParticipants();

        if (FALSE === $participants->in_conversation($user_id, $parent_id)) {
            $resp->success(FALSE);
            $resp->error(__('You are not part of this conversation.', 'msgso'));
        } else {
            $model = new PeepSoMessagesModel();
            $msg_id = $model->add_to_conversation($user_id, $parent_id, $message);

            if (FALSE === $msg_id) {
                $resp->success(FALSE);
                $resp->error(__('Failed to send message.', 'msgso'));
            } else {
                $resp->success(TRUE);
                $resp->notice(__('Message sent.', 'msgso'));
                $resp->set('msg_id', $msg_id);
            }
        }
    }

And here is the js file that controls the long polling:

/**
             * Starts long-polling to get chat stack state.
             */
            startLongPolling: function () {
                if (!SSE_ENABLED && !this.sessionExpired) {
                    this.stopLongPolling();
                    this.fetchChatState().done(
                        $.proxy(function () {
                            this.checkChatState();
                        }, this)
                    );
                }
            },

            /**
             * Stops long-polling to get chat stack state.
             */
            stopLongPolling: function () {
                if (!SSE_ENABLED) {
                    clearTimeout(this.checkChatTimer);
                    this.checkChatXHR && this.checkChatXHR.abort();
                    this.fetchChatXHR && this.fetchChatXHR.ret && this.fetchChatXHR.ret.abort();
                    this.checkChatXHR = false;
                    this.fetchChatXHR = false;
                }
            }

I tried to get those data by adding this line into the code echo json_encode($resp, JSON_PRETTY_PRINT); but nothing happened. So, how can I get the data from this code above? If you need any further information please let me know as I am new on long polling and how to connect it with android application. Any answer will be appreciated.

php

wordpress

long-polling

php

wordpress

long-polling

0 Answers

Your Answer

Accepted video resources