1
0
Fork 0

refactor: split handle

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
MiaoWoo 2019-06-28 15:37:26 +08:00
parent d9e3cad8a1
commit cf89e80151
3 changed files with 35 additions and 34 deletions

View File

@ -1,6 +1,5 @@
import { inject, postConstruct } from '@cc-server/ioc';
import { Vaild, NotBlank, NotNull, controller, requestBody, httpGet, httpPost, requestParam } from '@cc-server/binding'
import { namespace, listener, interfaces, io, getSocketContext } from '@cc-server/ws'
import { DBClient } from '@cc-server/db'
import '@cc-server/db-mongo'
@ -57,35 +56,3 @@ export class Controller {
return this.client.updateById(id, model);
}
}
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'before connection');
next();
})
export class Namespace extends interfaces.Namespace {
private cache: { [key: string]: string } = {};
public async connection(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'connection');
return `Welcome to Websocket Chat Room Now: ${Date.now()} Your ID: ${socket.id}! \n`;
}
public async disconnect(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'disconnect');
}
@listener('message', (socket: io.Socket, packet: io.Packet, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'listener middleware', [...packet]);
next();
})
public async message(socket: io.Socket, data: any) {
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')
this.cache[socket.id] = '';
return result;
}
return data;
}
}

View File

@ -0,0 +1,33 @@
import { namespace, listener, interfaces, io } from '@cc-server/ws'
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'before connection');
next();
})
export class Namespace extends interfaces.Namespace {
private cache: { [key: string]: string } = {};
public async connection(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'connection');
return `Welcome to Websocket Chat Room Now: ${Date.now()} Your ID: ${socket.id}! \n`;
}
public async disconnect(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'disconnect');
}
@listener('message', (socket: io.Socket, packet: io.Packet, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'listener middleware', [...packet]);
next();
})
public async message(socket: io.Socket, data: any) {
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')
this.cache[socket.id] = '';
return result;
}
return data;
}
}

View File

@ -1,6 +1,7 @@
import { CcServerBoot, express } from './index'
import './function/handle';
import './function/http';
import './function/websocket';
let server = new CcServerBoot();