Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
MeowningMaster committed May 24, 2024
1 parent 0607d99 commit f7d0bde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions libs/server/src/broadcast/rediscast.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Redis, type RedisOptions } from "ioredis"
import type { WsxSocket } from "../socket"
import { genId } from "../utils/gen-id"
import { Broadcast, broadcastSymbols } from "./broadcast"
import type { Topic } from "./topic"

/**
* Redis broadcast. Shares actions via Redis Pub/Sub
*/
export class Rediscast extends Broadcast {
private id: string = genId()
redis: Redis
prefix: string

Expand Down
7 changes: 2 additions & 5 deletions libs/server/src/socket/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ServerWebSocket } from "bun"
import type { Topic } from "../broadcast"
import { genId } from "../utils/gen-id"
import * as symbols from "./symbols"
export { symbols as socketSymbols }

Expand All @@ -14,11 +15,7 @@ export type WsxRawSocket = ServerWebSocket<{
*/
export class WsxSocket<Ws extends WsxRawSocket = WsxRawSocket> {
constructor(public raw: Ws) {
if (!this.id) {
const array = new Uint32Array(1)
crypto.getRandomValues(array)
this.id = array[0].toString()
}
this.id ??= genId()
}

static reuse(ws: WsxRawSocket): WsxSocket {
Expand Down
5 changes: 5 additions & 0 deletions libs/server/src/utils/gen-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function genId() {
const array = new Uint32Array(1)
crypto.getRandomValues(array)
return array[0].toString()
}

0 comments on commit f7d0bde

Please sign in to comment.