modify ws class

This commit is contained in:
Jay 2018-07-16 16:51:31 +08:00
parent aec6232cc6
commit cde2fb6195
2 changed files with 14 additions and 9 deletions

View File

@ -9,6 +9,7 @@ class WS {
constructor () { constructor () {
/** @type {WebSocket} */ /** @type {WebSocket} */
this.ws = null this.ws = null
this.join = []
} }
runBot () { runBot () {
@ -65,18 +66,30 @@ class WS {
// release pool connection // release pool connection
await db.release() await db.release()
this.joinChannel = []
if (result !== null && result.rowCount > 0) { if (result !== null && result.rowCount > 0) {
for (let row of result.rows) { for (let row of result.rows) {
if ('name' in row) { if ('name' in row) {
this.ws.send('JOIN #' + row.name) this.ws.send('JOIN #' + row.name)
this.join.push(row.name)
} }
} }
} }
} }
sendMsg (channel = null, message = null) {
if (this.ws === null || !('send' in this.ws) || typeof this.ws.send !== 'function') return null
if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null
if (message === null || typeof message !== 'string' || message.trim().length === 0) return null
if (this.join.indexOf(channel) === -1) return null
this.ws.send(`PRIVMSG #${channel} :${message}`)
}
joinChannel (channel = null) { joinChannel (channel = null) {
if (this.ws === null || !('send' in this.ws) || typeof this.ws.send !== 'function') return null
if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null
this.ws.send(`JOIN #${channel.trim()}`) this.ws.send(`JOIN #${channel.trim()}`)
this.join.push(channel.trim())
} }
} }

View File

@ -31,14 +31,6 @@ const msgSplit = async function (ws, msg) {
} }
} }
const sendMsg = async (ws, channel = null, msg = null) => {
if (!ws || !('send' in ws) || typeof ws.send !== 'function') return null
if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null
if (msg === null || typeof msg !== 'string' || msg.trim().length === 0) return null
ws.send(`PRIVMSG #${channel} :${msg}`)
}
module.exports = { module.exports = {
msgSplit, msgSplit
sendMsg
} }