1

我已经使用 js 和 ajax 集成了 zulip api,问题是交换的消息不在真实的 tile 中所以请,如何使用 js 和 ajax 来收听实时事件 zulip,这是我的 js 代码

$("#post").click(function () {

var url = 'https://domain_name/api/v1/messages';
var content = $("#postButton").val();
$.ajax({
    type: "post",
    url: url,
    data: {type: 'private', content: content, to: 'khalil@gmail.com'},
    headers: {Authorization: getBasicAuthenticationToken(username, 
    password)},
   

   })






  });


$(function () {
var $msgs = $('#msgs');


 $.ajax({
    type: 'GET',
    url: 'https://domain_name/api/v1/messages',
    data: {num_before: 10, num_after: 1000, use_first_unread_anchor: true},
    headers: {
        "Authorization": getBasicAuthenticationToken(username, password)
    },
    success: function (msgs) {
   
        /*console.log(msgs['messages'][0]['sender_email']);*/
        $.each(msgs.messages, function (i, msg) {
            $msgs.append('<li>' + msg.content + '</li>');
            $msgs.append('<li>' + msg.sender_email + '</li>');
            var who = msg.sender_email;

            insertChat('<p>' + who + '</p>', '<p>' + msg.content +'</p>',0)
        });
    }
});
});
4

1 回答 1

1

您需要的 API 是 Zulip 实时事件 API。首先,使用 注册一个事件队列GET /api/v1/register,指定event_types=["message"]。然后,您可以通过重复 long-polling 从队列中获取事件GET /api/v1/events

于 2021-06-10T16:24:42.653 回答