feat: support handshark & boardcast

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-14 19:03:41 +08:00
parent e6aba870da
commit 805f7ad521
6 changed files with 51 additions and 19 deletions

View File

@ -72,7 +72,7 @@ export class Adapter extends EventEmitter implements SocketIO.Adapter {
var socket: Socket;
packet.nsp = this.nsp.name;
// let encodedPackets = this.parser.encode(packet)
let encodedPackets = this.parser.encode(packet)
if (rooms.length) {
for (var i = 0; i < rooms.length; i++) {
var room = self.rooms[rooms[i]];
@ -83,7 +83,7 @@ export class Adapter extends EventEmitter implements SocketIO.Adapter {
if (ids[id] || ~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) {
socket.packet(packet, packetOpts);
socket.packet(encodedPackets as any, packetOpts);
ids[id] = true;
}
}
@ -94,7 +94,7 @@ export class Adapter extends EventEmitter implements SocketIO.Adapter {
if (self.sids.hasOwnProperty(id)) {
if (~except.indexOf(id)) continue;
socket = self.nsp.connected[id];
if (socket) socket.packet(packet, packetOpts);
if (socket) socket.packet(encodedPackets as any, packetOpts);
}
}
}

View File

@ -62,8 +62,8 @@ export class Client extends EventEmitter implements SocketIO.Client {
}
});
}
packet(packet: Packet, opts?: any) {
this.conn.send(parser.encode(packet))
packet(packet: Packet, opts: any = { preEncoded: false }) {
this.conn.send(opts.preEncoded ? packet as unknown as string : parser.encode(packet))
}
onclose(reason?: string) {
// debug('client close with reason %s', reason);

View File

@ -727,6 +727,16 @@ export declare namespace SocketIO {
*/
del(id: string, room: string, callback?: (err?: any) => void): void;
/**
* Adds a socket to a list of room.
*
* @param {String} socket id
* @param {String} rooms
* @param {Function} callback
* @api public
*/
addAll(id: string, rooms: string | any[], fn: { (err?: any): void; bind?: any; });
/**
* Removes a socket from all the rooms that it's joined
* @param id The ID of the socket that we're removing

View File

@ -7,7 +7,7 @@ import { Socket } from './socket';
import { Adapter } from './adapter';
import { Server } from './index'
import { Packet } from './packet';
import { SubPacketTypes } from './types';
import { PacketTypes, SubPacketTypes } from './types';
export class Namespace extends EventEmitter implements SocketIO.Namespace {
name: string;
@ -78,7 +78,9 @@ export class Namespace extends EventEmitter implements SocketIO.Namespace {
}
// set up packet object
var packet = {
type: (this.flags.binary !== undefined ? this.flags.binary : this.hasBin(args)) ? SubPacketTypes.BINARY_EVENT : SubPacketTypes.EVENT,
type: PacketTypes.MESSAGE,
sub_type: (this.flags.binary !== undefined ? this.flags.binary : this.hasBin(args)) ? SubPacketTypes.BINARY_EVENT : SubPacketTypes.EVENT,
name: event,
data: args
}

View File

@ -103,6 +103,11 @@ export class Parser {
p.nsp = '/';
}
// handle namespace query
if (p.nsp.indexOf('?') !== -1) {
p.nsp = p.nsp.split('?')[0];
}
// look up id
let next = str.charAt(i + 1);
if ('' !== next && !isNaN(Number(next))) {

View File

@ -5,6 +5,7 @@ import { Packet } from './packet';
import { PacketTypes, SubPacketTypes } from './types';
import { Client } from './client';
import { Namespace } from './namespace';
import * as querystring from 'querystring'
export class Socket extends EventEmitter implements SocketIO.Socket {
nsp: Namespace;
@ -45,7 +46,7 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
this.acks = {};
this.connected = true;
this.disconnected = false;
// this.handshake = this.buildHandshake(query);
this.handshake = this.buildHandshake(query);
this.fns = [];
this.flags = {};
this._rooms = [];
@ -97,6 +98,14 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
fn && fn(null);
return this;
}
this.adapter.addAll(this.id, rooms, (err) => {
if (err) return fn && fn(err);
// debug('joined room %s', rooms);
(rooms as Array<string>).forEach((room) => {
this.rooms[room] = room;
});
fn && fn(null);
});
return this;
}
leave(name: string, fn?: Function): SocketIO.Socket {
@ -127,16 +136,21 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
// ==========================================
buildHandshake(query): SocketIO.Handshake {
let requestQuery = this.request.uri();
let requestUri = this.request.uri();
let headers = {};
let nativeHeaders = this.request.headers();
nativeHeaders.forEach(function (header) {
headers[header.getKey()] = header.getValue();
})
return {
headers: this.request.headers(),
headers: headers,
time: (new Date) + '',
address: this.conn.remoteAddress,
xdomain: !!this.request.headers.origin,
secure: !!this.request.connection.encrypted,
address: this.conn.remoteAddress + '',
xdomain: !!headers['origin'],
secure: false,
issued: +(new Date),
url: this.request.url,
query: Object.assign(query, requestQuery)
url: requestUri,
query: Object.assign(query, querystring.parse(requestUri.indexOf('?') != -1 ? requestUri.split('?')[1] : ''))
}
}
emit(event: string, ...args: any[]): boolean {
@ -183,10 +197,11 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
// @ts-ignore
return this;
}
packet(packet: Packet, opts?: any) {
packet.nsp = this.nsp.name;
opts = opts || {};
opts.compress = false !== opts.compress;
packet(packet: Packet, opts: any = { preEncoded: false }) {
if (!opts.preEncoded) {
packet.nsp = this.nsp.name;
opts.compress = false !== opts.compress;
}
this.client.packet(packet, opts);
}
onconnect() {