-
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.
Merge pull request #16 from Nasar165/reconnect
Reconnect
- Loading branch information
Showing
8 changed files
with
149 additions
and
39 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,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 |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import { DEAD, ISocket, LIVE } from '@/app/model/websocket'; | ||
export class Websocket implements ISocket { | ||
private socket?: WebSocket; | ||
import { CloseEvent, DEAD, ISocket, LIVE } from '@/app/model/websocket'; | ||
|
||
export class Socket implements ISocket { | ||
protected socket?: WebSocket; | ||
|
||
Alive(): boolean { | ||
return this.socket != null && this.socket.readyState == this.socket.OPEN; | ||
} | ||
|
||
Start(url: string): void { | ||
if (this.Alive()) throw Error(LIVE); | ||
|
||
this.socket = new WebSocket(url, ['ocpp1.6']); | ||
console.log('Websocket connection was successful'); | ||
console.info('Websocket connection was successful'); | ||
} | ||
|
||
Stop(code?: number): void { | ||
if (!this.Alive()) throw new Error(DEAD); | ||
|
||
console.log('closing socket'); | ||
this.socket!.close(code ?? 1000); | ||
if (this.socket == null) throw new Error(DEAD); | ||
this.socket!.close(code ?? CloseEvent.NORMAL); | ||
console.info('socket closed'); | ||
} | ||
|
||
Send(message: string) { | ||
if (!this.Alive()) throw new Error(DEAD); | ||
|
||
this.socket!.send(message); | ||
} | ||
} |
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,11 +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'; | ||
|
||
interface ISocket { | ||
Alive(): boolean; | ||
Start(url: string, event: CloseEvent): void; | ||
type ConState = (connected: boolean) => void; | ||
|
||
interface ISocket extends IAlive { | ||
Start(url: string, event: ConState): void; | ||
Stop(code: number): void; | ||
} | ||
|
||
export type { ISocket }; | ||
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 }; |
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