Skip to content

Commit

Permalink
add websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nasar165 committed May 3, 2024
1 parent aa2ba19 commit 692c669
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 6 deletions.
29 changes: 29 additions & 0 deletions app/components/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SyntheticEvent } from 'react';

type InputEvent = (event: SyntheticEvent<HTMLInputElement>) => void;

type input = {
name: string;
value: string;
onChange: InputEvent;
};

export default function Input({ name, value, onChange }: input): JSX.Element {
return (
<div className='px-4'>
<label className='block text-sm font-medium leading-6 text-gray-900'>
Websocket Url
</label>
<div className='relative mt-2 rounded-md shadow-sm'>
<input
name={name}
type='text'
defaultValue={value}
onChange={onChange}
className='block w-full rounded-md border-0 py-1.5 pl-7 pr-20 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6'
placeholder='wss://'
/>
</div>
</div>
);
}
47 changes: 47 additions & 0 deletions app/components/socket.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use client';

import React, { useEffect, useState } from 'react';
import { Websocket } from '../lib/websocket/socket';

type Url = { url: string };

export default function Socket({ url }: Url): React.JSX.Element {
const [socket, setSocket] = useState<Websocket>();

const cleanup = () => {
try {
console.log('clean up lose connection');
console.log(socket);

socket?.Stop(1000);
} catch (err) {
console.error(err);
}
};

const start = () => {
try {
if (!socket?.Alive()) {
socket?.Start(url);
setSocket(socket);
} else socket?.Stop(1000);
} catch (err) {
console.error(err);
}
};

useEffect(() => {
setSocket(new Websocket());
}, []);

return (
<div>
<button
className='border border-blue-600 rounded-md px-4 py-2 my-4'
onClick={() => start()}
>
{socket?.Alive() ? 'Disconnect' : 'Connect'}
</button>
</div>
);
}
28 changes: 28 additions & 0 deletions app/lib/websocket/socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DEAD, ISocket, LIVE } from '@/app/model/websocket';
export class Websocket implements ISocket {
private 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');
}

Stop(code?: number): void {
if (!this.Alive()) throw new Error(DEAD);

console.log('closing socket');
this.socket!.close(code ?? 1000);
}

Send(message: string) {
if (!this.Alive()) throw new Error(DEAD);

this.socket!.send(message);
}
}
11 changes: 11 additions & 0 deletions app/model/websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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;
Stop(code: number): void;
}

export type { ISocket };
export { LIVE, DEAD };
24 changes: 23 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
'use client';

import { SyntheticEvent, useState } from 'react';
import Websocket from './components/socket';
import Input from './components/input';

export default function Home() {
return <div className='bg-red-600 text-center'>Hello world</div>;
const [url, setUrl] = useState(
'ws://localhost:8080/ocpp/JwNpTpPxPm/CHR202305102'
);

const onChange = (event: SyntheticEvent<HTMLInputElement>) => {
console.log(event.currentTarget.value);

setUrl(event.currentTarget.value);
};

return (
<div className='pt-6 text-center'>
<h1 className='text-4xl'>OCPP 1.6-J EVSE Test tool</h1>
<div className='w-1/3 mx-auto mt-8'>
<Input name='url' value={url} onChange={onChange} />
<Websocket url={url} />
</div>
</div>
);
}
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
"lint": "next lint"
},
"dependencies": {
"next": "14.2.3",
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/ws": "^8.5.10",
"eslint": "^8",
"eslint-config-next": "14.2.3",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "14.2.3"
"typescript": "^5"
}
}

0 comments on commit 692c669

Please sign in to comment.