Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/feiyun/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feiyun",
"repository": "https://github.com/hxg2050/feiyun.git",
"version": "0.2.15",
"version": "0.2.16",
"private": false,
"type": "module",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.11.19",
"feiyun": "^0.2.11",
"feiyun": "^0.2.16",
"rollup": "^4.12.0",
"rollup-plugin-ts": "^3.4.5",
"tslib": "^2.6.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@feiyun/server",
"repository": "https://github.com/hxg2050/feiyun.git",
"private": false,
"version": "0.0.6",
"version": "0.1.0",
"type": "module",
"main": "./src/index.ts",
"engines": {
Expand Down
43 changes: 43 additions & 0 deletions packages/server/src/BaseServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Socket } from "./socket";

export abstract class BaseServer<T = any> {
sockets = new Map<number, Socket>();
clients = new Map<number, T>();
uidSocketIds = new Map<number, number>()


handlerCallback?: ((client: Socket, data: string) => void) | undefined;

bindUid(uid: number, socket: Socket): void {
this.uidSocketIds.set(uid, socket.id)
}
unbindUid(uid: number): void {
this.uidSocketIds.delete(uid)
}

abstract send(socket: T, name: string | number, data: any): void;

sendTo(uid: number, name: string, data: any): void {
const tcpSocket = this.clients.get(uid);
if (!tcpSocket) {
return;
}
this.send(tcpSocket, name, data)
}
sendToUid(uid: number, name: string, data: any): void {
const socketId = this.uidSocketIds.get(uid)

if (!socketId) {
return;
}
this.sendTo(socketId, name, data)
}
reply(socketId: number, requestId: number, data: any): void {
const sws = this.clients.get(socketId);
if (!sws) {
return;
}
this.send(sws, requestId, data)
}

}
42 changes: 42 additions & 0 deletions packages/server/src/IServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Socket } from "./socket"

export interface IServer {

start(): void

handlerCallback?: (client: Socket, data: string) => void

/**
* 绑定uid
* @param uid
* @param socket
*/
bindUid(uid: number, socket: Socket): void
/**
* 解除用户绑定
* @param uid
*/
unbindUid(uid: number): void
/**
* 给指定id发送消息
* @param uid
* @param name
* @param data
*/
sendTo(uid: number, name: string, data: any): void
/**
* 给自定用户发送消息
* @param uid
* @param name
* @param data
*/
sendToUid(uid: number, name: string, data: any): void

/**
* 回复消息
* @param id
* @param requestId
* @param data
*/
reply(id: number, requestId: number, data: any): void;
}
64 changes: 9 additions & 55 deletions packages/server/src/feiyun.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Socket } from './socket'
import { compose, type Middleware } from './compose'
import { Server } from './server'
import { IServer } from './IServer'

export interface ApplicationConfig {
host: string
port: number
customServer?: (config: ApplicationConfig) => IServer
}

export class Request {
Expand Down Expand Up @@ -33,36 +35,16 @@ export class Feiyun {
port: 3000,
}

public server!: Server
public server!: IServer

constructor(config: Partial<ApplicationConfig> = {}) {
this.config = { ...this.config, ...config }
}

listen(port?: number) {
// const wss = new WebSocketServer(this.config);
// wss.on('connection', (ws, request) => {
// ws.on('open', () => {

// });
// ws.on('message', () => {

// });
// ws.on('error', () => {

// });
// ws.on('close', () => {

// });
// });
// wss.on('close', () => {

// });

// wss.on('error', () => {

// });
this.server = new Server(this.config)
listen() {
this.server = this.config.customServer ? this.config.customServer(this.config) : new Server({
port: this.config.port
})
this.server.start()
this.server.handlerCallback = (client, data) => {
const ctx = new Context()
Expand All @@ -87,42 +69,14 @@ export class Feiyun {
console.log('server listen:', `ws://${this.config.host}:${this.config.port}`)
}

// callback() {
// const ctx = new Context();
// return (client, data) => {

// }
// }

use(fn: FeiyunMiddleware) {
this.middleware.push(fn)
return this
}

async responseHandler(ctx: Context) {
if (ctx.response.data) {
this.server.reply(ctx.socket.socket, ctx.request.id, ctx.response.data)
this.server.reply(ctx.socket.id, ctx.request.id, ctx.response.data)
}
}
}

// const app = new Feiyun();
// app.use(async (ctx, next) => {
// const msg = JSON.parse(ctx.message);
// const [rid, route, req]: [number, string, any] = msg;

// const handler = this.handlers.get(route);

// ctx.response.body = await handler();

// if (!handler) {
// return;
// }

// const res = await handler(req, client);

// // 如果有返回值,那么直接回应
// if (res) {
// this.reply(client.socket, rid, res);
// }
// });
}
86 changes: 60 additions & 26 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Socket } from './socket'
import { createWebsocketServer } from './ws'
import { SWS, WebSocketData, createWebsocketServer } from './ws'
import { IWebsocketServer } from './IWebsocketServer'
import { ServerWebSocket } from 'bun'
import { IServer } from './IServer'
type Handler = (msg: any, client: Socket) => Promise<any> | any


Expand All @@ -12,27 +12,23 @@ interface ServerConfig {
/**
* 服务器
*/
export class Server {
export class Server implements IServer {
handlers = new Map<string, Handler>()

clientIndex: number = 0

wss?: IWebsocketServer
wss?: IWebsocketServer<WebSocketData>

clients: Map<number, Socket> = new Map()
clientsFromServerWebSocket: Map<ServerWebSocket, Socket> = new Map();
clientsFromUid: Map<string | number, Socket> = new Map()
clientsFromServerWebSocket: Map<SWS, Socket> = new Map();

clients: Map<number, SWS> = new Map()
clientsFromUid: Map<number, number> = new Map()

/**
* 配置
* @param config 配置
*/
constructor(private config: ServerConfig) {
// this.wss = new WebSocketServer(config);
// this.wss.on('connection', (socket, request) => {
// this.onConnection(socket, request);
// });
// console.log('ws://127.0.0.1:' + config.port);
}

/**
Expand All @@ -43,8 +39,9 @@ export class Server {
port: this.config.port
});
this.wss.open((ws) => {
const client = new Socket(++this.clientIndex, this, ws)
this.clients.set(this.clientIndex, client)
const client = new Socket(this)
ws.data.socketId = client.id;
this.clients.set(this.clientIndex, ws)
this.clientsFromServerWebSocket.set(ws, client);
});
this.wss.message((ws, data) => {
Expand Down Expand Up @@ -77,39 +74,76 @@ export class Server {
this.handlers.set(path, handler)
}

unbindUid(uid: number): void {
this.clientsFromUid.delete(uid)
}

/**
* 发送消息到客户端
* @param sokcet
* @param name
* @param data
*/
send(sokcet: ServerWebSocket, name: string | number, data: any) {
send(sokcet: SWS, name: string | number, data: any) {
sokcet.send(JSON.stringify([1, name, data]))
}


sendTo(id: number, name: string, data: any): void {
const sws = this.clients.get(id);
if (!sws) {
return;
}
this.send(sws, name, data)
}

/**
* 发送消息到客户端(uid)
*/
sendToUid(uid: string | number, name: string, data: any) {
const socket = this.clientsFromUid.get(uid)
sendToUid(uid: number, name: string, data: any) {
const socketId = this.clientsFromUid.get(uid)

if (!socket) {
return false
if (!socketId) {
return;
}
this.sendTo(socketId, name, data)
}

socket.send(name, data)
/**
* 绑定uid到连接上
* @param uid
* @param socket
*/
bindUid(uid: number, socket: Socket) {
this.clientsFromUid.set(uid, socket.id);
}

return true
/**
* 获取用户连接
* @param uid
* @returns
*/
getByUid(uid: number) {
return this.clientsFromUid.get(uid);
}

/**
* 获取在线状态
*/
isOnline(uid: number) {
const socket = this.clients.get(uid);
return socket?.readyState === 1
}

/**
* 给客户端回复消息
* @param socket
* @param id
* @param data
*/
reply(socket: ServerWebSocket, id: number, data: any) {
this.send(socket, id, data)
reply(socketId: number, requestId: number, data: any) {
const sws = this.clients.get(socketId);
if (!sws) {
return;
}
this.send(sws, requestId, data)
}

isDebug = false
Expand Down
Loading
Loading