บทที่ 5 โปรโตคอล WebSocket และ JS SDK

Last Updated: 2026-10-25 02:56 Views: 5

บทที่ 5 โปรโตคอล WebSocket และ JS SDK

5.1 การเชื่อมต่อและการยืนยันตัวตน

ws://127.0.0.1:8899/ws?token=T        # plain
wss://host:8898/ws?token=T            # TLS (required when the page is https)
  • ข้อความหนึ่งข้อความคือการส่งงานหนึ่งครั้ง: ส่งเทมเพลตและข้อมูล (หรือ data URL) ได้โดยตรง เซิร์ฟเวอร์รับ เรนเดอร์ และพิมพ์ โปรโตคอลบนสายไม่มีซองเมธอด
  • การสอบถาม (เครื่องพิมพ์/เทมเพลต/รายละเอียดงาน/ยกเลิก) ไปทาง REST เสมอ WS พกเฉพาะการส่งงานและเหตุการณ์เท่านั้น
  • การเต้นหัวใจใช้เฟรม ping/pong ระดับโปรโตคอล (เบราว์เซอร์ตอบ อัตโนมัติ)
  • การเชื่อมต่อใหม่: หลังเชื่อมกลับมาให้สอบถามงานที่ยังค้างอยู่ผ่าน REST GET /api/jobs/{id} (เหตุการณ์ไม่ถูกเล่นซ้ำ) การยกเลิกไป ทาง REST

5.2 ข้อความส่งงาน (ไคลเอนต์ → เซิร์ฟเวอร์)

ฟิลด์ ชนิด จำเป็น คำอธิบาย
type string ✓ "print" (พิมพ์ลงกระดาษ) หรือ "preview" (เรนเดอร์อย่างเดียว)
request_id string ✓ ไคลเอนต์สร้างขึ้นเอง ใช้สัมพันธ์กับ accepted/done/error
template string | object ✓ เหมือน REST: id / {url} / ออบเจกต์อินไลน์
data any | object ✓ เหมือน REST: อินไลน์ (อาร์เรย์ = แบตช์) / {json}/{path} / {url,headers,timeout_s}
printer string ชื่อเครื่องพิมพ์ (ไม่ระบุ = ค่าเริ่มต้นของระบบ)
copies / copies_per_record / collate / pages เหมือน REST /api/print
options object เหมือน REST

type:"preview" เทียบเท่ากับ REST POST /api/print แบบ "preview": true: เมื่อเสร็จแล้ว ถาดระบบจะเปิดหน้าต่างตัวอย่าง เนทีฟ (หัวข้อ 9.3) หากไม่มีถาดระบบก็ใช้คอมโพเนนต์เว็บ /preview/{job_id}

{ "type": "print", "request_id": "r1",
  "template": "sf-waybill-100x93",
  "data": { "url": "https://erp.example.com/api/waybill/12345" },
  "printer": "HP-LaserJet-M1005", "copies_per_record": 3 }

5.3 ข้อความเหตุการณ์ (เซิร์ฟเวอร์ → ไคลเอนต์)

type ตัวกระตุ้น ฟิลด์สำคัญ
accepted รับการส่งงานแล้ว request_id, job_id, total_pages
job สถานะงานเปลี่ยน / ความคืบหน้า job_id, status, page_done, total_pages (broadcast)
printer สถานะเครื่องพิมพ์เปลี่ยน name, state (broadcast)
done งานเสร็จสิ้น request_id, job_id, status
error การส่งงานล้มเหลว request_id, code, message (รหัสตามหัวข้อ 4.2)
{ "type": "accepted", "request_id": "r1", "job_id": "j_01J8…", "total_pages": 6 }
{ "type": "job", "job_id": "j_01J8…", "status": "printing", "page_done": 4, "total_pages": 6 }
{ "type": "printer", "name": "HP-LaserJet-M1005", "state": "out_of_paper" }
{ "type": "done", "request_id": "r1", "job_id": "j_01J8…", "status": "done" }
{ "type": "error", "request_id": "r1", "code": "E_RENDER", "message": "…" }

5.4 การใช้โปรโตคอลดิบ (เบราว์เซอร์)

แนะนำให้ใช้ JS SDK (หัวข้อ 5.5) โปรโตคอลดิบมีไว้เพื่อทำความเข้าใจ และดีบัก:

const ws = new WebSocket("ws://127.0.0.1:8899/ws?token=T");
ws.onopen = () => ws.send(JSON.stringify({
  type: "print", request_id: "r1",
  template: "sf-waybill-100x93",
  data: { url: "https://erp.example.com/api/waybill/12345" },
  copies_per_record: 3 }));
ws.onmessage = (e) => {
  const m = JSON.parse(e.data);
  if (m.type === "accepted") console.log("job:", m.job_id, m.total_pages);
  if (m.type === "job")     console.log(m.status, m.page_done + "/" + m.total_pages);
  if (m.type === "done")    console.log("finished");
  if (m.type === "error")   console.error(m.code, m.message);
};

5.5 เอกสารอ้างอิง JS SDK (wxprint.js)

เซิร์ฟเวอร์โฮสต์ http://127.0.0.1:8899/sdk/wxprint.js (UMD/ESM) ครอบโปรโตคอล WS และสคริปต์ API เป็นเมธอด นำเข้าด้วย:

<script src="http://127.0.0.1:8899/sdk/wxprint.js"></script>

ตัวเลือกของคอนสตรักเตอร์

const wp = new wxprint.WxPrint({ host: "127.0.0.1", port: 8899,
                                 tlsPort: 8898, token: "T",
                                 reconnectDelay: 2000 });
// or the factory: const wp = await wxprint.connect({ token: "T" });
ตัวเลือก ชนิด ค่าเริ่มต้น คำอธิบาย
host string location.hostname โฮสต์ของบริการ
port int 8899 พอร์ตธรรมดา (http/ws)
tlsPort int 8898 พอร์ต TLS (ใช้อัตโนมัติเมื่อหน้าเว็บเป็น https)
token string null token สำหรับยืนยันตัวตน
reconnectDelay int 2000 ช่วงห่างระหว่างการเชื่อมใหม่ (ms)

เมธอดพิมพ์ตรง (ครอบโปรโตคอล WS)

เมธอด พารามิเตอร์ ค่าที่คืน คำอธิบาย
connect() — Promise สร้างการเชื่อมต่อ (เชื่อมใหม่อัตโนมัติหลังหลุดตาม reconnectDelay การส่งงานขณะออฟไลน์เข้าคิวและถูกส่งออกทันทีเมื่อเชื่อมใหม่)
close() — — ปิดการเชื่อมต่อ (ไม่เชื่อมใหม่อีก)
print(req) ฟิลด์ตามหัวข้อ 5.2 Promise<{job_id, total_pages}> ส่งพิมพ์ลงกระดาษ
preview(req) เหมือนเดิม Promise ส่งงานตัวอย่าง (เมื่อเสร็จหน้าต่างถาดระบบจะเด้งขึ้น)
saveas(req, format) req + `“pdf” “png” “zip”`
printers() — Promise รายชื่อเครื่องพิมพ์ (ส่งต่อให้ REST)
job(jobId) string Promise สถานะงานพิมพ์ (ส่งต่อให้ REST)
onJob(cb) (ev) => void this ติดตามเหตุการณ์งานพิมพ์ (การเปลี่ยนสถานะ + ความคืบหน้า)
onPrinter(cb) (ev) => void this ติดตามเหตุการณ์เครื่องพิมพ์

การสร้างเนื้อหาแบบโปรแกรม (DocBuilder ครอบสคริปต์ API)

wp.document() คืนค่าบิลเดอร์แบบต่อโซ่ที่ verb ของมันแม็ปหนึ่ง ต่อหนึ่งกับบทที่ 6:

เมธอด พารามิเตอร์ คำอธิบาย
init(taskName) string ชื่องาน (แสดงในคิวการพิมพ์)
pagesize(w, h, opts?) mm + {orientation, continuous} ขนาดหน้า h=0 ปรับตามความสูงของเนื้อหา
loadData(data) object | array ข้อมูลอินไลน์ (อาร์เรย์ = แบตช์)
loadJSON(textOrPath) string ข้อความ JSON หรือพาธไฟล์ฝั่งเซิร์ฟเวอร์
loadURL(url, opts?) url + {headers, timeout_s} ดึงตอนรันไทม์
addText(el) / addHtml(el) / addTable(el) ออบเจกต์องค์ประกอบ ฟิลด์ตามหัวข้อ 7.5–7.7
addBarcode(el) / addQrcode(el) / addImage(el) ออบเจกต์องค์ประกอบ ฟิลด์ตามหัวข้อ 7.8–7.10
addRect(el) / addLine(el) / addChart(el) ออบเจกต์องค์ประกอบ ฟิลด์ตามหัวข้อ 7.4
newpage() — บังคับตัดหน้า
print(params?) {printer, copies, …} ส่งพิมพ์ลงกระดาษ
preview() — ส่งงานตัวอย่าง

ตัวอย่างฉบับสมบูรณ์

// ① Direct printing + event subscription
const wp = new wxprint.WxPrint({ token: "T" });
wp.onJob(ev => console.log(ev.status, ev.page_done + "/" + ev.total_pages));
wp.onPrinter(ev => console.log(ev.name, ev.state));
await wp.print({ template: "sf-waybill-100x93",
                 data: { url: "https://erp.example.com/api/waybill/12345" },
                 printer: "HP-LaserJet-M1005", copies_per_record: 3 });

// ② Programmatic building (no template file)
const doc = wp.document();
doc.init("Delivery note")
   .loadURL("https://erp.example.com/api/order/SO-1024")
   .pagesize(210, 297)
   .addText({ x: 20, y: 15, w: 80, h: 8, text: "{{title}}",
              font: { size_pt: 16, weight: 700 } })
   .addBarcode({ x: 20, y: 30, w: 60, h: 10,
                 symbology: "code128", content: "{{order_no}}" })
   .newpage()
   .addHtml({ x: 0, y: 0, w: 210, h: 297,
              html: "<table>{% for it in items %}<tr><td>{{it.name}}</td></tr>{% endfor %}</table>" });
await doc.print({ printer: "HP-LaserJet-M1005", copies: 2 });

5.6 ใช้ WS หรือ REST?

สถานการณ์ ข้อแนะนำ
ส่งงานจากหน้าเว็บ + คอลแบ็กความคืบหน้า/ความเสร็จแบบสด WS (หรือ print ของ SDK)
แบ็กเอนด์ส่งครั้งเดียว สอบถามสถานะเมื่อจำเป็น REST + การโพลล์
ยกเลิก / สอบถามรายละเอียด REST (WS ไม่พกการสอบถาม)
หน้าเว็บ HTTPS wss (SDK สลับไปพอร์ต TLS ให้อัตโนมัติ)
← Previous Article บทที่ 4 เอกสารอ้างอิง HTTP API Next Article → บทที่ 6 เอกสารอ้างอิงสคริปต์ API

Comments

No comments yet