initial commit

This commit is contained in:
2023-07-19 11:56:01 +03:00
commit fb7610845e
17 changed files with 3291 additions and 0 deletions

36
src/env/node/index.ts vendored Normal file
View File

@ -0,0 +1,36 @@
import { WebSocket, RawData } from 'ws'
import { SocketAdapter } from '../../helpers/SocketAdapter'
class NodeSocketAdapter extends SocketAdapter {
private socket: WebSocket
constructor(url: string) {
super()
this.socket = new WebSocket(url)
this.socket.on('message', this.emitMessage.bind(this))
this.socket.on('error', err => this.emit('error', err))
this.socket.on('open', () => this.emit('open', null))
this.socket.on('close', () => this.emit('close', null))
}
public send(message: string) {
this.socket.send(message)
}
public close() {
this.socket.close()
}
private emitMessage(message: RawData) {
if(Array.isArray(message)) {
message = Buffer.concat(message)
}
if(message instanceof ArrayBuffer) {
message = Buffer.from(message)
}
this.emit('message', message.toString())
}
}
export { NodeSocketAdapter as SocketAdapter }