-
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
119 additions
and
62 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,16 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { ConState, IWebSocket } from '../model/websocket'; | ||
import { Websocket } from '../lib/websocket/websocket'; | ||
|
||
type Url = { state: ConState }; | ||
|
||
export default function WebSocketHook({ state }: Url): [IWebSocket] { | ||
const [socket, setSocket] = useState<IWebSocket>(); | ||
const ref = useRef(state); | ||
|
||
useEffect(() => { | ||
setSocket(new Websocket(ref.current)); | ||
}, []); | ||
|
||
return [socket!]; | ||
} |
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,75 @@ | ||
import { CloseEvent, ConState, DEFAULT_TIMER } from '@/app/model/websocket'; | ||
import { Socket } from './socket'; | ||
|
||
let id: ReturnType<typeof setTimeout>; | ||
|
||
export class Websocket extends Socket { | ||
private url: string = ''; | ||
private timer = 0; | ||
private stateChange: ConState; | ||
|
||
constructor(event: ConState) { | ||
super(); | ||
this.timer = DEFAULT_TIMER; | ||
this.stateChange = event; | ||
} | ||
|
||
Connect(url: string): void { | ||
this.stateChange(true); | ||
this.url = url; | ||
this.Start(url); | ||
this.setEventListeners(); | ||
} | ||
|
||
Disconnect(code: number): void { | ||
try { | ||
this.stateChange(false); | ||
this.Stop(code); | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
this.removeEventListeners(); | ||
} | ||
} | ||
|
||
protected setEventListeners(): void { | ||
this.socket!.addEventListener('close', this.close.bind(this)); | ||
this.socket!.addEventListener('error', this.error.bind(this)); | ||
this.socket!.addEventListener('open', this.open.bind(this)); | ||
} | ||
|
||
protected removeEventListeners(): void { | ||
this.socket!.removeEventListener('close', this.close.bind(this)); | ||
this.socket!.removeEventListener('error', this.error.bind(this)); | ||
this.socket!.addEventListener('open', this.open.bind(this)); | ||
} | ||
|
||
protected retry(): void { | ||
clearTimeout(id); | ||
console.log('connection lost retrying'); | ||
id = setTimeout(() => { | ||
this.Start(this.url); | ||
}, this.timer); | ||
} | ||
|
||
protected error(ev: Event): void { | ||
console.error('websocket an error has occurred', ev); | ||
const socket = ev.currentTarget as WebSocket; | ||
|
||
if (socket.readyState == socket.CLOSED) { | ||
this.stateChange(false); | ||
this.removeEventListeners(); | ||
this.retry(); | ||
} | ||
} | ||
|
||
protected close(reason: CloseEvent): void { | ||
if (this.socket?.readyState != this.socket?.OPEN) return; | ||
console.info(reason); | ||
this.Stop(CloseEvent.NORMAL); | ||
} | ||
|
||
protected open(): void { | ||
this.stateChange(true); | ||
} | ||
} |
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,13 +1,27 @@ | ||
const CloseEvent = Object.freeze({ | ||
NORMAL: 1000, | ||
INTERNALERROR: 1011, | ||
}); | ||
|
||
const DEFAULT_TIMER = 3600; | ||
const LIVE = 'web socket client is already initialized'; | ||
const DEAD = 'web socket client is dead, open a new connection'; | ||
|
||
type ConState = (connected: boolean) => void; | ||
|
||
interface ISocket { | ||
Alive(): boolean; | ||
interface ISocket extends IAlive { | ||
Start(url: string, event: ConState): void; | ||
Stop(code: number): void; | ||
} | ||
|
||
export type { ISocket, ConState }; | ||
export { LIVE, DEAD }; | ||
interface IWebSocket extends IAlive { | ||
Connect(url: string, event: ConState): void; | ||
Disconnect(code: number): void; | ||
} | ||
|
||
interface IAlive { | ||
Alive(): boolean; | ||
} | ||
|
||
export type { IWebSocket, ISocket, ConState }; | ||
export { LIVE, DEAD, DEFAULT_TIMER, CloseEvent }; |