TypeError: Invalid non-string/buffer chunk with WebSocket's

时间:2016-06-16 17:54:17

标签: javascript websocket typeerror

I've been getting a lot of TypeError's recently with ws和Node.JS。我尝试做的是使用简单的for循环向所有用户广播消息。我发送的数组是一个UInt8Array(),如果有帮助的话。这是功能:

function broadcast(data){
    for(var i = 0; i < clients.length; i++){
        if(clients[i]){
            clients[i].send(data, {binary:true});
        }
    }
}

电话:

broadcast(messages.movement.build(conn.id, conn.snake.direction.x, conn.snake.direction.y));

broadcast(messages.direction.build(conn.id, conn.snake.direction));

消息/运动:

var message = require('../utils/message');

var type = 'G'.charCodeAt(0);

exports.build = function(id, x, y) {
    var arr = new Uint8Array(9);
    var b = 0;
    b += message.writeInt8(b, arr, 0);
    b += message.writeInt8(b, arr, 0);
    b += message.writeInt8(b, arr, type);
    b += message.writeInt16(b, arr, id);
    b += message.writeInt8(b, arr, x);
    b += message.writeInt8(b, arr, y);
    return arr;
};

消息/方向:

var message = require('../utils/message');

var type = 'e'.charCodeAt(0);

exports.build = function(id, direction) {
    var arr = new Uint8Array(7);
    var b = 0;
    b += message.writeInt8(b, arr, 0);
    b += message.writeInt8(b, arr, 0);
    b += message.writeInt8(b, arr, type);
    b += message.writeInt16(b, arr, id);
    b += message.writeInt8(b, arr, direction.angle);
    b += message.writeInt8(b, arr, 104);
    b += message.writeInt8(b, arr, 0);
    return arr;
};

0 个答案:

没有答案