简体   繁体   English

Socket.io并非始终有效

[英]Socket.io not always working

I'm working with a few modules and everything is inplace EXCEPT for the fact that socket.io isn't always working... 我正在使用一些模块,并且除了socket.io并不总是可以正常工作外,其他所有内容都在原地...

We've got a few 'routes' we're already using in Socket.io, which all work great and work everytime! 我们已经在Socket.io中使用了一些“路线”,它们都很好用,并且每次都能工作!

But now, we're introducing a new one 但是现在,我们正在引入一个新的
'order/new', which should display every new order incomming. “订单/新订单”,应显示即将收到的每个新订单。

A new order get's created, a message is sent over the queue, we retreive it through a GET request and we sent it to the user with Socket.io. 创建了一个新的订单get,通过队列发送了一条消息,我们通过GET请求检索该消息,然后使用Socket.io将其发送给用户。

But the emit function doesn't always 'emit'(?!)... Can anyone help me with this? 但是send函数并不总是'emit'(?!)...有人可以帮我吗?

Server: 服务器:

io.on('connection', function (socket) {
    // somewhere else calls this function, nothing wrong here...
    function (mes) {
        console.log('New order received: ', mes);
        var doc_id = JSON.parse(mes)._id;
        request
            .get(urls['order/get'].replace(':id', doc_id))
            .end(function (err, res) {
                console.log(err);
                if (res.ok) {
                    console.log(chalk.green('OK: [GET] - ORDER/GET - ORDER_ID: ' + doc_id));
                    // The above console.log get's called, so still working...
                    socket.emit('order/new', res.body);
                    // THIS socket.emit is called randomly, it doesn't work everytime...
                } else {
                    console.log(chalk.red('ERROR: [GET] - ORDER/GET - ' + res.text))
                }
            });
    }

Front (React): 正面(反应):

var OrdersViewer = React.createClass({

    setNewOrder (order) {
        console.log("new order gotten nr.", this.state.orders.length);
        // this doesn't always get called...
        var orders = _.cloneDeep(this.state.orders);
        orders.push(order);
        this.setState({orders});
    },

    componentDidMount() {
        socket.on('order/new', this.setNewOrder)
    },

    render() {
        // renders stuff
    }
});

If any more info is needed, please say so in the comments below Everything is up to date (Node.js, React.js, Socket.io etc) 如果需要更多信息,请在下面的评论中说,一切都是最新的(Node.js,React.js,Socket.io等)

I see one problem with setNewOrder. 我看到setNewOrder的一个问题。

    var OrdersViewer = React.createClass({

            setNewOrder (order) {
                    console.log("new order gotten nr.", this.state.orders.length);
                    // this doesn't always get called...
                    var orders = _.cloneDeep(this.state.orders);
                    orders.push(order);
                    //this.setState({orders});
                    this.setState({orders: orders});
            },

            componentDidMount() {
                    socket.on('order/new', this.setNewOrder)
            },

            render() {
                    // renders stuff
            }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM