首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对JS错误作出反应:“TypeError: socket.emitWithPromise不是一个函数”

对JS错误作出反应:“TypeError: socket.emitWithPromise不是一个函数”
EN

Stack Overflow用户
提问于 2020-07-28 14:22:21
回答 1查看 193关注 0票数 0

我得到了一个未知的承诺错误,它说这个函数不是一个函数,我不明白为什么它会给我这个错误。

下面是我制作的“emitWithPromise”函数:

代码语言:javascript
复制
import React from 'react';
import io from 'socket.io-client';
import { SocketContext } from './SocketContext';
import SocketEvents from 'constants/socket-events';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { actionCreators as callsActionCreators } from 'state/calls/actions';
import { isAuthenticated, getAccessToken } from 'infrastructure/auth';
import { incomingCallSound, stopIncomingCallSound, dropSound } from 'components_amwell/CallSounds';
import { APP_CONFIG } from 'constants/global-variables';

class Socket extends React.Component {
    constructor(props) {
        super(props);
        
        if (!isAuthenticated()) {
            return;
        }

        //const signalingUrl =
        //process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test' ? process.env.REACT_APP_SIGNALING_URL : window.__env__.REACT_APP_SIGNALING_URL;
        const signalingUrl = APP_CONFIG.URL.signalingPath;

        this._socket = io.connect(signalingUrl, {
            secure: true,
            transports: ['websocket'],
        });

        let userProfile = null;
        if (localStorage.getItem('userProfile')) {
            userProfile = JSON.parse(localStorage.getItem('userProfile'));
        }

        const myClientInfo = {
            token: getAccessToken(),
            incomingCallsDisabled: userProfile != null ? userProfile.incomingCallsDisabled : false,
            clearConferences: true,
        };

        this._socket.on('connect', _data => {
            this._socket.emit('client.authorize', myClientInfo);
        });

        this._socket.on('client.authenticated', _data => {});

        this._socket.on('client.unauthorized', function(error) {});

        this._socket.on('disconnect', _data => {});

        this._socket.on(SocketEvents.Conference.ON_INCOMING, conferenceInfo => {
            incomingCallSound();
            this.props.calls.setConferenceInfo(Object.assign(conferenceInfo, { isReceivingCall: true }));
        });

        this._socket.on(SocketEvents.Conference.ON_INITIATOR_LEFT, () => {
            this.props.calls.setConferenceInfo(null);
            stopIncomingCallSound();
            dropSound();
        });

        this._socket.on('error', function(err) {
            console.error('Socket.IO error:' + err);
        });

        this.emitWithPromise = this.emitWithPromise.bind(this);
    }

    emitWithPromise = async (event, data) => {
        return new Promise((resolve, reject) => {
            this._socket.emit(event, data, resolve);
        });
    };

    render() {
        return <SocketContext.Provider value={this._socket}>{this.props.children}</SocketContext.Provider>;
    }
}

const mapStateToProps = state => {
    return {
        calls: state.calls,
    };
};

const mapDispatchToProps = dispatch => {
    return {
        calls: bindActionCreators(callsActionCreators, dispatch),
    };
};

export default connect(
    mapStateToProps,
    mapDispatchToProps,
)(Socket);

当我在其他地方使用这个函数时,它会显示错误:

Uncaught : socket.emitWithPromise不是一个函数

我在另一个文件中使用它,像这样:

等待socket.emitWithPromise(SocketEvents.Conference.START,

conferenceInfo);

此外,在代码中返回:

代码语言:javascript
复制
<SocketContext.Provider

返回到只有这么多代码的代码时,我认为最好只是在情况下共享:

代码语言:javascript
复制
import React from 'react';

export const SocketContext = React.createContext({});
EN

回答 1

Stack Overflow用户

发布于 2020-07-28 14:59:02

我想你应该回到这里

代码语言:javascript
复制
emitWithPromise = async (event, data) => {
        return new Promise((resolve, reject) => {
            return this._socket.emit(event, data, resolve);
        });
    };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63136338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档