Skip to content

Commit

Permalink
[Fix] 서버 주소 변경 (#25)
Browse files Browse the repository at this point in the history
* feat: PWA 적용

* feat: Toast 알람시 TTS 기능 추가

* feat: 서버 주소 변경
  • Loading branch information
jwo0o0 authored Feb 22, 2024
1 parent 7d3581b commit 5f4edb8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/api/instance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";

export const axiosInstance = axios.create({
baseURL: "http://13.209.80.41:8080",
baseURL: "http://3.38.211.169:8080",
headers: {
"Content-Type": "application/json",
},
Expand Down
40 changes: 40 additions & 0 deletions src/components/Drive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,63 @@ import styled from "styled-components";
import car_driving_img from "@assets/images/car_driving.gif";
import { useNavigate } from "react-router-dom";
import { drivingEndAPI } from "@api/drivingAPIS";
import { useEffect, useState } from "react";
import { Toast } from "@components/common/Toast";

export const Drive = () => {
const [toast, setToast] = useState<boolean>(false);
const [message, setMessage] = useState<string>("졸음 운전이 감지되었습니다");

const navigate = useNavigate();
let eventSource: any;

const handleClickArriveBtn = () => {
const apiResponse = drivingEndAPI();
apiResponse.then((res) => {
console.log(res);
navigate(`/end/${res.reportId}`);
});
eventSource.close();
};

useEffect(() => {
window.speechSynthesis.getVoices();
}, []);

const fetchSSE = async () => {
try {
eventSource = new EventSource("/url");
eventSource.onmessage = async (event: any) => {
const response = await event.data;
console.log(response);
console.log("sse event!");
setToast(true);
setMessage("message");
};
eventSource.onerror = async (event: any) => {
eventSource.close();
console.error(event);
};
} catch {}
};

useEffect(() => {
fetchSSE();
}, []);

return (
<DriveContainer>
<img src={car_driving_img} alt="주행 중" />
<InfoText>주행 중입니다</InfoText>
<button
onClick={() => {
setToast(true);
}}
>
클릭
</button>
<ArriveButton onClick={handleClickArriveBtn}>목적지 도착</ArriveButton>
{toast && <Toast message={message} setToast={setToast} />}
</DriveContainer>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/components/common/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useEffect } from "react";
import styled from "styled-components";
import alarm_sound from "@assets/sound/alram.mp3";
import { getSpeech } from "@utils/getSpeech";

interface ToastProps {
message: string;
setToast: React.Dispatch<React.SetStateAction<boolean>>;
}
export const Toast = ({ message, setToast }: ToastProps) => {
getSpeech(message);
useEffect(() => {
const timer = setTimeout(() => {
setToast(false);
Expand Down
33 changes: 33 additions & 0 deletions src/utils/getSpeech.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-ignore

export const getSpeech = (text: string) => {
let voices: any[] = [];

const setVoiceList = () => {
voices = window.speechSynthesis.getVoices();
};
setVoiceList();

window.speechSynthesis.onvoiceschanged = setVoiceList;

const speech = (text: string) => {
const lang = "ko-KR";
const utterThis = new SpeechSynthesisUtterance(text);

utterThis.lang = lang;

const kor_voice = voices.find(
(elem) => elem.lang == lang || elem.lang === lang.replace("-", "_")
);

if (kor_voice) {
utterThis.voice = kor_voice;
} else {
return;
}

window.speechSynthesis.speak(utterThis);
};

speech(text);
};

0 comments on commit 5f4edb8

Please sign in to comment.