-
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.
cleaner WebSocket hook api, useJsonMessage
- Loading branch information
Showing
7 changed files
with
156 additions
and
48 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,5 @@ | ||
export class ConnectionClosedError extends Error { | ||
constructor() { | ||
super('Connection closed error') | ||
} | ||
} |
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,21 @@ | ||
export class JsonMessageParsingError extends Error { | ||
constructor( | ||
private _connection: WebSocket | undefined, | ||
private _error: unknown, | ||
private _text: string | ||
) { | ||
super('Json message parsing error') | ||
} | ||
|
||
get connection(): WebSocket | undefined { | ||
return this._connection | ||
} | ||
|
||
get error(): unknown { | ||
return this._error | ||
} | ||
|
||
get text(): string { | ||
return this._text | ||
} | ||
} |
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 |
---|---|---|
@@ -1,40 +1,5 @@ | ||
export * from './useClientConnection' | ||
export * from './WebSocketError' | ||
|
||
// export class Connection extends React.Component<ConnectionProps> { | ||
// componentDidMount() { | ||
|
||
// this.connection.onmessage = (packet: MessageEvent<any>) => { | ||
// if (!onMessage) { | ||
// return | ||
// } | ||
// try { | ||
// // TODO: fix | ||
// const workaround = packet as unknown | ||
// const message = processPacketIntoMessage(workaround as Packet) | ||
// onMessage(message) | ||
// } catch (error) { | ||
// if (error instanceof InvalidMessageException) { | ||
// console.log( | ||
// 'Error: invalid message:', | ||
// error.type, | ||
// JSON.stringify(error.packet) | ||
// ) | ||
// } else { | ||
// console.log('Error: parsing message:', error) | ||
// } | ||
// } | ||
// } | ||
// } | ||
export * from './useJsonMessageClientConnection' | ||
|
||
// sendMessage(message: Message) { | ||
// if (!this.connection) { | ||
// throw new Error('Connection is not open') | ||
// } | ||
// sendMessage(this.connection, message) | ||
// } | ||
|
||
// render() { | ||
// return null | ||
// } | ||
// } | ||
export * from './ConnectionClosedError' | ||
export * from './WebSocketError' |
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
88 changes: 88 additions & 0 deletions
88
src/components/WebSocket/useJsonMessageClientConnection.ts
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,88 @@ | ||
import { useCallback } from 'react' | ||
import { JsonMessageParsingError } from './JsonMessageParsingError' | ||
import { useClientConnection, DataType } from './useClientConnection' | ||
|
||
interface Props<T> { | ||
url: string | ||
|
||
onOpen?: (connection: WebSocket | undefined, event: Event) => void | ||
onClose?: (connection: WebSocket | undefined, event: CloseEvent) => void | ||
|
||
binaryType?: BinaryType | ||
|
||
onMessage?: (connection: WebSocket | undefined, data: T) => void | ||
|
||
onError?: (connection: WebSocket | undefined, error: Error) => void | ||
|
||
unmountCloseMessage?: string | ||
} | ||
|
||
interface Result<T> { | ||
send: (data: T) => void | ||
readyState: () => number | ||
close: (code?: number, reason?: string) => void | ||
|
||
internal: { | ||
send: (data: DataType) => void | ||
} | ||
} | ||
|
||
export const useJsonMessageClientConnection = <T>({ | ||
url, | ||
onOpen, | ||
onClose, | ||
|
||
onMessage, | ||
|
||
onError, | ||
|
||
unmountCloseMessage = 'Browsing away' | ||
}: Props<T>): Result<T> => { | ||
// TODO: support binary json sending | ||
|
||
const onMessageDataText = useCallback( | ||
(connection: WebSocket | undefined, text: string) => { | ||
if (!onMessage) { | ||
return | ||
} | ||
try { | ||
const message = JSON.parse(text) | ||
onMessage(connection, message) // TODO: separate and handle with a different try/catch and error report | ||
} catch (error) { | ||
if (onError) { | ||
onError( | ||
connection, | ||
new JsonMessageParsingError(connection, error, text) | ||
) | ||
} | ||
} | ||
}, | ||
[onMessage, onError] | ||
) | ||
|
||
const { | ||
send: connectionSend, | ||
close, | ||
readyState | ||
} = useClientConnection<T>({ | ||
url, | ||
onOpen, | ||
onClose, | ||
onMessageDataText, | ||
onError, | ||
unmountCloseMessage | ||
}) | ||
|
||
const send = useCallback((data: T) => { | ||
connectionSend(JSON.stringify(data)) | ||
}, []) | ||
|
||
return { | ||
send, | ||
readyState, | ||
close, | ||
internal: { | ||
send: connectionSend | ||
} | ||
} | ||
} |