-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
215 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { BaseServer } from "../BaseServer"; | ||
import { IServer } from "../IServer"; | ||
import { Socket } from "../socket"; | ||
import { Socket as TcpSocket } from "bun"; | ||
|
||
type SocketData = { socketId: number }; | ||
|
||
export class TcpServer extends BaseServer<TcpSocket<SocketData>> implements IServer { | ||
|
||
constructor(public port: number) { | ||
super(); | ||
} | ||
|
||
start(): void { | ||
Bun.listen<SocketData>({ | ||
hostname: "0.0.0.0", | ||
port: this.port, | ||
socket: { | ||
open: (socket) => { | ||
const client = new Socket(this) | ||
socket.data.socketId = client.id; | ||
this.clients.set(client.id, socket) | ||
this.sockets.set(client.id, client); | ||
}, | ||
data: (socket, data) => { | ||
const client = this.sockets.get(socket.data.socketId)!; | ||
try { | ||
this.handlerCallback?.(client, data.toString()) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}, | ||
close: (socket) => { | ||
this.sockets.delete(socket.data.socketId); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
send(socket: TcpSocket<SocketData>, name: string | number, data: any): void { | ||
socket.write(JSON.stringify([1, name, data])); | ||
} | ||
} |
Oops, something went wrong.