refactor: split handle
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
d9e3cad8a1
commit
cf89e80151
@ -1,6 +1,5 @@
|
|||||||
import { inject, postConstruct } from '@cc-server/ioc';
|
import { inject, postConstruct } from '@cc-server/ioc';
|
||||||
import { Vaild, NotBlank, NotNull, controller, requestBody, httpGet, httpPost, requestParam } from '@cc-server/binding'
|
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 { DBClient } from '@cc-server/db'
|
||||||
import '@cc-server/db-mongo'
|
import '@cc-server/db-mongo'
|
||||||
|
|
||||||
@ -57,35 +56,3 @@ export class Controller {
|
|||||||
return this.client.updateById(id, model);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
33
packages/core/src/function/websocket.ts
Normal file
33
packages/core/src/function/websocket.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import { CcServerBoot, express } from './index'
|
import { CcServerBoot, express } from './index'
|
||||||
|
|
||||||
import './function/handle';
|
import './function/http';
|
||||||
|
import './function/websocket';
|
||||||
|
|
||||||
let server = new CcServerBoot();
|
let server = new CcServerBoot();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user