小编Dav*_*ave的帖子

如何将javascript数组转换为二进制数据并返回websocket?

我想发送这个示例数组

[{
    id: 1,
    name: "test",
    position: [1234,850], //random position on the map
    points: 100 //example points
}];
Run Code Online (Sandbox Code Playgroud)

到我的websocket服务器作为二进制数据.在服务器端,我希望将二进制数据解码回数组,进行更改并将二进制数据发送回客户端.最后在客户端如何将二进制数据解码回数组?

截屏示例我的意思是:

在此输入图像描述

这是我的实际代码:

var connection = new WebSocket('wss://my_website.eu:1234');
connection.binaryType = "ArrayBuffer";
connection.onmessage = function (event) {

    // console.log(event);
    if (event.data instanceof window["ArrayBuffer"]) {
        var data3 = JSON.parse(String.fromCharCode.apply(null, new Uint16Array(event.data)));
        console.log(data3);
    } else {
        console.log(event.data); // Blob {size: 0, type: ""}
    }
};

$("body").mousemove(function( event ) {

    var data = {
        name: "lol",
        pos: [event.pageX, event.pageY]
    };

    //convert to binary frame
    var data2 …
Run Code Online (Sandbox Code Playgroud)

javascript websocket

6
推荐指数
1
解决办法
1万
查看次数

如何创建数组副本?

我有阵列:

var array = ["a", "b", "c"];
Run Code Online (Sandbox Code Playgroud)

我需要将此数组保存到另一个变量

var save = array;
Run Code Online (Sandbox Code Playgroud)

现在我需要从save第一个索引进行拼接,但是当我尝试它时,从两个数组中删除索引.

var array = ["a", "b", "c"];
var save = array;

save.splice(0, 1);
console.log(array);
console.log(save);
Run Code Online (Sandbox Code Playgroud)

javascript arrays

5
推荐指数
1
解决办法
131
查看次数

如何计算鼠标的路径?

我有虚幻的世界:

var worldSize = [10000, 10000]; //width, height
Run Code Online (Sandbox Code Playgroud)

接下来我有球员的位置:

var currentPlayerPosition = [5000, 5000]; //center on the map
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在我需要每10ms检查当前鼠标位置,并计算鼠标的方向,并更新currentPlayerPosition到方向结果-1px(如果鼠标在播放器下,则更新+ 1px ...).与右/左侧相同.

编辑:我不需要代码,我需要mathematica来计算方向,然后减去/添加像素到玩家位置的数组[5000,5000]

编辑2

我的意思是:

如果currentPlayerPosition[5000,5000]mousePosition[4500,4800]那么我必须从数学currentPlayerPosition每10ms,如果我想移动玩家的鼠标位置?

currentPlayerPosition[0] = currentPlayerPosition[0]-(what_number_here)
currentPlayerPosition[1] = currentPlayerPosition[1]-(what_number_here)
Run Code Online (Sandbox Code Playgroud)

要么

currentPlayerPosition[0] = currentPlayerPosition[0]+(what_number_here)
currentPlayerPosition[1] = currentPlayerPosition[1]+(what_number_here)
Run Code Online (Sandbox Code Playgroud)

编辑3

我想让这个问题更好,所以我在这里写下我的(坏)想法:

我先尝试计算距离X和Y:

var fullWidth = 5000;
var playerPositionX = 2500;
var mousePositionX = 2700;
var distanceBetweenX = mousePositionX-playerPositionX; //200
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试的相同代码计算Y距离.我得到了这样的结果:

distanceBetweenX = 200;
distanceBetweenY = 150; …
Run Code Online (Sandbox Code Playgroud)

javascript

-4
推荐指数
1
解决办法
272
查看次数

标签 统计

javascript ×3

arrays ×1

websocket ×1