2020-03-22 16:58:53 +00:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
|
|
|
|
import { SocketIO } from "./interfaces";
|
|
|
|
import { Packet } from './packet';
|
|
|
|
import { PacketTypes, SubPacketTypes } from './types';
|
2020-03-23 10:33:12 +00:00
|
|
|
import { Client } from './client';
|
|
|
|
import { Namespace } from './namespace';
|
2020-03-22 16:58:53 +00:00
|
|
|
|
|
|
|
export class Socket extends EventEmitter implements SocketIO.Socket {
|
2020-03-23 10:33:12 +00:00
|
|
|
nsp: Namespace;
|
2020-03-22 16:58:53 +00:00
|
|
|
server: SocketIO.Server;
|
|
|
|
adapter: SocketIO.Adapter;
|
|
|
|
id: string;
|
|
|
|
request: any;
|
2020-03-23 10:33:12 +00:00
|
|
|
client: Client;
|
2020-03-22 16:58:53 +00:00
|
|
|
conn: SocketIO.EngineSocket;
|
|
|
|
rooms: { [id: string]: string; };
|
2020-03-23 10:33:12 +00:00
|
|
|
acks: { [id: string]: Function; };
|
2020-03-22 16:58:53 +00:00
|
|
|
connected: boolean;
|
|
|
|
disconnected: boolean;
|
|
|
|
handshake: SocketIO.Handshake;
|
|
|
|
fns: any[];
|
2020-03-24 05:27:11 +00:00
|
|
|
flags: { [key: string]: boolean };
|
2020-03-22 16:58:53 +00:00
|
|
|
_rooms: string[];
|
|
|
|
|
2020-03-24 05:27:11 +00:00
|
|
|
private events = [
|
|
|
|
'error',
|
|
|
|
'connect',
|
|
|
|
'disconnect',
|
|
|
|
'disconnecting',
|
|
|
|
'newListener',
|
|
|
|
'removeListener'
|
|
|
|
]
|
|
|
|
|
2020-03-23 10:33:12 +00:00
|
|
|
constructor(nsp: Namespace, client: Client, query = {}) {
|
2020-03-22 16:58:53 +00:00
|
|
|
super();
|
|
|
|
this.nsp = nsp;
|
|
|
|
this.server = nsp.server;
|
|
|
|
this.adapter = this.nsp.adapter;
|
|
|
|
this.id = nsp.name !== '/' ? nsp.name + '#' + client.id : client.id;
|
|
|
|
this.client = client;
|
2020-03-23 10:33:12 +00:00
|
|
|
this.request = client.request;
|
2020-03-22 16:58:53 +00:00
|
|
|
this.conn = client.conn;
|
|
|
|
this.rooms = {};
|
2020-03-23 10:33:12 +00:00
|
|
|
this.acks = {};
|
2020-03-22 16:58:53 +00:00
|
|
|
this.connected = true;
|
|
|
|
this.disconnected = false;
|
|
|
|
// this.handshake = this.buildHandshake(query);
|
|
|
|
this.fns = [];
|
2020-03-24 05:27:11 +00:00
|
|
|
this.flags = {};
|
2020-03-22 16:58:53 +00:00
|
|
|
this._rooms = [];
|
2020-03-24 05:27:11 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
|
2020-03-24 05:27:11 +00:00
|
|
|
get json() {
|
|
|
|
this.flags.json = true;
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
get volatile() {
|
|
|
|
this.flags.volatile = true;
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
get broadcast() {
|
|
|
|
this.flags.broadcast = true;
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
get local() {
|
|
|
|
this.flags.local = true;
|
|
|
|
return this
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to(room: string): SocketIO.Socket {
|
|
|
|
if (!~this._rooms.indexOf(room)) this._rooms.push(room);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
in(room: string): SocketIO.Socket {
|
|
|
|
return this.to(room);
|
|
|
|
}
|
|
|
|
use(fn: (packet: SocketIO.Packet, next: (err?: any) => void) => void): SocketIO.Socket {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
send(...args: any[]): SocketIO.Socket {
|
|
|
|
this.emit("message", ...args)
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
write(...args: any[]): SocketIO.Socket {
|
|
|
|
return this.send(...args);
|
|
|
|
}
|
|
|
|
join(rooms: string | string[], fn?: (err?: any) => void): SocketIO.Socket {
|
|
|
|
if (!Array.isArray(rooms)) {
|
|
|
|
rooms = [rooms];
|
|
|
|
}
|
|
|
|
rooms = rooms.filter((room) => {
|
|
|
|
return !this.rooms.hasOwnProperty(room);
|
|
|
|
});
|
|
|
|
if (!rooms.length) {
|
|
|
|
fn && fn(null);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
leave(name: string, fn?: Function): SocketIO.Socket {
|
|
|
|
delete this.rooms[name];
|
|
|
|
fn && fn(null)
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
leaveAll(): void {
|
|
|
|
this.adapter.delAll(this.id);
|
|
|
|
this.rooms = {};
|
|
|
|
}
|
|
|
|
disconnect(close?: boolean): SocketIO.Socket {
|
2020-03-24 06:52:50 +00:00
|
|
|
if (!this.connected) return this;
|
|
|
|
if (close) {
|
|
|
|
this.client.disconnect();
|
|
|
|
} else {
|
|
|
|
this.packet({ type: PacketTypes.MESSAGE, sub_type: SubPacketTypes.DISCONNECT });
|
|
|
|
this.onclose('server namespace disconnect');
|
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
return this;
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
compress(compress: boolean): SocketIO.Socket {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
error(err: any): void {
|
2020-03-24 05:27:11 +00:00
|
|
|
this.packet({ type: PacketTypes.MESSAGE, sub_type: SubPacketTypes.ERROR, data: err });
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 10:33:12 +00:00
|
|
|
// ==========================================
|
|
|
|
buildHandshake(query): SocketIO.Handshake {
|
|
|
|
let requestQuery = this.request.uri();
|
|
|
|
return {
|
|
|
|
headers: this.request.headers(),
|
|
|
|
time: (new Date) + '',
|
|
|
|
address: this.conn.remoteAddress,
|
|
|
|
xdomain: !!this.request.headers.origin,
|
|
|
|
secure: !!this.request.connection.encrypted,
|
|
|
|
issued: +(new Date),
|
|
|
|
url: this.request.url,
|
|
|
|
query: Object.assign(query, requestQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit(event: string, ...args: any[]): boolean {
|
2020-03-24 05:27:11 +00:00
|
|
|
if (~this.events.indexOf(event)) {
|
|
|
|
super.emit(event, ...args);
|
|
|
|
// @ts-ignore
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
let packet: Packet = {
|
2020-03-23 10:33:12 +00:00
|
|
|
type: PacketTypes.MESSAGE,
|
2020-03-24 05:27:11 +00:00
|
|
|
sub_type: (this.flags.binary !== undefined ? this.flags.binary : this.hasBin(args)) ? SubPacketTypes.BINARY_EVENT : SubPacketTypes.EVENT,
|
2020-03-23 10:33:12 +00:00
|
|
|
name: event,
|
2020-03-24 06:52:50 +00:00
|
|
|
data: args
|
2020-03-24 05:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// access last argument to see if it's an ACK callback
|
|
|
|
if (typeof args[args.length - 1] === 'function') {
|
|
|
|
if (this._rooms.length || this.flags.broadcast) {
|
|
|
|
throw new Error('Callbacks are not supported when broadcasting');
|
|
|
|
}
|
|
|
|
// debug('emitting packet with ack id %d', this.nsp.ids);
|
|
|
|
this.acks[this.nsp.ids] = args.pop();
|
|
|
|
packet.id = this.nsp.ids++;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rooms = this._rooms.slice(0);
|
|
|
|
let flags = Object.assign({}, this.flags);
|
|
|
|
|
|
|
|
// reset flags
|
|
|
|
this._rooms = [];
|
|
|
|
this.flags = {};
|
|
|
|
|
|
|
|
if (rooms.length || flags.broadcast) {
|
|
|
|
this.adapter.broadcast(packet, {
|
|
|
|
except: [this.id],
|
|
|
|
rooms: rooms,
|
|
|
|
flags: flags
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// dispatch packet
|
|
|
|
this.packet(packet, flags);
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
return this;
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|
2020-03-24 05:27:11 +00:00
|
|
|
packet(packet: Packet, opts?: any) {
|
2020-03-23 10:33:12 +00:00
|
|
|
packet.nsp = this.nsp.name;
|
2020-03-24 05:27:11 +00:00
|
|
|
opts = opts || {};
|
|
|
|
opts.compress = false !== opts.compress;
|
|
|
|
this.client.packet(packet, opts);
|
2020-03-22 16:58:53 +00:00
|
|
|
}
|
|
|
|
onconnect() {
|
|
|
|
this.nsp.connected[this.id] = this;
|
2020-03-23 10:33:12 +00:00
|
|
|
this.client.sockets[this.id] = this;
|
2020-03-22 16:58:53 +00:00
|
|
|
this.join(this.id);
|
2020-03-24 05:27:11 +00:00
|
|
|
// let skip = this.nsp.name === '/' && this.nsp.fns.length === 0;
|
2020-03-22 16:58:53 +00:00
|
|
|
// if (skip) {
|
|
|
|
// debug('packet already sent in initial handshake');
|
|
|
|
// } else {
|
|
|
|
this.packet({
|
|
|
|
type: PacketTypes.MESSAGE,
|
|
|
|
sub_type: SubPacketTypes.CONNECT
|
|
|
|
});
|
|
|
|
// }
|
|
|
|
}
|
2020-03-24 05:27:11 +00:00
|
|
|
onclose(reason?: string) {
|
2020-03-23 10:33:12 +00:00
|
|
|
if (!this.connected) return this;
|
|
|
|
// debug('closing socket - reason %s', reason);
|
|
|
|
this.emit('disconnecting', reason);
|
|
|
|
this.leaveAll();
|
|
|
|
this.nsp.remove(this);
|
|
|
|
this.client.remove(this);
|
|
|
|
this.connected = false;
|
|
|
|
this.disconnected = true;
|
|
|
|
delete this.nsp.connected[this.id];
|
|
|
|
this.emit('disconnect', reason);
|
2020-03-24 05:27:11 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
onpacket(packet: Packet) {
|
|
|
|
switch (packet.sub_type) {
|
2020-03-24 06:52:50 +00:00
|
|
|
// 2
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.EVENT:
|
|
|
|
this.onevent(packet);
|
|
|
|
break;
|
2020-03-24 06:52:50 +00:00
|
|
|
// 5
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.BINARY_EVENT:
|
|
|
|
this.onevent(packet);
|
|
|
|
break;
|
2020-03-24 06:52:50 +00:00
|
|
|
// 3
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.ACK:
|
|
|
|
this.onack(packet);
|
|
|
|
break;
|
2020-03-24 06:52:50 +00:00
|
|
|
// 6
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.BINARY_ACK:
|
|
|
|
this.onack(packet);
|
|
|
|
break;
|
2020-03-24 06:52:50 +00:00
|
|
|
// 1
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.DISCONNECT:
|
|
|
|
this.ondisconnect();
|
|
|
|
break;
|
2020-03-24 06:52:50 +00:00
|
|
|
// 4
|
2020-03-23 10:33:12 +00:00
|
|
|
case SubPacketTypes.ERROR:
|
|
|
|
this.onerror(new Error(packet.data));
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 05:27:11 +00:00
|
|
|
onerror(err: Error) {
|
|
|
|
if (this.listeners('error').length) {
|
|
|
|
this.emit('error', err);
|
|
|
|
} else {
|
|
|
|
console.error('Missing error handler on `socket`.');
|
|
|
|
console.error(err.stack);
|
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|
|
|
|
ondisconnect() {
|
2020-03-24 05:27:11 +00:00
|
|
|
this.onclose('client namespace disconnect')
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|
|
|
|
onevent(packet: Packet) {
|
|
|
|
if (null != packet.id) {
|
2020-03-24 06:52:50 +00:00
|
|
|
// debug('attaching ack callback to event');
|
|
|
|
this.dispatch(packet, this.ack(packet.id));
|
|
|
|
} else {
|
|
|
|
this.dispatch(packet);
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|
2020-03-24 05:27:11 +00:00
|
|
|
}
|
2020-03-23 10:33:12 +00:00
|
|
|
ack(id: number) {
|
2020-03-24 05:27:11 +00:00
|
|
|
let sent = false;
|
2020-03-23 10:33:12 +00:00
|
|
|
return (...args: any[]) => {
|
|
|
|
if (sent) return;
|
|
|
|
this.packet({
|
|
|
|
id: id,
|
|
|
|
type: PacketTypes.MESSAGE,
|
|
|
|
sub_type: this.hasBin(args) ? SubPacketTypes.BINARY_ACK : SubPacketTypes.ACK,
|
|
|
|
data: args
|
|
|
|
});
|
|
|
|
sent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onack(packet: Packet) {
|
2020-03-24 05:27:11 +00:00
|
|
|
let ack = this.acks[packet.id];
|
2020-03-23 10:33:12 +00:00
|
|
|
if ('function' == typeof ack) {
|
|
|
|
// debug('calling ack %s with %j', packet.id, packet.data);
|
|
|
|
ack.apply(this, packet.data);
|
|
|
|
delete this.acks[packet.id];
|
|
|
|
} else {
|
|
|
|
// debug('bad ack %s', packet.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dispatch(packet: Packet, ack?: Function) {
|
|
|
|
if (ack) { this.acks[packet.id] = ack; }
|
2020-03-24 06:52:50 +00:00
|
|
|
super.emit(packet.name, ...packet.data, ack)
|
2020-03-23 10:33:12 +00:00
|
|
|
}
|
|
|
|
private hasBin(obj: any) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|