-
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
5 changed files
with
95 additions
and
74 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
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,3 @@ | ||
import { WebSocket as _WebSocket } from "ws"; | ||
|
||
export default globalThis.WebSocket || _WebSocket; |
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 |
---|---|---|
@@ -1,79 +1,79 @@ | ||
import { ServerWebSocket } from "bun"; | ||
import { IWebsocketServer } from "./IWebSocketServer"; | ||
import { IWebsocketServer } from "./IWebsocketServer"; | ||
|
||
const ALL = '_:world'; | ||
export const createWebsocketServer = (options: {port?: number, timeout?: number} = {}): IWebsocketServer => { | ||
export const createWebsocketServer = (options: { port?: number, timeout?: number } = {}): IWebsocketServer => { | ||
|
||
const allTimeout: Map<ServerWebSocket, Timer> = new Map(); | ||
const ping = (ws: ServerWebSocket) => { | ||
if (!options.timeout || options.timeout < 0) { | ||
return | ||
} | ||
clearTimeout(allTimeout.get(ws)); | ||
allTimeout.set(ws, setTimeout(ws.close, options.timeout)); | ||
const allTimeout: Map<ServerWebSocket, Timer> = new Map(); | ||
const ping = (ws: ServerWebSocket) => { | ||
if (!options.timeout || options.timeout < 0) { | ||
return | ||
} | ||
clearTimeout(allTimeout.get(ws)); | ||
allTimeout.set(ws, setTimeout(ws.close, options.timeout)); | ||
} | ||
|
||
const stopPing = (ws: ServerWebSocket) => { | ||
clearTimeout(allTimeout.get(ws)); | ||
allTimeout.delete(ws); | ||
} | ||
const stopPing = (ws: ServerWebSocket) => { | ||
clearTimeout(allTimeout.get(ws)); | ||
allTimeout.delete(ws); | ||
} | ||
|
||
let openHandler: (ws: ServerWebSocket) => void; | ||
let messageHandler: (ws: ServerWebSocket, message: string | Buffer) => void; | ||
let closeHandler: (ws: ServerWebSocket) => void; | ||
let openHandler: (ws: ServerWebSocket) => void; | ||
let messageHandler: (ws: ServerWebSocket, message: string | Buffer) => void; | ||
let closeHandler: (ws: ServerWebSocket) => void; | ||
|
||
const open = (handler: typeof openHandler) => { | ||
openHandler = handler; | ||
} | ||
const message = (handler: typeof messageHandler) => { | ||
messageHandler = handler; | ||
} | ||
const open = (handler: typeof openHandler) => { | ||
openHandler = handler; | ||
} | ||
const message = (handler: typeof messageHandler) => { | ||
messageHandler = handler; | ||
} | ||
|
||
const close = (handler: typeof closeHandler) => { | ||
closeHandler = handler; | ||
} | ||
Bun.serve<undefined>({ | ||
port: 3000, | ||
fetch(req, server) { | ||
if ( | ||
server.upgrade(req, { | ||
data: { | ||
}, | ||
}) | ||
) | ||
return; | ||
return new Response("Error"); | ||
}, | ||
websocket: { | ||
open(ws) { | ||
ws.subscribe(ALL); | ||
options.timeout && ping(ws); | ||
openHandler && openHandler(ws); | ||
}, | ||
message(ws, message) { | ||
if (message === 'ping') { | ||
if (options.timeout) { | ||
ping(ws); | ||
} | ||
} else{ | ||
messageHandler && messageHandler(ws, message); | ||
} | ||
}, | ||
close(ws) { | ||
ws.unsubscribe(ALL); | ||
options.timeout && stopPing(ws); | ||
closeHandler && closeHandler(ws); | ||
}, | ||
perMessageDeflate: false, | ||
publishToSelf: true, | ||
}, | ||
}); | ||
return { | ||
open, | ||
message, | ||
close | ||
} | ||
const close = (handler: typeof closeHandler) => { | ||
closeHandler = handler; | ||
} | ||
|
||
Bun.serve<undefined>({ | ||
port: 3000, | ||
fetch(req, server) { | ||
if ( | ||
server.upgrade(req, { | ||
data: { | ||
}, | ||
}) | ||
) | ||
return; | ||
|
||
return new Response("Error"); | ||
}, | ||
websocket: { | ||
open(ws) { | ||
ws.subscribe(ALL); | ||
options.timeout && ping(ws); | ||
openHandler && openHandler(ws); | ||
}, | ||
message(ws, message) { | ||
if (message === 'ping') { | ||
if (options.timeout) { | ||
ping(ws); | ||
} | ||
} else { | ||
messageHandler && messageHandler(ws, message); | ||
} | ||
}, | ||
close(ws) { | ||
ws.unsubscribe(ALL); | ||
options.timeout && stopPing(ws); | ||
closeHandler && closeHandler(ws); | ||
}, | ||
perMessageDeflate: false, | ||
publishToSelf: true, | ||
}, | ||
}); | ||
|
||
return { | ||
open, | ||
message, | ||
close | ||
} | ||
} |