feat: add adapter and socket

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-03-23 00:58:53 +08:00
parent aa9169b043
commit c25a616dba
10 changed files with 429 additions and 172 deletions

View File

@@ -1,96 +1,66 @@
import { EventEmitter } from 'events'
import { SocketIOClient } from './client'
import { Client } from './client'
import { SocketIO } from './interfaces';
import { ServerEvent } from '../server';
import { Socket } from './socket';
import { Adapter } from './adapter';
import { Server } from './index'
export class Namespace implements SocketIO.Namespace {
private event: EventEmitter;
private allClients: { [key: string]: SocketIOClient };
private roomClients: { [key: string]: Set<string> };
private clientRooms: { [key: string]: Set<string> };
export class Namespace extends EventEmitter implements SocketIO.Namespace {
name: string;
server: SocketIO.Server;
server: Server;
sockets: { [id: string]: SocketIO.Socket; };
connected: { [id: string]: SocketIO.Socket; };
adapter: SocketIO.Adapter;
json: SocketIO.Namespace;
constructor(name: string) {
super();
this.name = name;
this.event = new EventEmitter();
this.allClients = {};
this.roomClients = {};
this.clientRooms = {};
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);
}
use(fn: (socket: SocketIO.Socket, fn: (err?: any) => void) => void): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
to(room: string): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
in(room: string): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
send(...args: any[]): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
write(...args: any[]): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
on(event: "connection", listener: (socket: SocketIO.Socket) => void): this;
on(event: "connect", listener: (socket: SocketIO.Socket) => void): this;
on(event: string, listener: Function): this;
on(event: any, listener: any) {
// TODO
return this;
}
to(room: string): SocketIO.Namespace {
// TODO
return this;
}
in(room: string): SocketIO.Namespace {
return this.to(room);
}
send(...args: any[]): SocketIO.Namespace {
// TODO
return this;
}
write(...args: any[]): SocketIO.Namespace {
return this.send(...args);
}
clients(fn: Function): SocketIO.Namespace {
throw new Error("Method not implemented.");
return fn(Object.values(this.sockets))
}
compress(compress: boolean): SocketIO.Namespace {
throw new Error("Method not implemented.");
}
addListener(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
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);
}
once(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
}
removeListener(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
}
off(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
}
removeAllListeners(event?: string | symbol): this {
throw new Error("Method not implemented.");
}
setMaxListeners(n: number): this {
throw new Error("Method not implemented.");
}
getMaxListeners(): number {
throw new Error("Method not implemented.");
}
listeners(event: string | symbol): Function[] {
throw new Error("Method not implemented.");
}
rawListeners(event: string | symbol): Function[] {
throw new Error("Method not implemented.");
}
emit(event: string | symbol, ...args: any[]): boolean {
throw new Error("Method not implemented.");
}
listenerCount(type: string | symbol): number {
throw new Error("Method not implemented.");
}
prependListener(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
}
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this {
throw new Error("Method not implemented.");
}
eventNames(): (string | symbol)[] {
throw new Error("Method not implemented.");
}
}