2020-03-21 07:47:42 +00:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
|
|
|
|
import { NettyWebSocketServer } from '../server'
|
|
|
|
import { ServerEvent } from '../server/constants';
|
|
|
|
|
|
|
|
import { Namespace } from './namespace';
|
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 { Parser } from './parser'
|
|
|
|
import { PacketTypes, SubPacketTypes } from './types';
|
|
|
|
import { Packet } from './packet';
|
|
|
|
import { Socket } from './socket';
|
|
|
|
import { Adapter } from './adapter';
|
2020-03-21 07:47:42 +00:00
|
|
|
|
2020-03-22 16:58:53 +00:00
|
|
|
class Server implements SocketIO.Server {
|
2020-03-21 07:47:42 +00:00
|
|
|
private nettyServer: NettyWebSocketServer;
|
2020-03-22 16:58:53 +00:00
|
|
|
private allClients: { [key: string]: Client };
|
2020-03-21 07:47:42 +00:00
|
|
|
|
|
|
|
engine: { ws: any; };
|
2020-03-22 16:58:53 +00:00
|
|
|
nsps: { [namespace: string]: Namespace; };
|
|
|
|
sockets: Namespace;
|
2020-03-21 07:47:42 +00:00
|
|
|
json: SocketIO.Server;
|
|
|
|
volatile: SocketIO.Server;
|
|
|
|
local: SocketIO.Server;
|
2020-03-22 16:58:53 +00:00
|
|
|
parser = new Parser();
|
|
|
|
_adapter: Adapter;
|
|
|
|
options: SocketIO.ServerOptions;
|
2020-03-21 07:47:42 +00:00
|
|
|
|
|
|
|
constructor(pipeline: any, options: SocketIO.ServerOptions) {
|
|
|
|
if (!pipeline) { throw new Error('Netty Pipeline can\'t be undefiend!') }
|
|
|
|
this.allClients = {};
|
2020-03-22 16:58:53 +00:00
|
|
|
this.nsps = {};
|
2020-03-23 10:33:12 +00:00
|
|
|
this.sockets = new Namespace('/', this);
|
2020-03-22 16:58:53 +00:00
|
|
|
this.nsps['/'] = this.sockets;
|
|
|
|
this.initNettyServer(pipeline, options);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkRequest(req: any, fn: (err: any, success: boolean) => void): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
serveClient(): boolean;
|
|
|
|
serveClient(v: boolean): SocketIO.Server;
|
|
|
|
serveClient(v?: any): boolean | SocketIO.Server {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
path(): string;
|
|
|
|
path(v: string): SocketIO.Server;
|
|
|
|
path(v?: any): string | SocketIO.Server {
|
2020-03-22 16:58:53 +00:00
|
|
|
if (!arguments.length) return this.options.path;
|
|
|
|
this.options.path = v.replace(/\/$/, '');
|
2020-03-21 07:47:42 +00:00
|
|
|
return this;
|
|
|
|
}
|
2020-03-22 16:58:53 +00:00
|
|
|
adapter(): Adapter;
|
2020-03-21 07:47:42 +00:00
|
|
|
adapter(v: any): SocketIO.Server;
|
2020-03-22 16:58:53 +00:00
|
|
|
adapter(v?: any): Adapter | SocketIO.Server {
|
|
|
|
if (!arguments.length) return this._adapter;
|
|
|
|
this._adapter = v;
|
|
|
|
for (var i in this.nsps) {
|
|
|
|
if (this.nsps.hasOwnProperty(i)) {
|
|
|
|
this.nsps[i].initAdapter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
origins(): string | string[];
|
|
|
|
origins(v: string | string[]): SocketIO.Server;
|
|
|
|
origins(fn: (origin: string, callback: (error: string, success: boolean) => void) => void): SocketIO.Server;
|
|
|
|
origins(fn?: any): string | string[] | SocketIO.Server {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
attach(srv: any, opts?: SocketIO.ServerOptions): SocketIO.Server;
|
|
|
|
attach(port: number, opts?: SocketIO.ServerOptions): SocketIO.Server;
|
|
|
|
attach(port: any, opts?: any): SocketIO.Server {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
listen(srv: any, opts?: SocketIO.ServerOptions): SocketIO.Server;
|
|
|
|
listen(port: number, opts?: SocketIO.ServerOptions): SocketIO.Server;
|
|
|
|
listen(port: any, opts?: any): SocketIO.Server {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
bind(srv: any): SocketIO.Server {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-03-24 05:27:11 +00:00
|
|
|
onconnection(socket: Client): SocketIO.Server {
|
2020-03-23 10:33:12 +00:00
|
|
|
this.allClients[socket.id] = socket;
|
2020-03-22 16:58:53 +00:00
|
|
|
socket.packet({
|
|
|
|
type: PacketTypes.OPEN,
|
|
|
|
data: {
|
|
|
|
sid: socket.id,
|
|
|
|
upgrades: [],
|
|
|
|
pingInterval: 25000,
|
|
|
|
pingTimeout: 5000
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.sockets.add(socket);
|
|
|
|
return this;
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
of(nsp: string): Namespace {
|
2020-03-22 16:58:53 +00:00
|
|
|
if (!this.nsps[nsp]) {
|
2020-03-23 10:33:12 +00:00
|
|
|
this.nsps[nsp] = new Namespace(nsp, this);
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
return this.nsps[nsp];
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
close(fn?: () => void): void {
|
2020-03-24 05:27:11 +00:00
|
|
|
for (let socket in this.sockets.sockets) {
|
|
|
|
this.sockets.sockets[socket].onclose()
|
|
|
|
}
|
|
|
|
this.nettyServer.close();
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
on(event: "connection", listener: (socket: SocketIO.Socket) => void): SocketIO.Namespace;
|
|
|
|
on(event: "connect", listener: (socket: SocketIO.Socket) => void): SocketIO.Namespace;
|
|
|
|
on(event: string, listener: Function): SocketIO.Namespace;
|
|
|
|
on(event: any, listener: any): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.on(event, listener);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
to(room: string): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.to(room);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
in(room: string): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.in(room);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
use(fn: (socket: SocketIO.Socket, fn: (err?: any) => void) => void): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.use(fn);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
emit(event: string, ...args: any[]): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return this.sockets.emit(event, ...args);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
send(...args: any[]): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.send(...args);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
write(...args: any[]): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.write(...args);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
clients(...args: any[]): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.clients(args[0]);
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
|
|
|
compress(...args: any[]): SocketIO.Namespace {
|
2020-03-24 05:27:11 +00:00
|
|
|
return this.sockets.compress(args[0])
|
2020-03-21 07:47:42 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
// ===============================
|
|
|
|
checkNamespace(name, query, fn) {
|
|
|
|
fn(false);
|
|
|
|
};
|
|
|
|
|
2020-03-22 16:58:53 +00:00
|
|
|
private initNettyServer(pipeline, options) {
|
|
|
|
this.nettyServer = new NettyWebSocketServer(pipeline, {
|
|
|
|
event: new EventEmitter(),
|
|
|
|
path: options.path
|
|
|
|
});
|
|
|
|
this.nettyServer.on(ServerEvent.connect, (nettyClient) => {
|
|
|
|
let client = new Client(this, nettyClient);
|
|
|
|
this.onconnection(client);
|
|
|
|
})
|
|
|
|
this.nettyServer.on(ServerEvent.message, (nettyClient, text) => {
|
|
|
|
this.processPacket(this.parser.decode(text), this.allClients[nettyClient.id]);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private processPacket(packet: Packet, client: Client) {
|
|
|
|
switch (packet.type) {
|
|
|
|
case PacketTypes.PING:
|
|
|
|
client.packet({
|
|
|
|
type: PacketTypes.PONG,
|
|
|
|
data: packet.data
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
case PacketTypes.UPGRADE:
|
|
|
|
break;
|
|
|
|
case PacketTypes.MESSAGE:
|
|
|
|
this.processSubPacket(packet, client);
|
|
|
|
break;
|
|
|
|
case PacketTypes.CLOSE:
|
2020-03-24 06:52:50 +00:00
|
|
|
client.onclose()
|
|
|
|
break;
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private processSubPacket(packet: Packet, client: Client) {
|
2020-03-23 10:33:12 +00:00
|
|
|
let namespace = this.nsps[packet.nsp]
|
|
|
|
if (!namespace) {
|
|
|
|
client.packet({
|
|
|
|
type: PacketTypes.MESSAGE,
|
|
|
|
sub_type: SubPacketTypes.ERROR,
|
2020-03-24 06:52:50 +00:00
|
|
|
data: 'not support dynamic namespace: ' + packet.nsp
|
2020-03-23 10:33:12 +00:00
|
|
|
});
|
2020-03-24 06:52:50 +00:00
|
|
|
client.disconnect();
|
2020-03-23 10:33:12 +00:00
|
|
|
return;
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
namespace.process(packet, client);
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export {
|
|
|
|
Server,
|
2020-03-23 10:33:12 +00:00
|
|
|
Socket,
|
2020-03-22 16:58:53 +00:00
|
|
|
Server as SocketIOServer,
|
|
|
|
Client as SocketIOClient
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|