diff --git a/packages/core/src/function/handle.ts b/packages/core/src/function/http.ts similarity index 52% rename from packages/core/src/function/handle.ts rename to packages/core/src/function/http.ts index 166d273..b1d4b64 100644 --- a/packages/core/src/function/handle.ts +++ b/packages/core/src/function/http.ts @@ -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; - } -} diff --git a/packages/core/src/function/websocket.ts b/packages/core/src/function/websocket.ts new file mode 100644 index 0000000..8a1b8ef --- /dev/null +++ b/packages/core/src/function/websocket.ts @@ -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; + } +} diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts index 328f251..027c918 100644 --- a/packages/core/src/server.ts +++ b/packages/core/src/server.ts @@ -1,6 +1,7 @@ import { CcServerBoot, express } from './index' -import './function/handle'; +import './function/http'; +import './function/websocket'; let server = new CcServerBoot();