-
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.
- Loading branch information
1 parent
cc4715f
commit 5a466b2
Showing
9 changed files
with
993 additions
and
1,404 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,64 @@ | ||
"use client" | ||
|
||
import { useEffect, useState } from 'react'; | ||
import Image from 'next/image' | ||
|
||
export default function WebSocketExample({port = 8080}:{port?:number}){ | ||
const [imgUrl, setImgUrl] = useState<string|null>(null); | ||
const [socket, setSocket] = useState<WebSocket|null>(null) | ||
|
||
useEffect(() => { | ||
const socket = new WebSocket(`ws://localhost:${port}`) | ||
|
||
setSocket(socket); | ||
|
||
socket.onopen = () => { | ||
console.log('WebSocket Open'); | ||
socket.send("hello"); | ||
}; | ||
|
||
socket.onclose = () => { | ||
console.log('WebSocket Close'); | ||
}; | ||
|
||
socket.onerror = (error) => { | ||
console.error("WebSocket Error:", error); | ||
}; | ||
|
||
socket.onmessage = (event) => { | ||
try { | ||
const data = JSON.parse(event.data); | ||
console.log("WebSocket Message:", data); | ||
if (data.images && data.images.length > 0) { | ||
setImgUrl(data.images[0]); | ||
} | ||
} catch (e) { | ||
console.error("Error parsing the WebSocket response:", e); | ||
} | ||
}; | ||
|
||
return () => { | ||
socket.close(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
{imgUrl ? ( | ||
<div className="h-[80vh] w-[80vw] relative"> | ||
<Image | ||
className="rounded" | ||
src={imgUrl} | ||
alt="Streamed image" | ||
objectFit="cover" | ||
fill | ||
/> | ||
</div> | ||
): ( | ||
<p>loading ...</p> | ||
)} | ||
</div> | ||
) | ||
}; | ||
|
||
// {/* <img style={{border: "1px solid black"}} src={imgUrl} alt="Received from server" /> */} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.