mirror of
https://git.hmsn.ink/kospo/helptalk/api.git
synced 2026-03-20 07:43:33 +09:00
first
This commit is contained in:
46
sample/prod/talk/js/module/queue.js
Normal file
46
sample/prod/talk/js/module/queue.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import {global} from 'http://talk.kospo.co.kr:3000/static/js/module/variable.js';
|
||||
import {talkEvent} from 'http://talk.kospo.co.kr:3000/static/js/module/talkEvent.js';
|
||||
import Common from "http://talk.kospo.co.kr:3000/static/js/utils.js";
|
||||
|
||||
class SocketQueue {
|
||||
constructor() {
|
||||
this.queue = [];
|
||||
this.isSending = false;
|
||||
}
|
||||
|
||||
enqueue(data) {
|
||||
this.queue.push(data);
|
||||
if(!this.isSending) {
|
||||
this.processQueue();
|
||||
}
|
||||
}
|
||||
|
||||
async processQueue() {
|
||||
this.isSending = true;
|
||||
while(this.queue.length > 0) {
|
||||
const data = this.queue.shift();
|
||||
try {
|
||||
await this.sendToSocket(data);
|
||||
Common.sleep(30).then(() => {
|
||||
this.isSending = false;
|
||||
});
|
||||
} catch(err) {
|
||||
this.isSending = false;
|
||||
console.error('전송실패', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async sendToSocket(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(global.sockJS.readyState === WebSocket.OPEN) {
|
||||
talkEvent.send(global.talkParams(data));
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error('소켓 연결 안됨'))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default SocketQueue
|
||||
Reference in New Issue
Block a user