Skip to content

Commit

Permalink
chore: core 'ws' change to 'bun.serve'
Browse files Browse the repository at this point in the history
  • Loading branch information
hxg2050 committed Feb 24, 2024
1 parent 697a8c6 commit a8610f8
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 292 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feiyun",
"private": false,
"version": "0.1.3",
"version": "0.2.0",
"repository": "https://github.com/hxg2050/feiyun.git",
"license": "MIT",
"main": "./dist/index.cjs",
Expand All @@ -25,6 +25,7 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.3",
"@types/bun": "latest",
"@types/jest": "^29.5.4",
"@types/ws": "^8.5.5",
"jest": "^29.7.0",
Expand Down
36 changes: 0 additions & 36 deletions src/server/GameSocket.ts

This file was deleted.

175 changes: 0 additions & 175 deletions src/server/GameSocketServer.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/server/IWebsocketServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ServerWebSocket } from "bun";

export interface IWebsocketServer<T = undefined> {
open(handler: (ws: ServerWebSocket<T>) => void): void;
message(handler: (ws: ServerWebSocket<T>, message: string | Buffer) => void): void;
close(handler: (ws: ServerWebSocket<T>) => void): void;
}
3 changes: 3 additions & 0 deletions src/server/isBun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isBun = () => {
return typeof Bun !== 'undefined';
}
101 changes: 23 additions & 78 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IncomingMessage } from 'node:http'
import type WebSocket from 'ws'
import { WebSocketServer } from 'ws'
import { Socket } from './socket'

import { createWebsocketServer } from './ws'
import { IWebsocketServer } from './IWebSocketServer'
import { ServerWebSocket } from 'bun'
type Handler = (msg: any, client: Socket) => Promise<any> | any


interface ServerConfig {
port: number
timeout?: number
Expand All @@ -17,9 +17,10 @@ export class Server {

clientIndex: number = 0

wss?: WebSocketServer
wss?: IWebsocketServer

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

/**
Expand All @@ -38,79 +39,28 @@ export class Server {
* 开始
*/
start() {
this.wss = new WebSocketServer(this.config)
this.wss.on('connection', (socket, request) => {
this.onConnection(socket, request)
})
this.wss = createWebsocketServer({
port: this.config.port
});
this.wss.open((ws) => {
const client = new Socket(++this.clientIndex, this, ws)
this.clients.set(this.clientIndex, client)
this.clientsFromServerWebSocket.set(ws, client);
});
this.wss.message((ws, data) => {
const str = data.toString()
this.onMessage(this.clientsFromServerWebSocket.get(ws)!, str)
});
this.wss.close((ws) => {
this.clientsFromServerWebSocket.delete(ws);
});
// console.log('ws://127.0.0.1:' + this.config.port);
}

/**
* 当有新用户连接上来时
* 处理心跳
* @param socket
* @param request
*/
onConnection(socket: WebSocket, request: IncomingMessage) {
const client = new Socket(++this.clientIndex, this, socket)
this.clients.set(this.clientIndex, client)
let timeout: NodeJS.Timeout
const ping = () => {
if (!this.config.timeout || this.config.timeout < 0) {
return
}
console.log('刷新心跳')
clearTimeout(timeout)
timeout = setTimeout(close, this.config.timeout)
}

const close = () => {
console.log('超时,断开链接')
socket.close()
}
this.config.timeout && ping()

socket.on('message', (data) => {
const str = data.toString()
// console.log(str);
if (str === 'ping') {
if (this.config.timeout) {
ping()
}
return
}
this.onMessage(client, str)
})

socket.on('close', () => {
console.log('连接断开')
this.clients.delete(client.id)
if (client.uid !== undefined) {
this.clientsFromUid.delete(client.uid)
}
clearTimeout(timeout)
this.closeHandler && this.closeHandler(client)
})
}

async onMessage(client: Socket, data: string) {
try {
this.handlerCallback?.(client, data)
// const msg = JSON.parse(data);
// const [rid, route, req]: [number, string, any] = msg;

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

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

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

// // 如果有返回值,那么直接回应
// if (res) {
// this.reply(client.socket, rid, res);
// }
} catch (error) {
console.error(error)
}
Expand All @@ -133,7 +83,7 @@ export class Server {
* @param name
* @param data
*/
send(sokcet: WebSocket, name: string | number, data: any) {
send(sokcet: ServerWebSocket, name: string | number, data: any) {
sokcet.send(JSON.stringify([1, name, data]))
}

Expand All @@ -158,8 +108,7 @@ export class Server {
* @param id
* @param data
*/
reply(socket: WebSocket, id: number, data: any) {
// console.log('reply', data);
reply(socket: ServerWebSocket, id: number, data: any) {
this.send(socket, id, data)
}

Expand All @@ -173,8 +122,4 @@ export class Server {
this.isDebug = isDebug
this.doc = config.doc
}

// addHandler(allHandler: any[]) {
// const maps = mapRoute(allHandler);
// }
}
Loading

0 comments on commit a8610f8

Please sign in to comment.