zyno-bot-addons
Advanced tools
Comparing version 1.1.1 to 1.1.2
{ | ||
"name": "zyno-bot-addons", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Create easily addons for Zyno Bot", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const EventEmitter = require('events'); | ||
const http = require('http'); | ||
const WebSocket = require('./websocket.js'); | ||
const RequestInfo = require('./requestInfo.js'); | ||
const { WebSocketServer } = require('ws'); | ||
const { ValueSaver } = require('valuesaver'); | ||
const websockets = new ValueSaver(); | ||
class WebSocketHandler extends EventEmitter{ | ||
constructor(httpServer = http.Server.prototype){ | ||
class WebSocket extends EventEmitter{ | ||
constructor(socket, handler){ | ||
super(); | ||
const wsServer = new WebSocketServer({ | ||
server: httpServer | ||
let id = Math.round(new Date().getTime() / 1000).toString() + Math.round(Math.random() * 9999).toString(); | ||
while(handler.sockets.filter(ws => ws.id === id).length > 0){ | ||
id = Math.round(new Date().getTime() / 1000).toString() + Math.round(Math.random() * 9999).toString(); | ||
} | ||
this.id = id; | ||
this.send = function(data){ | ||
socket.send(data); | ||
}; | ||
this.close = () => { | ||
socket.close(); | ||
this.emit('close'); | ||
this.removeAllListeners(); | ||
}; | ||
socket.on('message', msg => { | ||
this.emit('message', Buffer.from(msg).toString()); | ||
}); | ||
wsServer.on('connection', (_ws, req) => { | ||
const reqInfo = new RequestInfo(req, null); | ||
const ws = new WebSocket(_ws, this); | ||
websockets.set(ws.id, _ws); | ||
this.sockets.push(ws); | ||
this.emit('connection', ws, reqInfo); | ||
socket.on('close', () => { | ||
this.emit('close'); | ||
this.removeAllListeners(); | ||
}); | ||
socket.on('error', err => { | ||
var errorMessage = err.message.toString(); | ||
handler.errorMessages.push(errorMessage); | ||
}) | ||
} | ||
sockets = []; | ||
errorMessages = []; | ||
} | ||
module.exports = WebSocketHandler; | ||
module.exports = WebSocket; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
437965
9130
5