Websocket onMessage是必需的,但值未定义

时间:2020-02-06 16:44:16

标签: javascript reactjs websocket

我想知道为什么会这样吗?

失败的道具类型:道具onMessageWebsocket中标记为必需,但其值为undefined。 在Websocket中(由LobbyBase创建)

这是我的大厅基地

import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Websocket from 'react-websocket'
import $ from 'jquery'

class LobbyBase extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            player_game_list: [],
            available_game_list: []
        }

        // bind button click
        //this.sendSocketMessage = this.sendSocketMessage.bind(this);
    }

    componentDidMount() {

    }

    componentWillUnmount() {
        this.serverRequest.abort();
    }

    handleData(data) {
        //receives messages from the connected websocket
        let result = JSON.parse(data)


        // we've received an updated list of available games
        this.setState({available_game_list: result})
    }

    sendSocketMessage(message){
        // sends message to channels back-end
       const socket = this.refs.socket
       socket.state.ws.send(JSON.stringify(message))
    }

    render() {
        return (

            <div className="row">
                <Websocket url={this.props.socket}
                    onMessage={this.handleData.bind(this)}
                    />
                <span>Lobby Components will go here....</span>
            </div>

        )
    }
}

LobbyBase.propTypes = {
    socket: PropTypes.string
};

export default LobbyBase;

这是我的网址(在索引中)

var lobby_sock = 'ws://' + window.location.host + "/ws/lobby/"

我已经看到人们为声明websocket做类似的代码。任何帮助将不胜感激!

编辑:有关句柄数据的更多信息

 handleData(data) {
        //receives messages from the connected websocket
        let result = JSON.parse(data)


        // we've received an updated list of available games
        this.setState({available_game_list: result})
    }

0 个答案:

没有答案