事件源并不代表每个客户端/会话的信息

问题描述 投票:0回答:1

我有一个使用SSE发送请求的python服务器端。

这里是python代码的示例。它发送一个“动作状态”和JS必须处理(要做)的数据:

async def sse_updates(request):
    loop = request.app.loop
    async with sse_response(request) as resp:
        while True:

            # Sending request for queue' token remove when it's been removed on server.
            if request.app['sse_requests']['update_queue_vis_remove']:
                await resp.send("update-remove")
                request.app['sse_requests']['update_queue_vis_remove'] = False

            # Sending request for queue' token adding up when it's been added on server.
            if request.app['sse_requests']['update_queue_vis_append'][0]:
                await resp.send(f"update-append {request.app['sse_requests']['update_queue_vis_append'][1]} {request.app['sse_requests']['update_queue_vis_append'][2]}")
                request.app['sse_requests']['update_queue_vis_append'][0] = False

            # Sending request for redundant token's list rewrite (on client side ofc)
            if request.app['sse_requests']['redundant_tokens_vis'][0]:
                await resp.send('update-redtokens ' + ''.join(token + ' ' for token in request.app['sse_requests']['redundant_tokens_vis'][1]))
                request.app['sse_requests']['redundant_tokens_vis'][0] = False

            await asyncio.sleep(0.1, loop=loop)
    return resp

以及处理响应的JS脚本:

evtSource = new EventSource("http://" + window.location.host + "/update")

evtSource.onmessage = function(e) {
    // Data from server is fetching as "<server-event-name> <data1> <data2> <data3> ..."
    let fetched_data = e.data.split(' ');

    // First option is when a token has been removed from server this event has to be represented on a client-side.
    if(fetched_data[0] === "update-remove")
        displayQueueRemove();

    // The second option is when a token appended on server and also it should be represented to a user
    else if(fetched_data[0] === "update-append")

        // fetched_data[1] - token
        // fetched_data[2] - it's (token's) position
        displayQueueAdd(fetched_data[1], parseInt(fetched_data[2]));

    // The last possible options is that if the web-page will has refreshed a data in redundant_tokens should be rewritten
    else if (fetched_data[0] === "update-redtokens"){

        fetched_data.shift();

        // Creating variables for token' wrapping
        let tag;
        let text;

        // Wrapping tokens and store it into the array.
        for(let i = 0; i < fetched_data.length - 1; i++) {
            tag = document.createElement("div");
            text = document.createTextNode(fetched_data[i]);
            tag.appendChild(text);
            tag.setAttribute("class", "token-field");
            redundant_tokens[i] = tag;
        }
    }
}

问题是,如果我打开两个或多个浏览器窗口(会话),则其中只有一个捕获响应并表示响应。此外,在某些情况下,我从一个会话发送请求,但是获得对另一个会话的响应。是否可以使用SSE进行修复(我是说,我正在考虑其他方法,但我想尝试使用SSE)?

javascript python server-sent-events aiohttp
1个回答
0
投票

我认为您的问题是同步数据(app["sse_requests"])。根据您修改数据的方式以及需要通知谁的情况,您可能需要保留一个客户端列表(会话)。

例如,如果需要通知所有客户端所有事件,则保留连接的客户端的list(甚至更好的set),并创建一个周期性函数(使用create_task),在其中通知所有客户端。

如果只需要向客户通知某些事件,则需要使用request对象中的某种键来识别该客户。

© www.soinside.com 2019 - 2024. All rights reserved.