2020-03-21 07:47:42 +00:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
|
2020-03-22 16:58:53 +00:00
|
|
|
import { Client } from './client'
|
2020-03-21 07:47:42 +00:00
|
|
|
import { SocketIO } from './interfaces';
|
2020-03-22 16:58:53 +00:00
|
|
|
import { ServerEvent } from '../server';
|
|
|
|
import { Socket } from './socket';
|
|
|
|
import { Adapter } from './adapter';
|
|
|
|
import { Server } from './index'
|
2020-03-23 10:33:12 +00:00
|
|
|
import { Packet } from './packet';
|
|
|
|
import { SubPacketTypes } from './types';
|
2020-03-21 07:47:42 +00:00
|
|
|
|
2020-03-22 16:58:53 +00:00
|
|
|
export class Namespace extends EventEmitter implements SocketIO.Namespace {
|
2020-03-21 07:47:42 +00:00
|
|
|
name: string;
|
2020-03-22 16:58:53 +00:00
|
|
|
server: Server;
|
2020-03-23 10:33:12 +00:00
|
|
|
sockets: { [id: string]: Socket; };
|
|
|
|
connected: { [id: string]: Socket; };
|
2020-03-21 07:47:42 +00:00
|
|
|
adapter: SocketIO.Adapter;
|
|
|
|
json: SocketIO.Namespace;
|
|
|
|
|
2020-03-23 10:33:12 +00:00
|
|
|
constructor(name: string, server: Server) {
|
2020-03-22 16:58:53 +00:00
|
|
|
super();
|
2020-03-21 07:47:42 +00:00
|
|
|
this.name = name;
|
2020-03-23 10:33:12 +00:00
|
|
|
this.server = server;
|
2020-03-22 16:58:53 +00:00
|
|
|
this.sockets = {};
|
|
|
|
this.connected = {};
|
|
|
|
this.adapter = new Adapter(this);
|
|
|
|
}
|
|
|
|
initAdapter() {
|
2020-03-23 10:33:12 +00:00
|
|
|
// @ts-ignore
|
|
|
|
this.adapter = new (this.server.adapter())()
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
add(client: Client, query?: any, callback?: () => void) {
|
|
|
|
// client.conn.request.url();
|
|
|
|
let socket = new Socket(this, client, {});
|
|
|
|
this.sockets[client.id] = socket;
|
|
|
|
client.nsps[this.name] = socket;
|
|
|
|
this.onconnection(socket);
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
del(client: Client) {
|
|
|
|
let socket = this.sockets[client.id];
|
|
|
|
socket.disconnect();
|
|
|
|
delete this.sockets[client.id];
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
use(fn: (socket: SocketIO.Socket, fn: (err?: any) => void) => void): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
// TODO
|
|
|
|
return this;
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
to(room: string): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
// TODO
|
|
|
|
return this;
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
in(room: string): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
return this.to(room);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
send(...args: any[]): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
// TODO
|
|
|
|
return this;
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
write(...args: any[]): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
return this.send(...args);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
clients(fn: Function): SocketIO.Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
return fn(Object.values(this.sockets))
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
compress(compress: boolean): SocketIO.Namespace {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
process(packet: Packet, client: Client) {
|
|
|
|
switch (packet.sub_type) {
|
|
|
|
case SubPacketTypes.CONNECT:
|
|
|
|
this.add(client);
|
|
|
|
break;
|
|
|
|
case SubPacketTypes.DISCONNECT:
|
|
|
|
this.del(client);
|
|
|
|
break;
|
|
|
|
case SubPacketTypes.EVENT:
|
|
|
|
this.sockets[client.id].onpacket(packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remove(socket: Socket) {
|
|
|
|
if (this.sockets.hasOwnProperty(socket.id)) {
|
|
|
|
delete this.sockets[socket.id];
|
|
|
|
} else {
|
|
|
|
// debug('ignoring remove for %s', socket.id);
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 16:58:53 +00:00
|
|
|
private onconnection(socket: any) {
|
|
|
|
let client = socket as Socket;
|
|
|
|
this.sockets[client.id] = client;
|
|
|
|
client.onconnect()
|
|
|
|
this.emit(ServerEvent.connect, socket);
|
|
|
|
this.emit(ServerEvent.connection, socket);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
}
|