feat: add defer function

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-06-30 10:58:15 +08:00
parent c523b16fa7
commit 88d779c630
3 changed files with 36 additions and 20 deletions

View File

@ -1,6 +1,6 @@
import { lazyInjectNamed } from '@cc-server/ioc' import { lazyInjectNamed } from '@cc-server/ioc'
import { controller, httpPost, requestBody } from '@cc-server/binding'; 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) => { @namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'before connection'); 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) { public async connection(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'connection'); 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`; 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) console.log(this.nsp.name, socket.id, 'message', data)
this.cache[socket.id] = (this.cache[socket.id] || '') + data; this.cache[socket.id] = (this.cache[socket.id] || '') + data;
if (data == '\r' && this.cache[socket.id] !== "") { 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] = ''; this.cache[socket.id] = '';
return result; return result;
} }

View File

@ -2,7 +2,7 @@ import "reflect-metadata";
import * as io from 'socket.io' import * as io from 'socket.io'
import { Container } from 'inversify' import { Container } from 'inversify'
import { TYPE } from './constants' import { TYPE } from './constants'
import { interfaces, BroadcastMessage } from './interfaces' import { interfaces, Message, BroadcastMessage } from './interfaces'
import { getNamespaces, getNamespaceMetadata, getNamespaceListenerMetadata } from './utils' import { getNamespaces, getNamespaceMetadata, getNamespaceListenerMetadata } from './utils'
export function buildWebSocket(container: Container, server: io.Server) { export function buildWebSocket(container: Container, server: io.Server) {
@ -66,9 +66,12 @@ async function applyEvent(namespaceInstance: interfaces.Namespace, socket: io.So
socket.send(result); socket.send(result);
} }
} }
socket.on('disconnect', async () => {
await namespaceInstance.defers.forEach(defer => defer(socket));
if (namespaceInstance.disconnect) { if (namespaceInstance.disconnect) {
socket.on('disconnect', async () => await namespaceInstance.disconnect(socket)); await namespaceInstance.disconnect(socket);
} }
});
} }
function applyListeners(namespaceEventMetadata: interfaces.ListenerMetadata[], socket: io.Socket, namespaceInstance: interfaces.Namespace) { 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 => { socket.on(event.name, async data => {
let result = await namespaceInstance[event.key](socket, data); let result = await namespaceInstance[event.key](socket, data);
if (result != undefined) { if (result != undefined) {
if (result instanceof Message) {
if (result instanceof BroadcastMessage) { if (result instanceof BroadcastMessage) {
socket.broadcast.emit(event.name, result.message); socket.broadcast.emit(result.event || event.name, result.message);
} else {
socket.emit(result.event || event.name, result.message);
} }
else { } else {
socket.emit(event.name, result); socket.emit(event.name, result);
} }
} }

View File

@ -2,10 +2,9 @@ import * as io from 'socket.io'
import { injectable } from 'inversify'; import { injectable } from 'inversify';
export class Message { export class Message {
constructor(public message: any) { } constructor(public message: any, public event?: string) { }
} }
export class BroadcastMessage { export class BroadcastMessage extends Message {
constructor(public message: any) { }
} }
export namespace interfaces { export namespace interfaces {
@ -15,6 +14,10 @@ export namespace interfaces {
* @see io.Namespace * @see io.Namespace
*/ */
public nsp?: io.Namespace; public nsp?: io.Namespace;
/**
* Defer Functions Array
*/
public defers?: ((socket: io.Socket) => void)[] = [];
/** /**
* The event fired when we get a new connection * The event fired when we get a new connection
* @param socket socket * @param socket socket
@ -27,22 +30,28 @@ export namespace interfaces {
*/ */
public disconnect?(socket: io.Socket): void; public disconnect?(socket: io.Socket): void;
/** /**
* broadcast message on this namespace * add disconnect defer function
*/ */
public broadcast(message: any): BroadcastMessage { protected defer?(fn: (socket: io.Socket) => void) {
return new BroadcastMessage(message); this.defers.push(fn);
} }
/** /**
* Event Listener * Event Listener
* @param data event data * @param data event data
* @return return data will send use socket.emit(key, 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 { export interface Middleware {
(socket: io.Socket, next: (err?: any) => void): void; (socket: io.Socket, next: (err?: any) => void): void;
} }
/**
* Listener Middleware
*/
export interface ListenerMiddleware { export interface ListenerMiddleware {
(socket: io.Socket, packet: io.Packet, next: (err?: any) => void): void; (socket: io.Socket, packet: io.Packet, next: (err?: any) => void): void;
} }