-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Fix] Vercel deploy error
- Loading branch information
Showing
3 changed files
with
74 additions
and
84 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 |
---|---|---|
@@ -1,88 +1,78 @@ | ||
'use client' | ||
|
||
import React, { useState, useEffect, useRef } from 'react' | ||
|
||
const Chatting = () => { | ||
const chatRoomId = 1 | ||
const member = 1 | ||
const [messages, setMessages] = useState<string[]>([]) | ||
const [input, setInput] = useState<string>('') | ||
const [isConnected, setIsConnected] = useState<boolean>(false) | ||
const socketRef = useRef<WebSocket | null>(null) | ||
|
||
useEffect(() => { | ||
const socket = new WebSocket( | ||
`wss://lc3cc1cnma.execute-api.ap-northeast-2.amazonaws.com/mssaem?chatRoomId=${chatRoomId}&member=${member}`, | ||
) | ||
|
||
socket.onopen = () => { | ||
console.log('WebSocket Opened') | ||
setIsConnected(true) | ||
} | ||
|
||
socket.onmessage = (event: MessageEvent<string>) => { | ||
console.log('WebSocket Message:', event.data) | ||
setMessages((prev) => [...prev, event.data]) | ||
} | ||
|
||
socket.onerror = (error: Event) => { | ||
console.error('WebSocket Error:', error) | ||
} | ||
|
||
socket.onclose = () => { | ||
console.log('WebSocket is closed') | ||
setIsConnected(false) | ||
} | ||
|
||
socketRef.current = socket | ||
|
||
return () => { | ||
socket.close() | ||
} | ||
}, [chatRoomId, member]) | ||
|
||
const sendMessage = () => { | ||
if (socketRef.current && input.trim() !== '') { | ||
const message = { | ||
action: 'sendMessage', | ||
chatRoomId: 1, | ||
message: input, | ||
} | ||
socketRef.current.send(JSON.stringify(message)) | ||
setInput('') | ||
} | ||
} | ||
|
||
const disconnect = () => { | ||
if (socketRef.current) { | ||
socketRef.current.close() | ||
socketRef.current = null | ||
setIsConnected(false) | ||
} | ||
} | ||
// const chatRoomId = 1 | ||
// const member = 1 | ||
// const [messages, setMessages] = useState<string[]>([]) | ||
// const [input, setInput] = useState<string>('') | ||
// const [isConnected, setIsConnected] = useState<boolean>(false) | ||
// const socketRef = useRef<WebSocket | null>(null) | ||
// useEffect(() => { | ||
// const socket = new WebSocket( | ||
// `wss://lc3cc1cnma.execute-api.ap-northeast-2.amazonaws.com/mssaem?chatRoomId=${chatRoomId}&member=${member}`, | ||
// ) | ||
// socket.onopen = () => { | ||
// console.log('WebSocket Opened') | ||
// setIsConnected(true) | ||
// } | ||
// socket.onmessage = (event: MessageEvent<string>) => { | ||
// console.log('WebSocket Message:', event.data) | ||
// setMessages((prev) => [...prev, event.data]) | ||
// } | ||
// socket.onerror = (error: Event) => { | ||
// console.error('WebSocket Error:', error) | ||
// } | ||
// socket.onclose = () => { | ||
// console.log('WebSocket is closed') | ||
// setIsConnected(false) | ||
// } | ||
// socketRef.current = socket | ||
// return () => { | ||
// socket.close() | ||
// } | ||
// }, [chatRoomId, member]) | ||
// const sendMessage = () => { | ||
// if (socketRef.current && input.trim() !== '') { | ||
// const message = { | ||
// action: 'sendMessage', | ||
// chatRoomId: 1, | ||
// message: input, | ||
// } | ||
// socketRef.current.send(JSON.stringify(message)) | ||
// setInput('') | ||
// } | ||
// } | ||
// const disconnect = () => { | ||
// if (socketRef.current) { | ||
// socketRef.current.close() | ||
// socketRef.current = null | ||
// setIsConnected(false) | ||
// } | ||
// } | ||
// return ( | ||
// <div> | ||
// <h1>Chatting</h1> | ||
// <div> | ||
// {messages.map((msg, index) => ( | ||
// <div key={index}>{msg}</div> | ||
// ))} | ||
// </div> | ||
// <input | ||
// type="text" | ||
// value={input} | ||
// onChange={(e) => setInput(e.target.value)} | ||
// onKeyPress={(e) => { | ||
// if (e.key === 'Enter') { | ||
// sendMessage() | ||
// } | ||
// }} | ||
// /> | ||
// {/* <button onClick={sendMessage}>Send</button> | ||
// {isConnected && <button onClick={disconnect}>Disconnect</button>} */} | ||
// </div> | ||
// ) | ||
|
||
return ( | ||
<div> | ||
<h1>Chatting</h1> | ||
<div> | ||
{messages.map((msg, index) => ( | ||
<div key={index}>{msg}</div> | ||
))} | ||
</div> | ||
<input | ||
type="text" | ||
value={input} | ||
onChange={(e) => setInput(e.target.value)} | ||
onKeyPress={(e) => { | ||
if (e.key === 'Enter') { | ||
sendMessage() | ||
} | ||
}} | ||
/> | ||
<button onClick={sendMessage}>Send</button> | ||
{isConnected && <button onClick={disconnect}>Disconnect</button>} | ||
</div> | ||
) | ||
return <div>test</div> | ||
} | ||
|
||
export default Chatting |
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