Fixed tag parameter stuff, added tag to Rinit, added format checking
This commit is contained in:
parent
19638966bb
commit
871979105c
5 changed files with 30 additions and 26 deletions
|
@ -23,11 +23,11 @@ A client sends *requests* (T-messages) to a server, which subsequently *replies*
|
|||
[s] means a string that goes until the end of the message.
|
||||
[j] means JSON data that goes until the end of the message.
|
||||
`tag` is used to identify a request/response pair, like `lock` in the original API. `jid` is used to identify a job. `job` is a job object.
|
||||
- Rerror tag[4] error[s]
|
||||
- Tqueue tag[4] jid[4] job[j]
|
||||
- Rqueue tag[4]
|
||||
- Tcancel tag[4] jid[4]
|
||||
- Rcancel tag[4]
|
||||
- Twait tag[4] jid[4]
|
||||
- Rwait tag[4]
|
||||
- Rinit max_jobs[2] formats[j]
|
||||
- Rerror tag[2] error[s]
|
||||
- Tqueue tag[2] jid[4] job[j]
|
||||
- Rqueue tag[2]
|
||||
- Tcancel tag[2] jid[4]
|
||||
- Rcancel tag[2]
|
||||
- Twait tag[2] jid[4]
|
||||
- Rwait tag[2]
|
||||
- Rinit tag[2] max_jobs[2] formats[j]
|
||||
|
|
16
api/index.js
16
api/index.js
|
@ -88,7 +88,11 @@ wss.on("connection", (ws, request) => {
|
|||
log(`WS client ${request.socket.remoteAddress}:${request.socket.remotePort} has connected`);
|
||||
const num = Buffer.alloc(2);
|
||||
num.writeUInt16LE(MAX_JOBS);
|
||||
const init = Buffer.concat([Buffer.from([Rinit]), num, Buffer.from(JSON.stringify(Object.keys(magick)))]);
|
||||
const formats = {};
|
||||
for (const cmd of Object.keys(magick)) {
|
||||
formats[cmd] = ["image/png", "image/gif", "image/jpeg", "image/webp"];
|
||||
}
|
||||
const init = Buffer.concat([Buffer.from([Rinit]), Buffer.from([0x00, 0x00]), num, Buffer.from(JSON.stringify(formats))]);
|
||||
ws.send(init);
|
||||
|
||||
ws.on("error", (err) => {
|
||||
|
@ -97,11 +101,11 @@ wss.on("connection", (ws, request) => {
|
|||
|
||||
ws.on("message", (msg) => {
|
||||
const opcode = msg.readUint8(0);
|
||||
const tag = msg.slice(1, 5);
|
||||
const req = msg.toString().slice(5, msg.length);
|
||||
const tag = msg.slice(1, 3);
|
||||
const req = msg.toString().slice(3);
|
||||
if (opcode == Tqueue) {
|
||||
const id = msg.readUInt32LE(5);
|
||||
const obj = msg.slice(9, msg.length);
|
||||
const id = msg.readUInt32LE(3);
|
||||
const obj = msg.slice(7);
|
||||
const job = { msg: obj, num: jobAmount };
|
||||
jobs.set(id, job);
|
||||
queue.push(id);
|
||||
|
@ -121,7 +125,7 @@ wss.on("connection", (ws, request) => {
|
|||
const cancelResponse = Buffer.concat([Buffer.from([Rcancel]), tag]);
|
||||
ws.send(cancelResponse);
|
||||
} else if (opcode == Twait) {
|
||||
const id = msg.readUInt32LE(5);
|
||||
const id = msg.readUInt32LE(3);
|
||||
const job = jobs.get(id);
|
||||
if (!job) {
|
||||
const errorResponse = Buffer.concat([Buffer.from([Rerror]), tag, Buffer.from("Invalid job ID")]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue