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-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-21 07:47:42 +00:00
|
|
|
sockets: { [id: string]: SocketIO.Socket; };
|
|
|
|
connected: { [id: string]: SocketIO.Socket; };
|
|
|
|
adapter: SocketIO.Adapter;
|
|
|
|
json: SocketIO.Namespace;
|
|
|
|
|
|
|
|
constructor(name: string) {
|
2020-03-22 16:58:53 +00:00
|
|
|
super();
|
2020-03-21 07:47:42 +00:00
|
|
|
this.name = name;
|
2020-03-22 16:58:53 +00:00
|
|
|
this.sockets = {};
|
|
|
|
this.connected = {};
|
|
|
|
this.adapter = new Adapter(this);
|
|
|
|
}
|
|
|
|
initAdapter() {
|
|
|
|
let adp = this.server.adapter()
|
|
|
|
this.adapter = new adp()
|
|
|
|
}
|
|
|
|
add(client: Client) {
|
|
|
|
let nameClient = new Socket(this, client, {});
|
|
|
|
this.sockets[client.id] = nameClient;
|
|
|
|
client.nsps[this.name] = nameClient;
|
|
|
|
this.onconnection(nameClient);
|
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-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
|
|
|
}
|
|
|
|
}
|