-
Notifications
You must be signed in to change notification settings - Fork 241
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
6 changed files
with
79 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function mockAPI({ imitate = {}, delay = 500 } = {}) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ | ||
code: '0000', | ||
data: imitate, | ||
success: true, | ||
}) | ||
}, delay) | ||
}) | ||
} |
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,19 @@ | ||
const ws = new WebSocket('wss://websocket-echo.com/') | ||
|
||
let timeout = null | ||
function heartbeat() { | ||
clearTimeout(timeout) | ||
|
||
timeout = setTimeout(() => { | ||
ws.close() | ||
}, 30000 + 1000) | ||
} | ||
|
||
ws.addEventListener('error', console.error) | ||
ws.addEventListener('open', heartbeat) | ||
ws.addEventListener('ping', heartbeat) | ||
ws.addEventListener('close', () => { | ||
clearTimeout(timeout) | ||
}) | ||
|
||
export default ws |
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,34 @@ | ||
import { WebSocketServer } from 'ws' | ||
|
||
function createWebSocketServer() { | ||
const wss = new WebSocketServer({ port: 8080 }) | ||
|
||
function heartbeat(value = true) { | ||
this.isAlive = value | ||
} | ||
|
||
wss.on('connection', (ws) => { | ||
heartbeat.call(ws) | ||
ws.on('error', console.error) | ||
ws.on('pong', heartbeat) | ||
}) | ||
|
||
const interval = setInterval(() => { | ||
wss.clients.forEach((ws) => { | ||
if (ws.isAlive === false) { | ||
return ws.terminate() | ||
} | ||
|
||
heartbeat.call(ws, false) | ||
ws.ping() | ||
}) | ||
}, 30000) | ||
|
||
wss.on('close', () => { | ||
clearInterval(interval) | ||
}) | ||
|
||
return wss | ||
} | ||
|
||
export default createWebSocketServer |
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