Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js websockets客户端服务器数据包_Node.js_Websocket_Buffer - Fatal编程技术网

Node.js websockets客户端服务器数据包

Node.js websockets客户端服务器数据包,node.js,websocket,buffer,Node.js,Websocket,Buffer,我正在编写一个客户端和服务器,它们显然必须相互协作。在客户端和服务器中,我都有一个类来构造消息。这些消息通过WebSocket发送 现在问题在于我用来构造消息的方法。我使用了缓冲区类和read…BE函数和write…BE函数。不知何故,写在客户机上的数据与服务器上读取的数据不一致 发送到服务器的消息的一个示例是登录消息: import ClientMessage from "../../protocol/ClientMessage"; import { LOGIN } from "../../p

我正在编写一个客户端和服务器,它们显然必须相互协作。在客户端和服务器中,我都有一个类来构造消息。这些消息通过WebSocket发送

现在问题在于我用来构造消息的方法。我使用了缓冲区类和read…BE函数和write…BE函数。不知何故,写在客户机上的数据与服务器上读取的数据不一致

发送到服务器的消息的一个示例是登录消息:

import ClientMessage from "../../protocol/ClientMessage";
import { LOGIN } from "../../protocol/OpCodes/ClientOpCodes";

export default class RequestLogin extends ClientMessage {
    constructor(username: string, look: string) {
        super(LOGIN);
        this.appendString(username);
        this.appendString(look);
    }
}
来自操作码的登录名等于1,消息ID应为1

接下来,按下登录按钮后,将执行以下代码:

this.game.communicationManager.sendMessage(new RequestLogin(username, look));
这是sendMessage函数:

sendMessage(message: ClientMessage) {
    if (this.client.connected) {
        //console.log('Sent [' + message.id + ']: ' + message.constructor.name);
        this.client.send(message.getPacket());
    }
}
最后,这是ClientMessage类:

export default class ClientMessage {
    body: Buffer;
    pos: number;

    constructor(id: number) {
        this.body = Buffer.alloc(512);
        this.pos = 0;

        this.appendShort(id);
    }

    appendShort(i: number) {
        this.body.writeInt16BE(i, this.pos);
        this.pos += 2;
    }

    appendInt(i: number) {
        this.body.writeInt32BE(i, this.pos);
        this.pos += 4;
    }

    appendString(str: string) {
        this.appendShort(str.length);
        this.body.write(str, 'utf8');
        this.pos += str.length + 2;
    }

    getPacket(): string {
        return this.body.toString();
    }
}
(我知道512有点过头了,但我认为它在阅读方面并不重要)

现在,转到服务器,服务器端的ClientMessage如下所示:

export default class ClientMessage {
    packet: Buffer;
    id: number;
    pos:number;

    constructor(packet:string) {
        this.packet = Buffer.from(packet);
        this.pos = 0;
        this.id = this.getShort();
    }

    getShort(): number {
        const value = this.packet.readInt16BE(this.pos);
        this.pos += 2;
        return value;
    }

    getInt(): number {
        const value = this.packet.readInt32BE(this.pos);
        this.pos += 4;
        return value;
    }

    getString(): string {
        const strlen = this.getShort();
        const value = this.packet.toString('utf8', this.pos, this.pos + strlen);
        this.pos += strlen;
        return value;
    }
}
这是
This.ws.on('message'

onDataArrival(message:string): any {
    const clientMessage:ClientMessage = new ClientMessage(message);
    console.log(clientMessage.id);
    console.log(clientMessage.getString());
}

奇怪的是,id已经错了。如果它应该是1(因为登录名等于1),
clientMessage.id
的值是26738,这要高得多。我不确定我做错了什么或缺少了什么。(当然,字符串也不正确)

已经找到了。问题是我在客户端的appendString

appendString(str: string) {
    this.appendShort(str.length);
    this.body.write(str, this.pos, 'utf8');
    this.pos += str.length;
}
当然,我必须在正确的位置写字符串