-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add heartbeat params
responseTimeout
- Loading branch information
Showing
5 changed files
with
163 additions
and
65 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,61 @@ | ||
import React, { useRef, useMemo } from 'react'; | ||
import { useWebSocket } from 'ahooks'; | ||
|
||
enum ReadyState { | ||
Connecting = 0, | ||
Open = 1, | ||
Closing = 2, | ||
Closed = 3, | ||
} | ||
|
||
export default () => { | ||
const messageHistory = useRef<any[]>([]); | ||
|
||
const { readyState, sendMessage, latestMessage, disconnect, connect } = useWebSocket( | ||
'wss://ws.postman-echo.com/raw', | ||
{ | ||
heartbeat: { | ||
interval: 1000 * 3, | ||
}, | ||
}, | ||
); | ||
|
||
messageHistory.current = useMemo( | ||
() => messageHistory.current.concat(latestMessage), | ||
[latestMessage], | ||
); | ||
|
||
return ( | ||
<div> | ||
{/* send message */} | ||
<button | ||
onClick={() => sendMessage && sendMessage(`${Date.now()}`)} | ||
disabled={readyState !== ReadyState.Open} | ||
style={{ marginRight: 8 }} | ||
> | ||
✉️ send | ||
</button> | ||
{/* disconnect */} | ||
<button | ||
onClick={() => disconnect && disconnect()} | ||
disabled={readyState !== ReadyState.Open} | ||
style={{ marginRight: 8 }} | ||
> | ||
❌ disconnect | ||
</button> | ||
{/* connect */} | ||
<button onClick={() => connect && connect()} disabled={readyState === ReadyState.Open}> | ||
{readyState === ReadyState.Connecting ? 'connecting' : '📞 connect'} | ||
</button> | ||
<div style={{ marginTop: 8 }}>readyState: {readyState}</div> | ||
<div style={{ marginTop: 8 }}> | ||
<p>received message: </p> | ||
{messageHistory.current.map((message, index) => ( | ||
<p key={index} style={{ wordWrap: 'break-word' }}> | ||
{message?.data} | ||
</p> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; |
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