combine wsclient and wswrapper
This commit is contained in:
parent
b695b44b6c
commit
d5e0c89bf6
2 changed files with 69 additions and 77 deletions
|
|
@ -1,10 +1,32 @@
|
|||
"use strict"
|
||||
|
||||
import WsWrapper from './WsWrapper.js'
|
||||
import Time from '../common/Time.js'
|
||||
|
||||
const CODE_CUSTOM_DISCONNECT = 4000
|
||||
|
||||
/**
|
||||
* Wrapper around ws that
|
||||
* - buffers 'send' until a connection is available
|
||||
* - automatically tries to reconnect on close
|
||||
*/
|
||||
export default class WsClient {
|
||||
// actual ws handle
|
||||
handle = null
|
||||
|
||||
// timeout for automatic reconnect
|
||||
reconnectTimeout = null
|
||||
|
||||
// buffer for 'send'
|
||||
sendBuffer = []
|
||||
|
||||
export default class WsClient extends WsWrapper {
|
||||
constructor(addr, protocols) {
|
||||
super(addr, protocols)
|
||||
this.addr = addr
|
||||
this.protocols = protocols
|
||||
|
||||
this.onopen = () => {}
|
||||
this.onclose = () => {}
|
||||
this.onmessage = () => {}
|
||||
|
||||
this._on = {}
|
||||
this.onopen = (e) => {
|
||||
this._dispatch('socket', 'open', e)
|
||||
|
|
@ -23,6 +45,50 @@ export default class WsClient extends WsWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
send (txt) {
|
||||
if (this.handle) {
|
||||
this.handle.send(txt)
|
||||
} else {
|
||||
this.sendBuffer.push(txt)
|
||||
}
|
||||
}
|
||||
|
||||
connect() {
|
||||
let ws = new WebSocket(this.addr, this.protocols)
|
||||
ws.onopen = (e) => {
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
}
|
||||
this.handle = ws
|
||||
// should have a queue worker
|
||||
while (this.sendBuffer.length > 0) {
|
||||
this.handle.send(this.sendBuffer.shift())
|
||||
}
|
||||
this.onopen(e)
|
||||
}
|
||||
ws.onmessage = (e) => {
|
||||
this.onmessage(e)
|
||||
}
|
||||
ws.onerror = (e) => {
|
||||
this.handle = null
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1 * Time.SEC)
|
||||
this.onclose(e)
|
||||
}
|
||||
ws.onclose = (e) => {
|
||||
this.handle = null
|
||||
if (e.code !== CODE_CUSTOM_DISCONNECT) {
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1 * Time.SEC)
|
||||
}
|
||||
this.onclose(e)
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
if (this.handle) {
|
||||
this.handle.close(CODE_CUSTOM_DISCONNECT)
|
||||
}
|
||||
}
|
||||
|
||||
onSocket(tag, callback) {
|
||||
this.addEventListener('socket', tag, callback)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,74 +0,0 @@
|
|||
"use strict"
|
||||
|
||||
import Time from '../common/Time.js'
|
||||
|
||||
const CODE_CUSTOM_DISCONNECT = 4000
|
||||
|
||||
/**
|
||||
* Wrapper around ws that
|
||||
* - buffers 'send' until a connection is available
|
||||
* - automatically tries to reconnect on close
|
||||
*/
|
||||
export default class WsWrapper {
|
||||
// actual ws handle
|
||||
handle = null
|
||||
|
||||
// timeout for automatic reconnect
|
||||
reconnectTimeout = null
|
||||
|
||||
// buffer for 'send'
|
||||
sendBuffer = []
|
||||
|
||||
constructor(addr, protocols) {
|
||||
this.addr = addr
|
||||
this.protocols = protocols
|
||||
|
||||
this.onopen = () => {}
|
||||
this.onclose = () => {}
|
||||
this.onmessage = () => {}
|
||||
}
|
||||
|
||||
send (txt) {
|
||||
if (this.handle) {
|
||||
this.handle.send(txt)
|
||||
} else {
|
||||
this.sendBuffer.push(txt)
|
||||
}
|
||||
}
|
||||
|
||||
connect() {
|
||||
let ws = new WebSocket(this.addr, this.protocols)
|
||||
ws.onopen = (e) => {
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
}
|
||||
this.handle = ws
|
||||
// should have a queue worker
|
||||
while (this.sendBuffer.length > 0) {
|
||||
this.handle.send(this.sendBuffer.shift())
|
||||
}
|
||||
this.onopen(e)
|
||||
}
|
||||
ws.onmessage = (e) => {
|
||||
this.onmessage(e)
|
||||
}
|
||||
ws.onerror = (e) => {
|
||||
this.handle = null
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1 * Time.SEC)
|
||||
this.onclose(e)
|
||||
}
|
||||
ws.onclose = (e) => {
|
||||
this.handle = null
|
||||
if (e.code !== CODE_CUSTOM_DISCONNECT) {
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1 * Time.SEC)
|
||||
}
|
||||
this.onclose(e)
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
if (this.handle) {
|
||||
this.handle.close(CODE_CUSTOM_DISCONNECT)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue