Socket.io不从获取中发射对象

时间:2018-12-24 12:41:50

标签: javascript typescript sockets socket.io fetch

在标记为重复项之前,请仔细阅读。我已阅读并了解范围,下面的问题不是关于为什么该方法未返回正确响应的问题。这就是为什么当我使用套接字时,获取套接字会发出一个空对象的原因,或者更好的说我如何才能通过套接字发出要发送的对象。

我很难弄清楚为什么在套接字客户端中调用下面的方法然后发出时,套接字的另一端会得到一个'{}'空对象而不是结果。 当我在没有套接字的情况下运行方法部分时,会得到正确的响应。

使用socket.io

export async function performSearch(options: ISearchEntityRequest, callingMethod?: string): Promise<IActionResponse<ISearchEntityResponse>> {

const baseSearchRequest = Object.assign({}, BASE_SEARCH_REQUEST, options);
let response = <IActionResponse<ISearchEntityResponse>>{};

    const io = require('socket.io-client');
    const socket = io.connect();

    socket.on('connect', function (data) {
        socket.on('fetchShareClasses', async function (searchResponse) {

            searchResponse = await fetch(ENDPOINTS.fund.searchEntity, generateRequest('POST', baseSearchRequest));
            socket.emit('fetchShareClasses', JSON.stringify(searchResponse));

            response = searchResponse;

            return response;
        });
    });

    return Promise.resolve(response);
}

const BASE_SEARCH_REQUEST: ISearchEntityRequest = {
type: 'FUND',
search: [],
    include: {},
    culture: 'en-GB',
    applyFormats: false,
    start: 0,
    limit: 100,
};

并且没有套接字:

export async function performSearch(options: ISearchEntityRequest, callingMethod?: string): Promise<IActionResponse<ISearchEntityResponse>> {

const baseSearchRequest = Object.assign({}, BASE_SEARCH_REQUEST, options);
let response = <IActionResponse<ISearchEntityResponse>>{};

    const io = require('socket.io-client');
    const socket = io.connect();

    const request = await fetch(ENDPOINTS.fund.searchEntity, generateRequest('POST', baseSearchRequest));
    if (request.ok) {
        response.data = Object.assign({}, await request.json());
    }else {
        response.error = new Error(request.statusText);
    }
    return response;
}

谢谢

0 个答案:

没有答案