feat: add defer function
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
c523b16fa7
commit
88d779c630
@ -1,6 +1,6 @@
|
||||
import { lazyInjectNamed } from '@cc-server/ioc'
|
||||
import { controller, httpPost, requestBody } from '@cc-server/binding';
|
||||
import { namespace, listener, interfaces, io, TYPE } from '@cc-server/ws'
|
||||
import { namespace, listener, interfaces, BroadcastMessage, io, TYPE } from '@cc-server/ws'
|
||||
|
||||
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
|
||||
console.log(socket.nsp.name, socket.id, 'before connection');
|
||||
@ -11,6 +11,7 @@ export class Namespace extends interfaces.Namespace {
|
||||
|
||||
public async connection(socket: io.Socket) {
|
||||
console.log(this.nsp.name, socket.id, 'connection');
|
||||
this.defer(socket => console.log(this.nsp.name, socket.id, 'defer', this))
|
||||
return `Welcome to Websocket Chat Room Now: ${Date.now()} Your ID: ${socket.id}! \n`;
|
||||
}
|
||||
|
||||
@ -26,7 +27,7 @@ export class Namespace extends interfaces.Namespace {
|
||||
console.log(this.nsp.name, socket.id, 'message', data)
|
||||
this.cache[socket.id] = (this.cache[socket.id] || '') + data;
|
||||
if (data == '\r' && this.cache[socket.id] !== "") {
|
||||
let result = this.broadcast(this.cache[socket.id] + '\n')
|
||||
let result = new BroadcastMessage(this.cache[socket.id] + '\n')
|
||||
this.cache[socket.id] = '';
|
||||
return result;
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import "reflect-metadata";
|
||||
import * as io from 'socket.io'
|
||||
import { Container } from 'inversify'
|
||||
import { TYPE } from './constants'
|
||||
import { interfaces, BroadcastMessage } from './interfaces'
|
||||
import { interfaces, Message, BroadcastMessage } from './interfaces'
|
||||
import { getNamespaces, getNamespaceMetadata, getNamespaceListenerMetadata } from './utils'
|
||||
|
||||
export function buildWebSocket(container: Container, server: io.Server) {
|
||||
@ -66,9 +66,12 @@ async function applyEvent(namespaceInstance: interfaces.Namespace, socket: io.So
|
||||
socket.send(result);
|
||||
}
|
||||
}
|
||||
if (namespaceInstance.disconnect) {
|
||||
socket.on('disconnect', async () => await namespaceInstance.disconnect(socket));
|
||||
}
|
||||
socket.on('disconnect', async () => {
|
||||
await namespaceInstance.defers.forEach(defer => defer(socket));
|
||||
if (namespaceInstance.disconnect) {
|
||||
await namespaceInstance.disconnect(socket);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function applyListeners(namespaceEventMetadata: interfaces.ListenerMetadata[], socket: io.Socket, namespaceInstance: interfaces.Namespace) {
|
||||
@ -76,10 +79,13 @@ function applyListeners(namespaceEventMetadata: interfaces.ListenerMetadata[], s
|
||||
socket.on(event.name, async data => {
|
||||
let result = await namespaceInstance[event.key](socket, data);
|
||||
if (result != undefined) {
|
||||
if (result instanceof BroadcastMessage) {
|
||||
socket.broadcast.emit(event.name, result.message);
|
||||
}
|
||||
else {
|
||||
if (result instanceof Message) {
|
||||
if (result instanceof BroadcastMessage) {
|
||||
socket.broadcast.emit(result.event || event.name, result.message);
|
||||
} else {
|
||||
socket.emit(result.event || event.name, result.message);
|
||||
}
|
||||
} else {
|
||||
socket.emit(event.name, result);
|
||||
}
|
||||
}
|
||||
|
@ -2,19 +2,22 @@ import * as io from 'socket.io'
|
||||
import { injectable } from 'inversify';
|
||||
|
||||
export class Message {
|
||||
constructor(public message: any) { }
|
||||
constructor(public message: any, public event?: string) { }
|
||||
}
|
||||
export class BroadcastMessage {
|
||||
constructor(public message: any) { }
|
||||
export class BroadcastMessage extends Message {
|
||||
}
|
||||
|
||||
export namespace interfaces {
|
||||
@injectable()
|
||||
export class Namespace {
|
||||
/**
|
||||
* @see io.Namespace
|
||||
*/
|
||||
* @see io.Namespace
|
||||
*/
|
||||
public nsp?: io.Namespace;
|
||||
/**
|
||||
* Defer Functions Array
|
||||
*/
|
||||
public defers?: ((socket: io.Socket) => void)[] = [];
|
||||
/**
|
||||
* The event fired when we get a new connection
|
||||
* @param socket socket
|
||||
@ -27,22 +30,28 @@ export namespace interfaces {
|
||||
*/
|
||||
public disconnect?(socket: io.Socket): void;
|
||||
/**
|
||||
* broadcast message on this namespace
|
||||
* add disconnect defer function
|
||||
*/
|
||||
public broadcast(message: any): BroadcastMessage {
|
||||
return new BroadcastMessage(message);
|
||||
protected defer?(fn: (socket: io.Socket) => void) {
|
||||
this.defers.push(fn);
|
||||
}
|
||||
/**
|
||||
* Event Listener
|
||||
* @param data event data
|
||||
* @return return data will send use socket.emit(key, data)
|
||||
*/
|
||||
[key: string]: ((data: any, socket: io.Socket) => any) | any;
|
||||
[key: string]: ((socket: io.Socket, data: any) => any) | any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Namespace Middleware
|
||||
*/
|
||||
export interface Middleware {
|
||||
(socket: io.Socket, next: (err?: any) => void): void;
|
||||
}
|
||||
/**
|
||||
* Listener Middleware
|
||||
*/
|
||||
export interface ListenerMiddleware {
|
||||
(socket: io.Socket, packet: io.Packet, next: (err?: any) => void): void;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user