Skip to content

Commit

Permalink
fix: socket in shake
Browse files Browse the repository at this point in the history
  • Loading branch information
tntons committed Mar 28, 2024
1 parent 363c7ce commit 686f740
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
5 changes: 3 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"lint": "next lint"
},
"dependencies": {
"axios": "^1.6.8",
"@fingerprintjs/fingerprintjs": "^4.2.2",
"axios": "^1.6.8",
"next": "14.1.4",
"react": "^18",
"react-dom": "^18",
"socket.io-client": "^4.7.5"
"socket.io-client": "^4.7.5",
"universal-cookie": "^7.1.0"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
25 changes: 22 additions & 3 deletions apps/web/pnpm-lock.yaml

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

32 changes: 19 additions & 13 deletions apps/web/src/app/(user)/shake/[university]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { getMobileOperatingSystem } from "@/utils/getMobileOperatingSystem";
import ShakeComponent from '../../../../components/Shake';
import { useParams, useSearchParams } from "next/navigation";
import { Suspense } from 'react';
import { io } from "socket.io-client";
import { io, Socket } from "socket.io-client";
import FingerprintJS from '@fingerprintjs/fingerprintjs';
import Cookies from 'universal-cookie';

let shaking: { x: number; y: number; z: number } | undefined;

Expand All @@ -17,17 +18,18 @@ function normalize(x: number, y: number, z: number) {
}

export default function Shake() {

const [cid, setCid] = useState<string | null>(null);
let fid: string | null = null;
let socket: Socket | null = null;
const cookies = new Cookies();

useEffect(() => {
const handleConnect = () => {
console.log('Client has connected to the server!');
};

const handleCid = (serverCid: string) => {
setCid(serverCid);
console.log('Received cid from server:');
cookies.set('cid', serverCid);
};

const handleDisconnect = () => {
Expand All @@ -38,14 +40,18 @@ export default function Shake() {
const fp = await FingerprintJS.load();
const result = await fp.get();
fid = result.visitorId;
const savedCid = cookies.get('cid');

const extraHeaders: { [key: string]: string } = {
fid: fid,
name: 'john'
};

const socket = io('https://api.cutu2024.sgcu.in.th/', {
extraHeaders: {
fid: fid,
name: 'john'
}
});

if (savedCid) {
extraHeaders.cid = savedCid;
}
console.log(extraHeaders);
const socket = io('https://api.cutu2024.sgcu.in.th', { extraHeaders, path: "/api/ws", transports: ['websocket'] });
socket.on('connect', handleConnect);
socket.on('cid', handleCid);
socket.on('disconnect', handleDisconnect);
Expand Down Expand Up @@ -94,8 +100,8 @@ export default function Shake() {
};
setCount(prevCount => {
const newCount = prevCount + 1;
if (fid && cid) {
socket.emit('count', { count: newCount, fid, cid });
if (socket) {
socket.emit('events', `${university} ${count}`);
}
return newCount;
});
Expand Down

0 comments on commit 686f740

Please sign in to comment.