Skip to content

Commit

Permalink
websocket mode
Browse files Browse the repository at this point in the history
  • Loading branch information
d07RiV committed Aug 15, 2019
1 parent a702a42 commit 0153f01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packet.js
Expand Up @@ -40,7 +40,7 @@ class buffer_reader {
this.pos += length;
return result;
}
rest() {
read_buf() {
const size = this.read32();
const result = this.buffer.subarray(this.pos, this.pos + size);
this.pos += size;
Expand Down Expand Up @@ -86,11 +86,15 @@ class buffer_writer {
return this;
}
rest(value) {
this.write32(value.byteLength);
this.buffer.set(value, this.pos);
this.pos += value.byteLength;
return this;
}
write_buf(value) {
this.write32(value.byteLength);
this.rest(value);
return this;
}
}

const RejectionReason = {
Expand Down Expand Up @@ -194,9 +198,9 @@ const server_packet = {
},
message: {
code: 0x01,
read: reader => ({id: reader.read8(), payload: reader.rest()}),
read: reader => ({id: reader.read8(), payload: reader.read_buf()}),
size: ({payload}) => 5 + payload.byteLength,
write: (writer, {id, payload}) => writer.write8(id).rest(payload),
write: (writer, {id, payload}) => writer.write8(id).write_buf(payload),
},
turn: {
code: 0x02,
Expand Down Expand Up @@ -246,9 +250,9 @@ const client_packet = {
},
message: {
code: 0x01,
read: reader => ({id: reader.read8(), payload: reader.rest()}),
read: reader => ({id: reader.read8(), payload: reader.read_buf()}),
size: ({payload}) => 5 + payload.byteLength,
write: (writer, {id, payload}) => writer.write8(id).rest(payload),
write: (writer, {id, payload}) => writer.write8(id).write_buf(payload),
},
turn: {
code: 0x02,
Expand Down
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -118,7 +118,7 @@ class player_client {
this.ws.send(writer.result);
this.batch.length = 0;
}
}, 200);
}, 100);
}

send(msg) {
Expand Down

0 comments on commit 0153f01

Please sign in to comment.