feat: support handshark & boardcast
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
26eceac330
commit
f527c225fe
@ -72,7 +72,7 @@ export class Adapter extends EventEmitter implements SocketIO.Adapter {
|
|||||||
var socket: Socket;
|
var socket: Socket;
|
||||||
|
|
||||||
packet.nsp = this.nsp.name;
|
packet.nsp = this.nsp.name;
|
||||||
// let encodedPackets = this.parser.encode(packet)
|
let encodedPackets = this.parser.encode(packet)
|
||||||
if (rooms.length) {
|
if (rooms.length) {
|
||||||
for (var i = 0; i < rooms.length; i++) {
|
for (var i = 0; i < rooms.length; i++) {
|
||||||
var room = self.rooms[rooms[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;
|
if (ids[id] || ~except.indexOf(id)) continue;
|
||||||
socket = self.nsp.connected[id];
|
socket = self.nsp.connected[id];
|
||||||
if (socket) {
|
if (socket) {
|
||||||
socket.packet(packet, packetOpts);
|
socket.packet(encodedPackets as any, packetOpts);
|
||||||
ids[id] = true;
|
ids[id] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ export class Adapter extends EventEmitter implements SocketIO.Adapter {
|
|||||||
if (self.sids.hasOwnProperty(id)) {
|
if (self.sids.hasOwnProperty(id)) {
|
||||||
if (~except.indexOf(id)) continue;
|
if (~except.indexOf(id)) continue;
|
||||||
socket = self.nsp.connected[id];
|
socket = self.nsp.connected[id];
|
||||||
if (socket) socket.packet(packet, packetOpts);
|
if (socket) socket.packet(encodedPackets as any, packetOpts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,8 @@ export class Client extends EventEmitter implements SocketIO.Client {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
packet(packet: Packet, opts?: any) {
|
packet(packet: Packet, opts: any = { preEncoded: false }) {
|
||||||
this.conn.send(parser.encode(packet))
|
this.conn.send(opts.preEncoded ? packet as unknown as string : parser.encode(packet))
|
||||||
}
|
}
|
||||||
onclose(reason?: string) {
|
onclose(reason?: string) {
|
||||||
// debug('client close with reason %s', reason);
|
// debug('client close with reason %s', reason);
|
||||||
|
@ -727,6 +727,16 @@ export declare namespace SocketIO {
|
|||||||
*/
|
*/
|
||||||
del(id: string, room: string, callback?: (err?: any) => void): void;
|
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
|
* Removes a socket from all the rooms that it's joined
|
||||||
* @param id The ID of the socket that we're removing
|
* @param id The ID of the socket that we're removing
|
||||||
|
@ -7,7 +7,7 @@ import { Socket } from './socket';
|
|||||||
import { Adapter } from './adapter';
|
import { Adapter } from './adapter';
|
||||||
import { Server } from './index'
|
import { Server } from './index'
|
||||||
import { Packet } from './packet';
|
import { Packet } from './packet';
|
||||||
import { SubPacketTypes } from './types';
|
import { PacketTypes, SubPacketTypes } from './types';
|
||||||
|
|
||||||
export class Namespace extends EventEmitter implements SocketIO.Namespace {
|
export class Namespace extends EventEmitter implements SocketIO.Namespace {
|
||||||
name: string;
|
name: string;
|
||||||
@ -78,7 +78,9 @@ export class Namespace extends EventEmitter implements SocketIO.Namespace {
|
|||||||
}
|
}
|
||||||
// set up packet object
|
// set up packet object
|
||||||
var packet = {
|
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
|
data: args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +103,11 @@ export class Parser {
|
|||||||
p.nsp = '/';
|
p.nsp = '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle namespace query
|
||||||
|
if (p.nsp.indexOf('?') !== -1) {
|
||||||
|
p.nsp = p.nsp.split('?')[0];
|
||||||
|
}
|
||||||
|
|
||||||
// look up id
|
// look up id
|
||||||
let next = str.charAt(i + 1);
|
let next = str.charAt(i + 1);
|
||||||
if ('' !== next && !isNaN(Number(next))) {
|
if ('' !== next && !isNaN(Number(next))) {
|
||||||
|
@ -5,6 +5,7 @@ import { Packet } from './packet';
|
|||||||
import { PacketTypes, SubPacketTypes } from './types';
|
import { PacketTypes, SubPacketTypes } from './types';
|
||||||
import { Client } from './client';
|
import { Client } from './client';
|
||||||
import { Namespace } from './namespace';
|
import { Namespace } from './namespace';
|
||||||
|
import * as querystring from 'querystring'
|
||||||
|
|
||||||
export class Socket extends EventEmitter implements SocketIO.Socket {
|
export class Socket extends EventEmitter implements SocketIO.Socket {
|
||||||
nsp: Namespace;
|
nsp: Namespace;
|
||||||
@ -45,7 +46,7 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
|
|||||||
this.acks = {};
|
this.acks = {};
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
this.disconnected = false;
|
this.disconnected = false;
|
||||||
// this.handshake = this.buildHandshake(query);
|
this.handshake = this.buildHandshake(query);
|
||||||
this.fns = [];
|
this.fns = [];
|
||||||
this.flags = {};
|
this.flags = {};
|
||||||
this._rooms = [];
|
this._rooms = [];
|
||||||
@ -97,6 +98,14 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
|
|||||||
fn && fn(null);
|
fn && fn(null);
|
||||||
return this;
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
leave(name: string, fn?: Function): SocketIO.Socket {
|
leave(name: string, fn?: Function): SocketIO.Socket {
|
||||||
@ -127,16 +136,21 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
|
|||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
buildHandshake(query): SocketIO.Handshake {
|
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 {
|
return {
|
||||||
headers: this.request.headers(),
|
headers: headers,
|
||||||
time: (new Date) + '',
|
time: (new Date) + '',
|
||||||
address: this.conn.remoteAddress,
|
address: this.conn.remoteAddress + '',
|
||||||
xdomain: !!this.request.headers.origin,
|
xdomain: !!headers['origin'],
|
||||||
secure: !!this.request.connection.encrypted,
|
secure: false,
|
||||||
issued: +(new Date),
|
issued: +(new Date),
|
||||||
url: this.request.url,
|
url: requestUri,
|
||||||
query: Object.assign(query, requestQuery)
|
query: Object.assign(query, querystring.parse(requestUri.indexOf('?') != -1 ? requestUri.split('?')[1] : ''))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit(event: string, ...args: any[]): boolean {
|
emit(event: string, ...args: any[]): boolean {
|
||||||
@ -183,10 +197,11 @@ export class Socket extends EventEmitter implements SocketIO.Socket {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
packet(packet: Packet, opts?: any) {
|
packet(packet: Packet, opts: any = { preEncoded: false }) {
|
||||||
packet.nsp = this.nsp.name;
|
if (!opts.preEncoded) {
|
||||||
opts = opts || {};
|
packet.nsp = this.nsp.name;
|
||||||
opts.compress = false !== opts.compress;
|
opts.compress = false !== opts.compress;
|
||||||
|
}
|
||||||
this.client.packet(packet, opts);
|
this.client.packet(packet, opts);
|
||||||
}
|
}
|
||||||
onconnect() {
|
onconnect() {
|
||||||
|
Loading…
Reference in New Issue
Block a user