test deregistering chunks

This commit is contained in:
HF 2022-07-03 22:56:00 +02:00
parent 09157955fa
commit 14aaa3211d
5 changed files with 42 additions and 52 deletions

View File

@ -141,9 +141,9 @@ document.addEventListener('DOMContentLoaded', () => {
let cnt = 0; let cnt = 0;
chunks.forEach((value, key) => { chunks.forEach((value, key) => {
if (curTime > value.timestamp + 300000) { if (curTime > value.timestamp + 300000) {
cnt++;
const [zc, xc, yc] = value.cell; const [zc, xc, yc] = value.cell;
if (!renderer.isChunkInView(zc, xc, yc)) { if (!renderer.isChunkInView(zc, xc, yc)) {
cnt++;
if (value.isBasechunk) { if (value.isBasechunk) {
SocketClient.deRegisterChunk([xc, yc]); SocketClient.deRegisterChunk([xc, yc]);
} }

View File

@ -69,6 +69,7 @@ class SocketClient extends EventEmitter {
if (now - 23000 > this.timeLastSent) { if (now - 23000 > this.timeLastSent) {
// make sure we send something at least all 25s // make sure we send something at least all 25s
this.send(Ping.dehydrate()); this.send(Ping.dehydrate());
this.timeLastSent = now;
} }
} }
} }

View File

@ -145,13 +145,13 @@ class SocketServer {
country, country,
) => { ) => {
const text = JSON.stringify([name, message, country, channelId, id]); const text = JSON.stringify([name, message, country, channelId, id]);
const clientArray = [];
this.wss.clients.forEach((ws) => { this.wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN && ws.user) { if (ws.user && chatProvider.userHasChannelAccess(ws.user, channelId)) {
if (chatProvider.userHasChannelAccess(ws.user, channelId)) { clientArray.push(ws);
ws.send(text);
}
} }
}); });
SocketServer.broadcastSelected(clientArray, text);
}); });
socketEvents.on('addChatChannel', (userId, channelId, channelArray) => { socketEvents.on('addChatChannel', (userId, channelId, channelArray) => {
@ -219,34 +219,46 @@ class SocketServer {
* https://github.com/websockets/ws/issues/617 * https://github.com/websockets/ws/issues/617
* @param data * @param data
*/ */
broadcast(data) { static broadcastSelected(clients, data) {
let frames;
if (typeof data === 'string') { if (typeof data === 'string') {
this.wss.clients.forEach((ws) => { frames = WebSocket.Sender.frame(Buffer.from(data), {
if (ws.readyState === WebSocket.OPEN) { readOnly: false,
ws.send(data); mask: false,
} rsv1: false,
opcode: 1,
fin: true,
}); });
} else { } else {
const frame = WebSocket.Sender.frame(data, { frames = WebSocket.Sender.frame(data, {
readOnly: true, readOnly: false,
mask: false, mask: false,
rsv1: false, rsv1: false,
opcode: 2, opcode: 2,
fin: true, fin: true,
}); });
this.wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) {
frame.forEach((buffer) => {
try {
// eslint-disable-next-line no-underscore-dangle
ws._socket.write(buffer);
} catch (error) {
logger.error(`WebSocket broadcast error: ${error.message}`);
}
});
}
});
} }
return clients.map((ws) => new Promise((resolve) => {
if (ws.readyState === WebSocket.OPEN) {
// eslint-disable-next-line no-underscore-dangle
ws._sender.sendFrame(frames, (err) => {
if (err) {
logger.error(`WebSocket broadcast error: ${err.message}`);
}
});
}
resolve();
}));
}
broadcast(data) {
const clientArray = [];
this.wss.clients.forEach((ws) => {
clientArray.push(ws);
});
SocketServer.broadcastSelected(clientArray, data);
} }
/* /*
@ -305,34 +317,11 @@ class SocketServer {
} }
broadcastPixelBuffer(canvasId: number, chunkid, data: Buffer) { broadcastPixelBuffer(canvasId: number, chunkid, data: Buffer) {
const frame = WebSocket.Sender.frame(data, {
readOnly: true,
mask: false,
rsv1: false,
opcode: 2,
fin: true,
});
if (this.CHUNK_CLIENTS.has(chunkid)) { if (this.CHUNK_CLIENTS.has(chunkid)) {
const clients = this.CHUNK_CLIENTS.get(chunkid); const clients = this.CHUNK_CLIENTS.get(chunkid)
clients.forEach((client) => { // eslint-disable-next-line eqeqeq
if (client.readyState === WebSocket.OPEN .filter((ws) => ws.canvasId == canvasId);
// canvasId can be number or string, caused by SocketServer.broadcastSelected(clients, data);
// js disctionaries not being able to have numbers as keys
// eslint-disable-next-line eqeqeq
&& client.canvasId == canvasId
) {
frame.forEach((buffer) => {
try {
// eslint-disable-next-line no-underscore-dangle
client._socket.write(buffer);
} catch (error) {
logger.error(
`WebSocket broadcast pixelbuffer error: ${error.message}`,
);
}
});
}
});
} }
} }

View File

@ -278,7 +278,6 @@ class ChunkLoader {
async fetchBaseChunk(zoom, cx, cy, chunkRGB) { async fetchBaseChunk(zoom, cx, cy, chunkRGB) {
const center = [zoom, cx, cy]; const center = [zoom, cx, cy];
this.store.dispatch(requestBigChunk(center)); this.store.dispatch(requestBigChunk(center));
chunkRGB.isBasechunk = true;
try { try {
const url = `chunks/${this.canvasId}/${cx}/${cy}.bmp`; const url = `chunks/${this.canvasId}/${cx}/${cy}.bmp`;
const response = await fetch(url); const response = await fetch(url);

View File

@ -41,6 +41,7 @@ class ChunkRGB {
// from Uint8Array // from Uint8Array
fromBuffer(chunkBuffer) { fromBuffer(chunkBuffer) {
this.isBasechunk = true;
const imageData = new ImageData(TILE_SIZE, TILE_SIZE); const imageData = new ImageData(TILE_SIZE, TILE_SIZE);
const imageView = new Uint32Array(imageData.data.buffer); const imageView = new Uint32Array(imageData.data.buffer);
const { abgr } = this.palette; const { abgr } = this.palette;